diff --git a/db/migrate/20210202052337_migrate_variant_unit_values.rb b/db/migrate/20210202052337_migrate_variant_unit_values.rb new file mode 100644 index 0000000000..9c6cf46a2e --- /dev/null +++ b/db/migrate/20210202052337_migrate_variant_unit_values.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index a5b4c1c42b..aa00312da6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/spec/models/spree/variant_spec.rb b/spec/models/spree/variant_spec.rb index 5ef2d0637f..cfd0d41d8b 100644 --- a/spec/models/spree/variant_spec.rb +++ b/spec/models/spree/variant_spec.rb @@ -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'