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.
This commit is contained in:
Matt-Yorkley
2023-06-22 13:10:47 +01:00
parent f9185ea56e
commit c88799618f

View File

@@ -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