-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f2fe1b
commit 8d1fd60
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
db/migrate/20250219205535_fix_invoices_with_incorrect_total_paid_amount.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
class FixInvoicesWithIncorrectTotalPaidAmount < ActiveRecord::Migration[7.1] | ||
def up | ||
safety_assured do | ||
execute <<~SQL | ||
UPDATE invoices | ||
SET total_paid_amount_cents = 0 | ||
WHERE id IN ( | ||
SELECT invoices.id | ||
FROM invoices | ||
LEFT JOIN payments ON invoices.id = payments.invoice_id | ||
LEFT JOIN invoices_payment_requests ON invoices.id = invoices_payment_requests.invoice_id | ||
WHERE payments.id IS NULL | ||
AND invoices_payment_requests.id IS NULL | ||
AND total_amount_cents > 0 | ||
AND invoice_type <> 4 | ||
AND total_amount_cents = total_paid_amount_cents | ||
AND payment_status = 1 | ||
); | ||
SQL | ||
end | ||
end | ||
|
||
def down | ||
end | ||
end |