Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clean duplicate payments #2995

Merged
merged 3 commits into from
Dec 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions db/migrate/20241220095049_backfill_payable_payment_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

class BackfillPayablePaymentStatus < ActiveRecord::Migration[7.1]
def change
# clean all duplicate data
# Find `payable_id`s with duplicate "processing" payments
duplicate_payables = Payment.where(status: 'processing')
.group(:payable_id)
.having('COUNT(id) > 1')
.pluck(:payable_id)

# clean the duplicates
duplicate_payables.each do |payable_id|
# Find the most recent "processing"
latest_pending_payment = Payment.where(status: 'processing', payable_id: payable_id)
.order(created_at: :desc)
.first

# Update all other "processing" payments for this `payable_id` to "failed"
Payment.where(status: 'processing', payable_id: payable_id)
.where.not(id: latest_pending_payment.id)
.update_all(status: 'failed') # rubocop:disable Rails/SkipsModelValidations
end

provider_types = PaymentProviders::BaseProvider.distinct.pluck(:type)
provider_types.each do |provider_type|
provider_class = provider_type.constantize
Expand Down
Loading