Skip to content

Commit

Permalink
Update migrations for backfilling (#1188)
Browse files Browse the repository at this point in the history
  • Loading branch information
mononoken authored Nov 27, 2024
1 parent 4fc840c commit 4c2fd67
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
8 changes: 3 additions & 5 deletions db/migrate/20241122053814_add_locatable_to_locations.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions db/migrate/20241127052013_backfill_locatable_for_locations.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion db/schema.rb

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

0 comments on commit 4c2fd67

Please sign in to comment.