diff --git a/prisma/migrations/20250513212337_delete_duplicate_wallets/migration.sql b/prisma/migrations/20250513212337_delete_duplicate_wallets/migration.sql new file mode 100644 index 00000000..546e9d40 --- /dev/null +++ b/prisma/migrations/20250513212337_delete_duplicate_wallets/migration.sql @@ -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"); diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 49c4b858..cb92eab8 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -246,6 +246,7 @@ model Wallet { InvoiceForward InvoiceForward[] DirectPayment DirectPayment[] + @@unique([userId, type]) @@index([userId]) @@index([priority]) }