diff --git a/db/migrate/20210202052337_migrate_variant_unit_values.rb b/db/migrate/20210202052337_migrate_variant_unit_values.rb index 3c63a919bb..3dd5da4805 100644 --- a/db/migrate/20210202052337_migrate_variant_unit_values.rb +++ b/db/migrate/20210202052337_migrate_variant_unit_values.rb @@ -2,15 +2,28 @@ class MigrateVariantUnitValues < ActiveRecord::Migration def up Spree::Variant.includes(:product).where( spree_products: { variant_unit: "items" }, - spree_variants: { unit_value: nil } + spree_variants: { unit_value: [nil, Float::NAN] } ).find_each do |variant| variant.unit_value = 1 variant.save end + Spree::Variant.includes(:product).where( + spree_products: { variant_unit: "items" }, + spree_variants: { 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 + 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 + 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 82647c958b..9c4b067bd4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1061,7 +1061,7 @@ ActiveRecord::Schema.define(version: 20210202052337) 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, null: false t.decimal "height", precision: 8, scale: 2 t.decimal "width", precision: 8, scale: 2 t.decimal "depth", precision: 8, scale: 2