diff --git a/core/app/models/spree/price.rb b/core/app/models/spree/price.rb index defe76c780..59cf304d51 100644 --- a/core/app/models/spree/price.rb +++ b/core/app/models/spree/price.rb @@ -13,7 +13,7 @@ class Price < Spree::Base delegate :tax_rates, to: :variant validate :check_price - validates :amount, allow_nil: true, numericality: { + validates :amount, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: MAXIMUM_AMOUNT } diff --git a/core/db/migrate/20210312061050_change_column_null_on_prices.rb b/core/db/migrate/20210312061050_change_column_null_on_prices.rb new file mode 100644 index 0000000000..19c88ce3a2 --- /dev/null +++ b/core/db/migrate/20210312061050_change_column_null_on_prices.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class ChangeColumnNullOnPrices < ActiveRecord::Migration[5.2] + def change + change_column_null(:spree_prices, :amount, false) + end +end diff --git a/core/lib/tasks/migrations/delete_prices_with_nil_amount.rake b/core/lib/tasks/migrations/delete_prices_with_nil_amount.rake new file mode 100644 index 0000000000..96cbaafda3 --- /dev/null +++ b/core/lib/tasks/migrations/delete_prices_with_nil_amount.rake @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +namespace :solidus do + namespace :migrations do + namespace :delete_prices_with_nil_amount do + task up: :environment do + print "Deleting prices wich amount attribute is nil ... " + Spree::Price.where(amount: nil).delete_all + puts "Success" + end + end + end +end diff --git a/core/lib/tasks/upgrade.rake b/core/lib/tasks/upgrade.rake index d57aba14e4..cb10b9cf31 100644 --- a/core/lib/tasks/upgrade.rake +++ b/core/lib/tasks/upgrade.rake @@ -9,5 +9,12 @@ namespace :solidus do ] do puts "Your Solidus install is ready for Solidus 2.11" end + + desc "Upgrade Solidus to version 3.0" + task three_point_zero: [ + 'solidus:migrations:delete_prices_with_nil_amount:up', + ] do + puts "Your Solidus install is ready for Solidus 3.0" + end end end diff --git a/core/spec/lib/tasks/migrations/delete_prices_with_nil_amount_spec.rb b/core/spec/lib/tasks/migrations/delete_prices_with_nil_amount_spec.rb new file mode 100644 index 0000000000..331fab94b8 --- /dev/null +++ b/core/spec/lib/tasks/migrations/delete_prices_with_nil_amount_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'rails_helper' + +path = Spree::Core::Engine.root.join('lib/tasks/migrations/delete_prices_with_nil_amount.rake') + +RSpec.describe 'solidus:migrations:delete_prices_with_nil_amount' do + describe 'up' do + include_context( + 'rake', + task_path: path, + task_name: 'solidus:migrations:delete_prices_with_nil_amount:up' + ) + + it 'removes all prices which amount column is NULL' do + price = create(:price) + expect(Spree::Price).to receive(:where).with(amount: nil).and_return(Spree::Price.where(id: price)) + + task.invoke + + expect { price.reload }.to raise_error(ActiveRecord::RecordNotFound) + end + end +end diff --git a/core/spec/models/spree/price_spec.rb b/core/spec/models/spree/price_spec.rb index f9e76f2612..c285a28c13 100644 --- a/core/spec/models/spree/price_spec.rb +++ b/core/spec/models/spree/price_spec.rb @@ -16,7 +16,7 @@ context 'when the amount is nil' do let(:amount) { nil } - it { is_expected.to be_valid } + it { is_expected.not_to be_valid } end context 'when the amount is less than 0' do