-
Notifications
You must be signed in to change notification settings - Fork 112
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
4518fce
commit 80c1330
Showing
1 changed file
with
23 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
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,23 @@ | ||
class FixInvoicesWithIncorrectTotalPaidAmount < ActiveRecord::Migration[7.1] | ||
def up | ||
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 | ||
|
||
def down | ||
end | ||
end |