-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
misc(fee): Delete zero amount finalized fees
- Loading branch information
1 parent
5437092
commit 5692302
Showing
2 changed files
with
42 additions
and
1 deletion.
There are no files selected for viewing
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,41 @@ | ||
# frozen_string_literal: true | ||
|
||
class DropZeroAmountFees < ActiveRecord::Migration[7.1] | ||
disable_ddl_transaction! | ||
|
||
class FeesTax < ApplicationRecord; end | ||
|
||
class Fee < ApplicationRecord; end | ||
|
||
def change | ||
sql = <<~SQL | ||
SELECT fees.id FROM fees | ||
INNER JOIN invoices ON fees.invoice_id = invoices.id | ||
WHERE | ||
invoices.status IN (1, 2, 6) -- finalized, voided and closed | ||
AND fees.fee_type = 0 -- charge | ||
AND fees.amount_cents = 0 | ||
AND fees.units = 0 | ||
AND fees.pay_in_advance = false | ||
AND fees.true_up_parent_fee_id IS NULL | ||
AND fees.id NOT IN ( | ||
SELECT f.true_up_parent_fee_id | ||
FROM fees f | ||
WHERE f.true_up_parent_fee_id IS NOT NULL | ||
) | ||
AND fees.id NOT IN ( | ||
SELECT fee_id | ||
FROM adjusted_fees | ||
WHERE adjusted_fees.fee_id IS NOT NULL | ||
) | ||
LIMIT 1000 | ||
SQL | ||
|
||
while (ids = ActiveRecord::Base.connection.select_all(sql).rows.map(&:first)).any? | ||
FeesTax.where(fee_id: ids).delete_all | ||
Fee.where(id: ids).delete_all | ||
|
||
puts "Deleted #{ids.size} fees - #{Time.current.iso8601}" # rubocop:disable Rails/Output | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.