Skip to content

Commit

Permalink
Refactor Sustainable tourism Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
yulia-bel committed Nov 23, 2023
1 parent 4031ec6 commit 049f95f
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
class ChangeTourismAndRecreationToSustainableTourism < ActiveRecord::Migration[7.0]
def up
Project.where(category: "tourism-and-recreation").update_all(category: "sustainable-tourism")

[Investor, ProjectDeveloper].each do |model|
model.find_each do |record|
model.where("categories @> ?", "{tourism-and-recreation}").find_each do |record|
new_categories = record.categories.map do |category|
category == "tourism-and-recreation" ? "sustainable-tourism" : category
end
record.update(categories: new_categories)

data = record.attributes
data['categories'] = new_categories

model.upsert_all([data], unique_by: :id)
end
end
end
Expand All @@ -15,11 +20,15 @@ def down
Project.where(category: "sustainable-tourism").update_all(category: "tourism-and-recreation")

[Investor, ProjectDeveloper].each do |model|
model.find_each do |record|
model.where("categories @> ?", "{sustainable-tourism}").find_each do |record|
new_categories = record.categories.map do |category|
category == "sustainable-tourism" ? "tourism-and-recreation" : category
end
record.update(categories: new_categories)

data = record.attributes
data['categories'] = new_categories

model.upsert_all([data], unique_by: :id)
end
end
end
Expand Down

0 comments on commit 049f95f

Please sign in to comment.