Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add payment type and reference #3003

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ def status_changed_to_finalized?
# taxes_rate :float default(0.0), not null
# timezone :string default("UTC"), not null
# total_amount_cents :bigint default(0), not null
# total_paid_amount_cents :bigint default(0), not null
# version_number :integer default(4), not null
# voided_at :datetime
# created_at :datetime not null
Expand Down
2 changes: 2 additions & 0 deletions app/models/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def should_sync_payment?
# amount_currency :string not null
# payable_payment_status :enum
# payable_type :string default("Invoice"), not null
# payment_type :enum default("provider"), not null
# provider_payment_data :jsonb
# reference :string
# status :string not null
# created_at :datetime not null
# updated_at :datetime not null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

class AddPaymentTypeAndReferenceToPayments < ActiveRecord::Migration[7.1]
disable_ddl_transaction!

def up
create_enum :payment_type, %w[provider manual]

safety_assured do
change_table :payments, bulk: true do |t|
t.column :payment_type, :enum, enum_type: 'payment_type', null: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will we need index on this column?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes good catch I will add it

t.column :reference, :string, default: nil
end
end

# Backfill existing records
Payment.in_batches(of: 10_000).update_all(payment_type: 'provider') # rubocop:disable Rails/SkipsModelValidations

safety_assured do
execute <<~SQL
ALTER TABLE payments ALTER COLUMN payment_type SET DEFAULT 'provider';
SQL
execute <<~SQL
ALTER TABLE payments ALTER COLUMN payment_type SET NOT NULL;
SQL
end
end

def down
change_table :payments, bulk: true do |t|
t.remove :payment_type
t.remove :reference
end

drop_enum :payment_type
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

class AddTotalPaidAmountCentsToInvoices < ActiveRecord::Migration[7.1]
disable_ddl_transaction!

def up
add_column :invoices, :total_paid_amount_cents, :bigint, null: true, default: 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will in the future we update the existing paid invoices with real values? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I will do a migration to fill the values i was thinking on another pr but can be on this one

# Backfill
Invoice.in_batches(of: 10_000).each do |batch|
batch.update_all(total_paid_amount_cents: 0) # rubocop:disable Rails/SkipsModelValidations
end

safety_assured do
execute <<~SQL
ALTER TABLE invoices ALTER COLUMN total_paid_amount_cents SET DEFAULT 0;
SQL
execute <<~SQL
ALTER TABLE invoices ALTER COLUMN total_paid_amount_cents SET NOT NULL;
SQL
end
end

def down
# Remove the column
remove_column :invoices, :total_paid_amount_cents
end
end
6 changes: 5 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading