From 049f95f79dabaac2ffc8a54b5460d459c107743d Mon Sep 17 00:00:00 2001 From: yulia-bel Date: Thu, 23 Nov 2023 14:47:57 +0100 Subject: [PATCH] Refactor Sustainable tourism Migration --- ...ism_and_recreation_to_sustainable_tourism.rb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/backend/db/migrate/20231123093623_change_tourism_and_recreation_to_sustainable_tourism.rb b/backend/db/migrate/20231123093623_change_tourism_and_recreation_to_sustainable_tourism.rb index e7f299b63..62f6bf073 100644 --- a/backend/db/migrate/20231123093623_change_tourism_and_recreation_to_sustainable_tourism.rb +++ b/backend/db/migrate/20231123093623_change_tourism_and_recreation_to_sustainable_tourism.rb @@ -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 @@ -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