Change invoices.vat_rate to NOT NULL

#1031
This commit is contained in:
Artur Beljajev 2019-05-16 19:15:57 +03:00
parent 942ac47b00
commit a0aac379b6
4 changed files with 8 additions and 57 deletions

View file

@ -0,0 +1,5 @@
class ChangeInvoicesVatRateToNotNull < ActiveRecord::Migration
def change
change_column_null :invoices, :vat_rate, false
end
end

View file

@ -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');

View file

@ -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

View file

@ -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