Skip to content

Commit

Permalink
db: restore regular primary keys
Browse files Browse the repository at this point in the history
This is dodgy (re: changing migrations 6 months later) but since we're
not live yet we can afford to just blow up the databases and start
fresh, which is far more value than trying to maintain integrity over
a DB that isn't live yet.
  • Loading branch information
freesteph committed Oct 23, 2023
1 parent cac4caf commit 040b355
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 60 deletions.
2 changes: 1 addition & 1 deletion app/controllers/pfmps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def set_pfmp
end

def set_student
@student = @classe.students.find_by(ine: params[:student_id])
@student = @classe.students.find(params[:student_id])
end

def set_classe
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/students_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def show
private

def set_student
@student = @classe.students.includes(:rib, :pfmps).find_by(ine: params[:id])
@student = @classe.students.includes(:rib, :pfmps).find(params[:id])
end

def set_classe
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,16 @@ def parse_identity

def save_roles!
@mapper.establishments.each do |e|
e.save! unless e.persisted?

EstablishmentUserRole
.where(user: @user, establishment: e, role: :dir)
.first_or_create
end

@mapper.authorised_establishments_for(@user.email).each do |e|
e.save! unless e.persisted?

EstablishmentUserRole
.where(user: @user, establishment: e, role: :authorised)
.first_or_create
Expand Down
2 changes: 0 additions & 2 deletions app/models/establishment.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

class Establishment < ApplicationRecord
self.primary_key = "uai"

validates :uai, presence: true, uniqueness: true

has_many :invitations, dependent: :nullify
Expand Down
2 changes: 0 additions & 2 deletions app/models/student.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

class Student < ApplicationRecord
self.primary_key = "ine"

validates :ine, :first_name, :last_name, :birthdate, presence: true

has_many :schoolings, dependent: :destroy
Expand Down
8 changes: 4 additions & 4 deletions db/migrate/20230512084221_create_establishments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

class CreateEstablishments < ActiveRecord::Migration[7.0]
def change
create_table :establishments, id: false do |t|
t.string :uai, primary_key: true, null: false
create_table :establishments do |t|
t.string :uai, null: false
t.string :name, null: false
t.string :denomination, null: false
t.string :nature, null: false

t.timestamps
end

add_index :establishments, :uai, unique: true
t.index :uai, unique: true
end
end
end
4 changes: 1 addition & 3 deletions db/migrate/20230516161726_create_classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
class CreateClasses < ActiveRecord::Migration[7.0]
def change
create_table :classes do |t|
t.references :establishment
t.references :mefstat, null: false, foreign_key: true
t.string :label

t.timestamps
end

add_column :classes, :establishment_id, :string, null: false
add_foreign_key :classes, :establishments, primary_key: "uai"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

class AddEstablishmentIdToPrincipals < ActiveRecord::Migration[7.0]
def change
add_column :principals, :establishment_id, :string, null: false
add_foreign_key :principals, :establishments, primary_key: "uai"
change_table :principals do |t|
t.references :establishment, foreign_key: true, null: false
end
end
end
7 changes: 4 additions & 3 deletions db/migrate/20230522131051_create_students.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

class CreateStudents < ActiveRecord::Migration[7.0]
def change
create_table :students, id: false do |t|
t.references :classe, null: false, foreign_key: true
t.string :ine, primary_key: true, null: false
create_table :students do |t|
t.string :ine, null: false
t.string :first_name
t.string :last_name

t.timestamps

t.index :ine, unique: true
end
end
end
3 changes: 0 additions & 3 deletions db/migrate/20230522230816_create_pfmps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,5 @@ def change
t.date :end_date
t.timestamps
end

add_column :pfmps, :student_id, :string, null: false
add_foreign_key :pfmps, :students, primary_key: "ine"
end
end
4 changes: 1 addition & 3 deletions db/migrate/20230606160103_create_ribs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
class CreateRibs < ActiveRecord::Migration[7.0]
def change
create_table :ribs do |t|
t.references :student, null: false, foreign_key: true
t.string :iban
t.string :bic
t.timestamp :archived_at

t.timestamps
end

add_column :ribs, :student_id, :string, null: false # rubocop:disable Rails/NotNullColumn
add_foreign_key :ribs, :students, primary_key: "ine"
end
end
14 changes: 3 additions & 11 deletions db/migrate/20230826134628_create_schoolings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,20 @@
class CreateSchoolings < ActiveRecord::Migration[7.0]
def change
create_table(:schoolings) do |t|
t.references :student, null: false, foreign_key: true
t.references :classe, null: false, foreign_key: true
t.date :start_date
t.date :end_date
t.references :student, type: :string, null: false
t.references :classe, null: false, foreign_key: true

t.timestamps
end

change_table(:pfmps) do |t|
t.remove_references :student, primary_key: "ine"
t.references :schooling, foreign_key: true, null: false
end

change_table(:students) do |t|
t.remove_references :classe, null: false
t.column :current_schooling, :bigint

t.foreign_key :schoolings, column: :current_schooling
t.references :current_schooling, foreign_key: { to_table: :schoolings }
end

# we need to do this here as `t.references` doesn't let us specify
# the different primary key name (I think).
add_foreign_key :schoolings, :students, primary_key: "ine"
end
end
4 changes: 1 addition & 3 deletions db/migrate/20231011223534_create_invitations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
class CreateInvitations < ActiveRecord::Migration[7.1]
def change
create_table :invitations do |t|
t.references :establishment, null: false, foreign_key: true
t.references :user, null: false, foreign_key: true

t.string :email, null: false

t.timestamps
end

add_column :invitations, :establishment_id, :string, null: false # rubocop:disable Rails:NotNullColumn
add_foreign_key :invitations, :establishments, primary_key: "uai"

add_index :invitations, %i[establishment_id email], unique: true
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
class CreateJoinTableEstablishmentUsers < ActiveRecord::Migration[7.1]
def change
create_table :establishment_users, id: false do |t|
t.references :user, foreign_key: true
t.references :establishment, foreign_key: true, null: false
t.references :user, foreign_key: true, null: false
t.references :granted_by, foreign_key: { to_table: :users }

t.integer :role, null: false
end

add_column :establishment_users, :establishment_id, :string, null: false # rubocop:disable Rails:NotNullColumn
add_foreign_key :establishment_users, :establishments, primary_key: "uai"
end
end
44 changes: 26 additions & 18 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 040b355

Please sign in to comment.