Delete duplicate wallets (#2163)

* Delete duplicate wallets

* Add unique Wallet index to prevent duplicate user wallets
This commit is contained in:
ekzyis 2025-05-14 19:49:04 -05:00 committed by GitHub
parent d2c71ca08f
commit d7ddfffbf0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 0 deletions

View File

@ -0,0 +1,16 @@
-- delete duplicate wallets per user
WITH duplicates AS (
SELECT
"userId",
"type",
(array_agg(id ORDER BY updated_at DESC))[2:] AS id
FROM "Wallet"
GROUP BY "userId", "type"
HAVING COUNT(id) > 1
ORDER BY COUNT(id) DESC, "userId" ASC
)
DELETE FROM "Wallet"
WHERE id in (SELECT unnest(id) FROM duplicates);
-- CreateIndex
CREATE UNIQUE INDEX "Wallet_userId_type_key" ON "Wallet"("userId", "type");

View File

@ -246,6 +246,7 @@ model Wallet {
InvoiceForward InvoiceForward[]
DirectPayment DirectPayment[]
@@unique([userId, type])
@@index([userId])
@@index([priority])
}