Last active
February 17, 2021 15:42
-
-
Save pythonandchips/988dc41557a7b2f37d1d1bce2c6f19ce to your computer and use it in GitHub Desktop.
Find duplicate invoice references
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
require "perform_on_replica" | |
def find_duplicate_references | |
PerformOnReplica.perform_on_replica do | |
companies = Company.all | |
progress_bar = ProgressBar.new(companies.count) | |
companies.includes(:subscription).find_each do |company| | |
references = company.invoices. | |
group(:reference). | |
having("count(*) > 1"). | |
count | |
if references.empty? | |
progress_bar.increment! | |
next | |
end | |
company.invoices.where(reference: references.keys).each do |invoice| | |
Rails.logger.info({ | |
event: "DUPLICATE_INVOICE_REFERENCE", | |
company_id: company.id, | |
company_subdomain: company.subdomain, | |
subscription_status: company.subscription.status, | |
subscription_channel: company.subscription.channel, | |
subscription_account_type: company.subscription.account_type, | |
invoice_reference: invoice.reference, | |
invoice_id: invoice.id, | |
invoice_dated_on: invoice.dated_on, | |
invoice_status: invoice.status, | |
created_at: invoice.created_at, | |
}) | |
end | |
progress_bar.increment! | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LGTM.
Only small comment is just to check whether there's enough info/order to the logs for you to make sense of it? I guess you'll be able to group by company ID and invoice reference in Humio, but just to note that the log lines may not be grouped by invoice reference or ordered in any way 👍