Merge pull request #6792 from andrewpbrett/migrate-variant-unit-values

Migrate variants with nil unit_value
This commit is contained in:
Andy Brett
2021-02-04 18:00:23 -08:00
committed by GitHub
3 changed files with 31 additions and 3 deletions

View File

@@ -0,0 +1,27 @@
class MigrateVariantUnitValues < ActiveRecord::Migration
def up
Spree::Variant.where(unit_value: [nil, Float::NAN]).find_each do |variant|
variant.unit_value = 1
variant.save
end
Spree::Variant.where(weight: [nil, Float::NAN]).find_each do |variant|
variant.weight = 0
variant.save
end
change_column_null :spree_variants, :unit_value, false, 1
change_column_null :spree_variants, :weight, false, 0.0
change_column_default :spree_variants, :unit_value, 1
change_column_default :spree_variants, :weight, 0.0
execute "ALTER TABLE spree_variants ADD CONSTRAINT check_unit_value_for_nan CHECK (unit_value <> 'NaN')"
execute "ALTER TABLE spree_variants ADD CONSTRAINT check_weight_for_nan CHECK (weight <> 'NaN')"
end
def down
change_column_null :spree_variants, :unit_value, true
change_column_null :spree_variants, :weight, true
change_column_default :spree_variants, :unit_value, nil
change_column_default :spree_variants, :weight, nil
execute "ALTER TABLE spree_variants DROP CONSTRAINT check_unit_value_for_nan"
execute "ALTER TABLE spree_variants DROP CONSTRAINT check_weight_for_nan"
end
end

View File

@@ -11,6 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20210203215049) do
# These are extensions that must be enabled in order to support this database
@@ -1061,7 +1062,7 @@ ActiveRecord::Schema.define(version: 20210203215049) do
create_table "spree_variants", force: :cascade do |t|
t.string "sku", limit: 255, default: "", null: false
t.decimal "weight", precision: 8, scale: 2
t.decimal "weight", precision: 8, scale: 2, default: 0.0, null: false
t.decimal "height", precision: 8, scale: 2
t.decimal "width", precision: 8, scale: 2
t.decimal "depth", precision: 8, scale: 2
@@ -1071,7 +1072,7 @@ ActiveRecord::Schema.define(version: 20210203215049) do
t.decimal "cost_price", precision: 8, scale: 2
t.integer "position"
t.string "cost_currency", limit: 255
t.float "unit_value"
t.float "unit_value", default: 1.0, null: false
t.string "unit_description", limit: 255, default: ""
t.string "display_name", limit: 255
t.string "display_as", limit: 255

View File

@@ -637,7 +637,7 @@ module Spree
describe "setting the variant's weight from the unit value" do
it "sets the variant's weight when unit is weight" do
p = create(:simple_product, variant_unit: 'volume')
v = create(:variant, product: p, weight: nil)
v = create(:variant, product: p, weight: 0)
p.update! variant_unit: 'weight', variant_unit_scale: 1
v.update! unit_value: 10, unit_description: 'foo'