From c88799618f919ddc834735dc7937a9ca77afb65d Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 22 Jun 2023 13:10:47 +0100 Subject: [PATCH] Set master variants which are associated to line items to non-master Line items which reference a master variant is a scenario that in theory shouldn't have been valid or even possible for at least 5-6 years, and these old bits of data in theory should have been cleaned up at the time those changes were made. But a couple of servers have some really old data that's not in a nice state. Here we can just flip the is_master flag to false for those specific (legacy data) cases before deleting any other master variants, to keep the legacy line item data intact. --- db/migrate/20230605133804_remove_master_variants.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/db/migrate/20230605133804_remove_master_variants.rb b/db/migrate/20230605133804_remove_master_variants.rb index b5b88d4d26..e2edd7b25f 100644 --- a/db/migrate/20230605133804_remove_master_variants.rb +++ b/db/migrate/20230605133804_remove_master_variants.rb @@ -4,7 +4,7 @@ class RemoveMasterVariants < ActiveRecord::Migration[7.0] delete_master_option_values end - delete_master_line_items + handle_master_line_items delete_master_inventory_units delete_master_variant_prices delete_master_variants @@ -12,10 +12,11 @@ class RemoveMasterVariants < ActiveRecord::Migration[7.0] private - def delete_master_line_items + def handle_master_line_items ActiveRecord::Base.connection.execute(<<-SQL - DELETE FROM spree_line_items - USING spree_variants + UPDATE spree_variants + SET is_master = false + FROM spree_line_items WHERE spree_variants.is_master = true AND spree_variants.id = spree_line_items.variant_id SQL