From 94cde5fb0918017d890679fa2623c74b38a6cbc1 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Mon, 23 Dec 2024 10:00:03 +0000 Subject: [PATCH 1/3] fix: clean duplicate data --- ...0095049_backfill_payable_payment_status.rb | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/db/migrate/20241220095049_backfill_payable_payment_status.rb b/db/migrate/20241220095049_backfill_payable_payment_status.rb index 7dc75faf992..e06eb2fe0c5 100644 --- a/db/migrate/20241220095049_backfill_payable_payment_status.rb +++ b/db/migrate/20241220095049_backfill_payable_payment_status.rb @@ -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 "pending" + latest_pending_payment = Payment.where(status: 'processing', payable_id: payable_id) + .order(created_at: :desc) + .first + + # Update all other "pending" 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') + end + provider_types = PaymentProviders::BaseProvider.distinct.pluck(:type) provider_types.each do |provider_type| provider_class = provider_type.constantize From 674dacf05c56f166c23b8d0b6157880e8a3ffb1c Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Mon, 23 Dec 2024 10:01:16 +0000 Subject: [PATCH 2/3] fix: clean duplicate data --- db/migrate/20241220095049_backfill_payable_payment_status.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db/migrate/20241220095049_backfill_payable_payment_status.rb b/db/migrate/20241220095049_backfill_payable_payment_status.rb index e06eb2fe0c5..bab043c2797 100644 --- a/db/migrate/20241220095049_backfill_payable_payment_status.rb +++ b/db/migrate/20241220095049_backfill_payable_payment_status.rb @@ -11,12 +11,12 @@ def change # clean the duplicates duplicate_payables.each do |payable_id| - # Find the most recent "pending" + # Find the most recent "processing" latest_pending_payment = Payment.where(status: 'processing', payable_id: payable_id) .order(created_at: :desc) .first - # Update all other "pending" payments for this `payable_id` to "failed" + # 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') From 0175aa882b3fa5dc5ea3b39146339428277e443c Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Mon, 23 Dec 2024 10:04:52 +0000 Subject: [PATCH 3/3] fix: fix rubocop --- db/migrate/20241220095049_backfill_payable_payment_status.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/migrate/20241220095049_backfill_payable_payment_status.rb b/db/migrate/20241220095049_backfill_payable_payment_status.rb index bab043c2797..00c9264d893 100644 --- a/db/migrate/20241220095049_backfill_payable_payment_status.rb +++ b/db/migrate/20241220095049_backfill_payable_payment_status.rb @@ -19,7 +19,7 @@ def change # 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') + .update_all(status: 'failed') # rubocop:disable Rails/SkipsModelValidations end provider_types = PaymentProviders::BaseProvider.distinct.pluck(:type)