diff --git a/app/models/spree/product.rb b/app/models/spree/product.rb index ee5cbd199e..5ef8741cc3 100755 --- a/app/models/spree/product.rb +++ b/app/models/spree/product.rb @@ -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, diff --git a/db/migrate/20230522090252_add_sku_to_product.rb b/db/migrate/20230522090252_add_sku_to_product.rb new file mode 100644 index 0000000000..d28b723ba3 --- /dev/null +++ b/db/migrate/20230522090252_add_sku_to_product.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 71537c92ce..ee71765608 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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"