From 21bf5797acf3d4fbc5cef121f0d61b5e49337e1a Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 3 Nov 2021 10:56:15 +0000 Subject: [PATCH] Improve data migration --- .../20211027140313_migrate_customer_names.rb | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/db/migrate/20211027140313_migrate_customer_names.rb b/db/migrate/20211027140313_migrate_customer_names.rb index 1b1a949c24..c361720dfd 100644 --- a/db/migrate/20211027140313_migrate_customer_names.rb +++ b/db/migrate/20211027140313_migrate_customer_names.rb @@ -1,12 +1,14 @@ class MigrateCustomerNames < ActiveRecord::Migration[6.1] - class ApplicationRecord < ActiveRecord::Base - self.abstract_class = true - end - class Enterprise < ApplicationRecord; end - module Spree - class Preference < ApplicationRecord - self.table_name = "spree_preferences" - serialize :value + class Enterprise < ActiveRecord::Base + scope :showing_customer_names, -> do + joins( + <<-SQL + JOIN spree_preferences ON ( + value LIKE '--- true\n%' + AND spree_preferences.key = CONCAT('/enterprise/show_customer_names_to_suppliers/', enterprises.id) + ) + SQL + ) end end @@ -15,14 +17,6 @@ class MigrateCustomerNames < ActiveRecord::Migration[6.1] end def migrate_customer_names_preferences! - Enterprise.where(sells: ["own", "any"]).find_each do |enterprise| - next unless Spree::Preference.where( - value: true, key: "/enterprise/show_customer_names_to_suppliers/#{enterprise.id}" - ).exists? - - enterprise.update_columns( - show_customer_names_to_suppliers: true - ) - end + Enterprise.showing_customer_names.update_all(show_customer_names_to_suppliers: true) end end