Skip to content

Commit

Permalink
Improve Migration for Sustainable tourism update
Browse files Browse the repository at this point in the history
  • Loading branch information
yulia-bel committed Nov 23, 2023
1 parent 049f95f commit 26b90f1
Showing 1 changed file with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@ def up
Project.where(category: "tourism-and-recreation").update_all(category: "sustainable-tourism")

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

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

model.upsert_all([data], unique_by: :id)
record.attributes.merge(categories: new_categories)
end

model.upsert_all(records_to_upsert, unique_by: :id) unless records_to_upsert.empty?
end
end

def down
Project.where(category: "sustainable-tourism").update_all(category: "tourism-and-recreation")

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

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

model.upsert_all([data], unique_by: :id)
record.attributes.merge(categories: new_categories)
end

model.upsert_all(records_to_upsert, unique_by: :id) unless records_to_upsert.empty?
end
end
end

0 comments on commit 26b90f1

Please sign in to comment.