db: add constraints to role tables

This commit is contained in:
Pierre de La Morinerie 2022-03-22 15:53:38 +00:00
parent 768b577f1e
commit 017625207e
8 changed files with 60 additions and 7 deletions

View file

@ -7,7 +7,7 @@
# encrypted_token :string # encrypted_token :string
# created_at :datetime # created_at :datetime
# updated_at :datetime # updated_at :datetime
# user_id :bigint # user_id :bigint not null
# #
class Administrateur < ApplicationRecord class Administrateur < ApplicationRecord
include ActiveRecord::SecureToken include ActiveRecord::SecureToken

View file

@ -5,7 +5,7 @@
# id :bigint not null, primary key # id :bigint not null, primary key
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# user_id :bigint # user_id :bigint not null
# #
class Expert < ApplicationRecord class Expert < ApplicationRecord
belongs_to :user belongs_to :user

View file

@ -9,7 +9,7 @@
# created_at :datetime # created_at :datetime
# updated_at :datetime # updated_at :datetime
# agent_connect_id :string # agent_connect_id :string
# user_id :bigint # user_id :bigint not null
# #
class Instructeur < ApplicationRecord class Instructeur < ApplicationRecord
has_and_belongs_to_many :administrateurs has_and_belongs_to_many :administrateurs

View file

@ -0,0 +1,18 @@
class AddNotNullConstraintsToRoleTables < ActiveRecord::Migration[6.1]
include Database::MigrationHelpers
def change
# If this migration fails, that means you need to run the matching data migration task first.
# Please run:
# bin/rake after_party:copy_user_association_to_user_related_models
# bin/rake after_party:delete_roles_without_users
#
# (We ignore strong_migrations safety warnings, because those tables are relatively small, and the null check
# will be very fast.)
safety_assured do
change_column_null :administrateurs, :user_id, false
change_column_null :instructeurs, :user_id, false
change_column_null :experts, :user_id, false
end
end
end

View file

@ -0,0 +1,10 @@
class AddForeignKeysToRoleTables < ActiveRecord::Migration[6.1]
def change
# Add foreign keys constraints to role tables.
#
# (We don't validate foreign keys right now, to avoid blocking writes to these tables for too long.)
add_foreign_key :administrateurs, :users, validate: false
add_foreign_key :instructeurs, :users, validate: false
add_foreign_key :experts, :users, validate: false
end
end

View file

@ -0,0 +1,10 @@
class ValidateForeignKeysToRoleTables < ActiveRecord::Migration[6.1]
disable_ddl_transaction!
def change
# Now that the foreign keys are added, we can validate them safely without blocking writes.
validate_foreign_key :administrateurs, :users
validate_foreign_key :instructeurs, :users
validate_foreign_key :experts, :users
end
end

View file

@ -0,0 +1,9 @@
class AddIndexToRoleTables < ActiveRecord::Migration[6.1]
disable_ddl_transaction!
def change
add_index :administrateurs, :user_id, algorithm: :concurrently
add_index :instructeurs, :user_id, algorithm: :concurrently
add_index :experts, :user_id, algorithm: :concurrently
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_03_22_110900) do ActiveRecord::Schema.define(version: 2022_03_23_113327) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -60,7 +60,8 @@ ActiveRecord::Schema.define(version: 2022_03_22_110900) do
t.datetime "created_at" t.datetime "created_at"
t.string "encrypted_token" t.string "encrypted_token"
t.datetime "updated_at" t.datetime "updated_at"
t.bigint "user_id" t.bigint "user_id", null: false
t.index ["user_id"], name: "index_administrateurs_on_user_id"
end end
create_table "administrateurs_instructeurs", id: false, force: :cascade do |t| create_table "administrateurs_instructeurs", id: false, force: :cascade do |t|
@ -410,7 +411,8 @@ ActiveRecord::Schema.define(version: 2022_03_22_110900) do
create_table "experts", force: :cascade do |t| create_table "experts", force: :cascade do |t|
t.datetime "created_at", precision: 6, null: false t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false
t.bigint "user_id" t.bigint "user_id", null: false
t.index ["user_id"], name: "index_experts_on_user_id"
end end
create_table "experts_procedures", force: :cascade do |t| create_table "experts_procedures", force: :cascade do |t|
@ -539,8 +541,9 @@ ActiveRecord::Schema.define(version: 2022_03_22_110900) do
t.text "encrypted_login_token" t.text "encrypted_login_token"
t.datetime "login_token_created_at" t.datetime "login_token_created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.bigint "user_id" t.bigint "user_id", null: false
t.index ["agent_connect_id"], name: "index_instructeurs_on_agent_connect_id", unique: true t.index ["agent_connect_id"], name: "index_instructeurs_on_agent_connect_id", unique: true
t.index ["user_id"], name: "index_instructeurs_on_user_id"
end end
create_table "invites", id: :serial, force: :cascade do |t| create_table "invites", id: :serial, force: :cascade do |t|
@ -852,6 +855,7 @@ ActiveRecord::Schema.define(version: 2022_03_22_110900) do
end end
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "administrateurs", "users"
add_foreign_key "administrateurs_instructeurs", "administrateurs" add_foreign_key "administrateurs_instructeurs", "administrateurs"
add_foreign_key "administrateurs_instructeurs", "instructeurs" add_foreign_key "administrateurs_instructeurs", "instructeurs"
add_foreign_key "administrateurs_procedures", "administrateurs" add_foreign_key "administrateurs_procedures", "administrateurs"
@ -876,12 +880,14 @@ ActiveRecord::Schema.define(version: 2022_03_22_110900) do
add_foreign_key "dossiers", "groupe_instructeurs" add_foreign_key "dossiers", "groupe_instructeurs"
add_foreign_key "dossiers", "procedure_revisions", column: "revision_id" add_foreign_key "dossiers", "procedure_revisions", column: "revision_id"
add_foreign_key "dossiers", "users" add_foreign_key "dossiers", "users"
add_foreign_key "experts", "users"
add_foreign_key "experts_procedures", "experts" add_foreign_key "experts_procedures", "experts"
add_foreign_key "experts_procedures", "procedures" add_foreign_key "experts_procedures", "procedures"
add_foreign_key "france_connect_informations", "users" add_foreign_key "france_connect_informations", "users"
add_foreign_key "geo_areas", "champs" add_foreign_key "geo_areas", "champs"
add_foreign_key "groupe_instructeurs", "procedures" add_foreign_key "groupe_instructeurs", "procedures"
add_foreign_key "initiated_mails", "procedures" add_foreign_key "initiated_mails", "procedures"
add_foreign_key "instructeurs", "users"
add_foreign_key "merge_logs", "users" add_foreign_key "merge_logs", "users"
add_foreign_key "procedure_presentations", "assign_tos" add_foreign_key "procedure_presentations", "assign_tos"
add_foreign_key "procedure_revision_types_de_champ", "procedure_revision_types_de_champ", column: "parent_id" add_foreign_key "procedure_revision_types_de_champ", "procedure_revision_types_de_champ", column: "parent_id"