Migrate master variant :sku to product

(cherry picked from commit d8649fc9fb)
This commit is contained in:
Matt-Yorkley
2023-05-22 10:17:49 +01:00
committed by David Cook
parent 7ef9c2f56a
commit 2f2506e698
3 changed files with 26 additions and 1 deletions

View File

@@ -75,7 +75,7 @@ module Spree
)
}
delegate_belongs_to :master, :images, :sku
delegate_belongs_to :master, :images
delegate :images_attributes=, to: :master
# Transient attributes used temporarily when creating a new product,

View File

@@ -0,0 +1,24 @@
class AddSkuToProduct < ActiveRecord::Migration[7.0]
def up
add_column :spree_products, :sku, :string, limit: 255, default: "", null: false
migrate_master_sku
end
def down
remove_column :spree_products, :sku
end
private
def migrate_master_sku
ActiveRecord::Base.connection.execute(<<-SQL
UPDATE spree_products
SET sku = spree_variants.sku
FROM spree_variants
WHERE spree_variants.product_id = spree_products.id
AND spree_variants.is_master = true
SQL
)
end
end

View File

@@ -745,6 +745,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_05_22_120633) do
t.text "notes"
t.integer "primary_taxon_id", null: false
t.boolean "inherits_properties", default: true, null: false
t.string "sku", limit: 255, default: "", null: false
t.index ["available_on"], name: "index_products_on_available_on"
t.index ["deleted_at"], name: "index_products_on_deleted_at"
t.index ["name"], name: "index_products_on_name"