Skip to content

Instantly share code, notes, and snippets.

@pythonandchips
Last active February 17, 2021 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pythonandchips/988dc41557a7b2f37d1d1bce2c6f19ce to your computer and use it in GitHub Desktop.
Save pythonandchips/988dc41557a7b2f37d1d1bce2c6f19ce to your computer and use it in GitHub Desktop.
Find duplicate invoice references
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
@domcleal
Copy link

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 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment