Skip to content

Commit

Permalink
misc(fee): Delete zero amount finalized fees
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet committed Jan 9, 2025
1 parent 5437092 commit 5692302
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
41 changes: 41 additions & 0 deletions db/migrate/20250103124802_drop_zero_amount_fees.rb
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
2 changes: 1 addition & 1 deletion db/schema.rb

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

0 comments on commit 5692302

Please sign in to comment.