-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update migrations for backfilling (#1188)
- Loading branch information
Showing
4 changed files
with
33 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
db/migrate/20241127052013_backfill_locatable_for_locations.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
7 changes: 7 additions & 0 deletions
7
db/migrate/20241127052542_remove_organization_from_locations.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.