From 27edc6676dc16827511705938a1a9ccaf998520b Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 8 Mar 2022 08:21:22 +0000 Subject: [PATCH] db: add foreign key contraint to AdministrateursProcedure In production there are around 20 000 AdministrateursProcedure records that reference a deleted Procedure. --- ...edure_foreign_key_to_administrateurs_procedure.rb | 12 ++++++++++++ db/schema.rb | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20220308110720_add_procedure_foreign_key_to_administrateurs_procedure.rb diff --git a/db/migrate/20220308110720_add_procedure_foreign_key_to_administrateurs_procedure.rb b/db/migrate/20220308110720_add_procedure_foreign_key_to_administrateurs_procedure.rb new file mode 100644 index 000000000..2b6fc5304 --- /dev/null +++ b/db/migrate/20220308110720_add_procedure_foreign_key_to_administrateurs_procedure.rb @@ -0,0 +1,12 @@ +class AddProcedureForeignKeyToAdministrateursProcedure < ActiveRecord::Migration[6.1] + include Database::MigrationHelpers + + def up + delete_orphans :administrateurs_procedures, :procedures + add_foreign_key :administrateurs_procedures, :procedures + end + + def down + remove_foreign_key :administrateurs_procedures, :procedures + end +end diff --git a/db/schema.rb b/db/schema.rb index 16c13c8d7..3e2d4a1f7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_03_02_101337) do +ActiveRecord::Schema.define(version: 2022_03_08_110720) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -855,6 +855,7 @@ ActiveRecord::Schema.define(version: 2022_03_02_101337) do add_foreign_key "administrateurs_instructeurs", "administrateurs" add_foreign_key "administrateurs_instructeurs", "instructeurs" add_foreign_key "administrateurs_procedures", "administrateurs" + add_foreign_key "administrateurs_procedures", "procedures" add_foreign_key "archives_groupe_instructeurs", "archives" add_foreign_key "archives_groupe_instructeurs", "groupe_instructeurs" add_foreign_key "assign_tos", "groupe_instructeurs"