From a0aac379b64ac24717d10365e766740ff71d809e Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Thu, 16 May 2019 19:15:57 +0300 Subject: [PATCH] Change `invoices.vat_rate` to `NOT NULL` #1031 --- ...39_change_invoices_vat_rate_to_not_null.rb | 5 +++ db/structure.sql | 4 +- .../populate_invoice_vat_rate.rake | 16 -------- .../populate_invoice_vat_rate_test.rb | 40 ------------------- 4 files changed, 8 insertions(+), 57 deletions(-) create mode 100644 db/migrate/20190516161439_change_invoices_vat_rate_to_not_null.rb delete mode 100644 lib/tasks/data_migrations/populate_invoice_vat_rate.rake delete mode 100644 test/tasks/data_migrations/populate_invoice_vat_rate_test.rb diff --git a/db/migrate/20190516161439_change_invoices_vat_rate_to_not_null.rb b/db/migrate/20190516161439_change_invoices_vat_rate_to_not_null.rb new file mode 100644 index 000000000..e47a2d2a4 --- /dev/null +++ b/db/migrate/20190516161439_change_invoices_vat_rate_to_not_null.rb @@ -0,0 +1,5 @@ +class ChangeInvoicesVatRateToNotNull < ActiveRecord::Migration + def change + change_column_null :invoices, :vat_rate, false + end +end diff --git a/db/structure.sql b/db/structure.sql index 6fd2efa2e..629aec4a8 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1047,7 +1047,7 @@ CREATE TABLE public.invoices ( currency character varying NOT NULL, description character varying, reference_no character varying NOT NULL, - vat_rate numeric(4,3), + vat_rate numeric(4,3) NOT NULL, seller_id integer, seller_name character varying NOT NULL, seller_reg_no character varying, @@ -4969,5 +4969,7 @@ INSERT INTO schema_migrations (version) VALUES ('20190510102549'); INSERT INTO schema_migrations (version) VALUES ('20190515113153'); +INSERT INTO schema_migrations (version) VALUES ('20190516161439'); + INSERT INTO schema_migrations (version) VALUES ('20190520093231'); diff --git a/lib/tasks/data_migrations/populate_invoice_vat_rate.rake b/lib/tasks/data_migrations/populate_invoice_vat_rate.rake deleted file mode 100644 index 3ae4a6a14..000000000 --- a/lib/tasks/data_migrations/populate_invoice_vat_rate.rake +++ /dev/null @@ -1,16 +0,0 @@ -namespace :data_migrations do - task populate_invoice_vat_rate: :environment do - processed_invoice_count = 0 - - Invoice.transaction do - Invoice.where(vat_rate: nil).find_each do |invoice| - vat_rate = Invoice::VatRateCalculator.new(registrar: invoice.buyer).calculate - invoice.update!(vat_rate: vat_rate) - - processed_invoice_count += 1 - end - end - - puts "Invoices processed: #{processed_invoice_count}" - end -end \ No newline at end of file diff --git a/test/tasks/data_migrations/populate_invoice_vat_rate_test.rb b/test/tasks/data_migrations/populate_invoice_vat_rate_test.rb deleted file mode 100644 index e587117d8..000000000 --- a/test/tasks/data_migrations/populate_invoice_vat_rate_test.rb +++ /dev/null @@ -1,40 +0,0 @@ -require 'test_helper' - -class PopulateInvoiceVatRateTaskTest < ActiveSupport::TestCase - def test_populates_invoice_issue_date - invoice = invoice_without_vat_rate - - capture_io do - run_task - end - invoice.reload - - assert_not_nil invoice.vat_rate - end - - def test_output - eliminate_effect_of_all_invoices_except(invoice_without_vat_rate) - - assert_output "Invoices processed: 1\n" do - run_task - end - end - - private - - def invoice_without_vat_rate - invoice = invoices(:one) - invoice.update_columns(vat_rate: nil) - invoice - end - - def eliminate_effect_of_all_invoices_except(invoice) - Invoice.connection.disable_referential_integrity do - Invoice.delete_all("id != #{invoice.id}") - end - end - - def run_task - Rake::Task['data_migrations:populate_invoice_vat_rate'].execute - end -end \ No newline at end of file