db: backport delete_orphans
in recent migrations
Recent migrations used ActiveRecord when removing invalid data. This may break if the ActiveRecord model later changes. To ensure these migrations will run correctly, even when the code will have changed, let's use an SQL-based helper instead of ActiveRecord.
This commit is contained in:
parent
3a16235868
commit
a4108c7787
2 changed files with 9 additions and 18 deletions
|
@ -1,12 +1,9 @@
|
||||||
class AddAdministrateurForeignKeyToAdministrateursProcedure < ActiveRecord::Migration[6.1]
|
class AddAdministrateurForeignKeyToAdministrateursProcedure < ActiveRecord::Migration[6.1]
|
||||||
def up
|
include Database::MigrationHelpers
|
||||||
# Sanity check
|
|
||||||
say_with_time 'Removing AdministrateursProcedures where the associated Administrateur no longer exists ' do
|
|
||||||
deleted_administrateur_ids = AdministrateursProcedure.where.missing(:administrateur).pluck(:administrateur_id)
|
|
||||||
AdministrateursProcedure.where(administrateur_id: deleted_administrateur_ids).delete_all
|
|
||||||
end
|
|
||||||
|
|
||||||
add_foreign_key :administrateurs_procedures, :administrateurs
|
def up
|
||||||
|
delete_orphans :administrateurs_procedures, :administrateurs_procedures
|
||||||
|
add_foreign_key :administrateurs_procedures, :administrateurs_procedures
|
||||||
end
|
end
|
||||||
|
|
||||||
def down
|
def down
|
||||||
|
|
|
@ -1,17 +1,11 @@
|
||||||
class AddForeignKeysToAdministrateursInstructeurs < ActiveRecord::Migration[6.1]
|
class AddForeignKeysToAdministrateursInstructeurs < ActiveRecord::Migration[6.1]
|
||||||
|
include Database::MigrationHelpers
|
||||||
|
|
||||||
def up
|
def up
|
||||||
# Sanity check
|
delete_orphans :administrateurs_instructeurs, :administrateurs
|
||||||
say_with_time 'Removing AdministrateursInstructeur where the associated Administrateur no longer exists ' do
|
|
||||||
deleted_administrateurs_ids = AdministrateursInstructeur.where.missing(:administrateur).pluck(:administrateur_id)
|
|
||||||
AdministrateursInstructeur.where(administrateur_id: deleted_administrateurs_ids).delete_all
|
|
||||||
end
|
|
||||||
|
|
||||||
say_with_time 'Removing AdministrateursInstructeur where the associated Instructeur no longer exists ' do
|
|
||||||
deleted_instructeurs_ids = AdministrateursInstructeur.where.missing(:instructeur).pluck(:instructeur_id)
|
|
||||||
AdministrateursInstructeur.where(instructeur_id: deleted_instructeurs_ids).delete_all
|
|
||||||
end
|
|
||||||
|
|
||||||
add_foreign_key :administrateurs_instructeurs, :administrateurs
|
add_foreign_key :administrateurs_instructeurs, :administrateurs
|
||||||
|
|
||||||
|
delete_orphans :administrateurs_instructeurs, :instructeurs
|
||||||
add_foreign_key :administrateurs_instructeurs, :instructeurs
|
add_foreign_key :administrateurs_instructeurs, :instructeurs
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue