mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-06 22:36:07 +00:00
add db constraint; check for existing nil/nan in migration
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user