From 4c2fd6797caff0d2992166e7159da3e5d855614b Mon Sep 17 00:00:00 2001 From: Ken Maeshima <81536479+mononoken@users.noreply.github.com> Date: Tue, 26 Nov 2024 22:59:35 -0800 Subject: [PATCH] Update migrations for backfilling (#1188) --- ...241122053814_add_locatable_to_locations.rb | 8 +++---- ...052013_backfill_locatable_for_locations.rb | 22 +++++++++++++++++++ ...2542_remove_organization_from_locations.rb | 7 ++++++ db/schema.rb | 2 +- 4 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20241127052013_backfill_locatable_for_locations.rb create mode 100644 db/migrate/20241127052542_remove_organization_from_locations.rb diff --git a/db/migrate/20241122053814_add_locatable_to_locations.rb b/db/migrate/20241122053814_add_locatable_to_locations.rb index 169fb8934..6e0af092d 100644 --- a/db/migrate/20241122053814_add_locatable_to_locations.rb +++ b/db/migrate/20241122053814_add_locatable_to_locations.rb @@ -1,9 +1,7 @@ class AddLocatableToLocations < ActiveRecord::Migration[7.2] - def change - safety_assured do - remove_reference :locations, :organization, foreign_key: true + disable_ddl_transaction! - add_reference :locations, :locatable, polymorphic: true, null: false, index: true - end + def change + add_reference :locations, :locatable, polymorphic: true, null: true, index: {algorithm: :concurrently} end end diff --git a/db/migrate/20241127052013_backfill_locatable_for_locations.rb b/db/migrate/20241127052013_backfill_locatable_for_locations.rb new file mode 100644 index 000000000..623d75d37 --- /dev/null +++ b/db/migrate/20241127052013_backfill_locatable_for_locations.rb @@ -0,0 +1,22 @@ +class BackfillLocatableForLocations < ActiveRecord::Migration[7.2] + def up + # All locatables were previously organizations + Location.find_each do |location| + location.update!( + locatable_type: "Organization", + locatable_id: location.organization_id + ) + end + + safety_assured do + change_column_null :locations, :locatable_id, false + change_column_null :locations, :locatable_type, false + end + end + + def down + Location.find_each do |location| + location.update!(organization_id: location.locatable_id) + end + end +end diff --git a/db/migrate/20241127052542_remove_organization_from_locations.rb b/db/migrate/20241127052542_remove_organization_from_locations.rb new file mode 100644 index 000000000..275addf81 --- /dev/null +++ b/db/migrate/20241127052542_remove_organization_from_locations.rb @@ -0,0 +1,7 @@ +class RemoveOrganizationFromLocations < ActiveRecord::Migration[7.2] + def change + safety_assured do + remove_reference :locations, :organization, foreign_key: true + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 5b801f497..6122777e9 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[7.2].define(version: 2024_11_22_053814) do +ActiveRecord::Schema[7.2].define(version: 2024_11_27_052542) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql"