Skip to content

Commit 9c2c4a4

Browse files
committed
feat(payment_terms): Backfill and nullable column migrations
1 parent 3aaadf1 commit 9c2c4a4

8 files changed

Lines changed: 44 additions & 14 deletions

app/models/billing_entity.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class BillingEntity < ApplicationRecord
9696
validates :document_number_prefix, length: {minimum: 1, maximum: 10}, allow_nil: true, on: :create
9797
validates :document_number_prefix, length: {minimum: 1, maximum: 10}, on: :update
9898
validates :invoice_grace_period, numericality: {greater_than_or_equal_to: 0}
99-
validates :net_payment_term, numericality: {greater_than_or_equal_to: 0}
99+
validates :net_payment_term, numericality: {greater_than_or_equal_to: 0}, allow_nil: true
100100
validates :logo,
101101
image: {authorized_content_type: %w[image/png image/jpg image/jpeg], max_size: 800.kilobytes},
102102
if: :logo?
@@ -209,7 +209,7 @@ def validate_einvoicing
209209
# legal_number :string
210210
# logo :string
211211
# name :string not null
212-
# net_payment_term :integer default(0), not null
212+
# net_payment_term :integer default(0)
213213
# payment_term :jsonb
214214
# phone :string
215215
# state :string

app/models/invoice.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ def set_finalized_at
768768
# finalized_at :datetime
769769
# invoice_type :integer default("subscription"), not null
770770
# issuing_date :date
771-
# net_payment_term :integer default(0), not null
771+
# net_payment_term :integer default(0)
772772
# number :string default(""), not null
773773
# payment_attempts :integer default(0), not null
774774
# payment_dispute_lost_at :datetime
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
class BackfillPaymentTerms < ActiveRecord::Migration[8.0]
4+
def up
5+
safety_assured do
6+
execute <<~SQL
7+
UPDATE customers
8+
SET payment_term = jsonb_build_object('term_type', 'net', 'days', net_payment_term)
9+
WHERE payment_term IS NULL AND net_payment_term IS NOT NULL
10+
SQL
11+
12+
execute <<~SQL
13+
UPDATE billing_entities
14+
SET payment_term = jsonb_build_object('term_type', 'net', 'days', net_payment_term)
15+
WHERE payment_term IS NULL AND net_payment_term IS NOT NULL
16+
SQL
17+
end
18+
end
19+
20+
def down
21+
raise ActiveRecord::IrreversibleMigration, "backfilled payment terms cannot be distinguished from user values"
22+
end
23+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
class RelaxNetPaymentTermNullability < ActiveRecord::Migration[8.0]
4+
def change
5+
change_column_null :billing_entities, :net_payment_term, true
6+
change_column_null :invoices, :net_payment_term, true
7+
end
8+
end

db/structure.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2269,7 +2269,7 @@ CREATE TABLE public.billing_entities (
22692269
finalize_zero_amount_invoice boolean DEFAULT true NOT NULL,
22702270
invoice_footer text,
22712271
invoice_grace_period integer DEFAULT 0 NOT NULL,
2272-
net_payment_term integer DEFAULT 0 NOT NULL,
2272+
net_payment_term integer DEFAULT 0,
22732273
email character varying,
22742274
email_settings character varying[] DEFAULT '{}'::character varying[] NOT NULL,
22752275
eu_tax_management boolean DEFAULT false,
@@ -3609,7 +3609,7 @@ CREATE TABLE public.invoices (
36093609
sub_total_excluding_taxes_amount_cents bigint DEFAULT 0 NOT NULL,
36103610
sub_total_including_taxes_amount_cents bigint DEFAULT 0 NOT NULL,
36113611
payment_due_date date,
3612-
net_payment_term integer DEFAULT 0 NOT NULL,
3612+
net_payment_term integer DEFAULT 0,
36133613
voided_at timestamp(6) without time zone,
36143614
organization_sequential_id integer DEFAULT 0 NOT NULL,
36153615
ready_to_be_refreshed boolean DEFAULT false NOT NULL,
@@ -14176,6 +14176,8 @@ ALTER TABLE ONLY public.membership_roles
1417614176
SET search_path TO "$user", public;
1417714177

1417814178
INSERT INTO "schema_migrations" (version) VALUES
14179+
('20260813134550'),
14180+
('20260813134021'),
1417914181
('20260810135202'),
1418014182
('20260805201143'),
1418114183
('20260805110509'),
@@ -15277,4 +15279,3 @@ INSERT INTO "schema_migrations" (version) VALUES
1527715279
('20220530091046'),
1527815280
('20220526101535'),
1527915281
('20220525122759');
15280-

schema.graphql

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

schema.json

Lines changed: 3 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/models/billing_entity_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@
8686
expect(billing_entity).to be_valid
8787
end
8888

89+
it { is_expected.to allow_value(nil).for(:net_payment_term) }
90+
8991
it { is_expected.to validate_length_of(:document_number_prefix).is_at_least(1).is_at_most(10).on(:update) }
9092

9193
it { is_expected.to allow_value(nil).for(:document_number_prefix).on(:create) }

0 commit comments

Comments
 (0)