From ccbde0d32229e17749cf55ee4ecc81250146309f Mon Sep 17 00:00:00 2001 From: ekzyis Date: Thu, 9 Jan 2025 01:37:09 +0100 Subject: [PATCH] Fix no index used --- api/resolvers/wallet.js | 18 ++++++------------ .../migration.sql | 1 + prisma/schema.prisma | 1 + 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/api/resolvers/wallet.js b/api/resolvers/wallet.js index 0eb36dfd4..e60d2e00f 100644 --- a/api/resolvers/wallet.js +++ b/api/resolvers/wallet.js @@ -467,21 +467,19 @@ const resolvers = { // make sure each invoice is only returned once via visibility timeouts and SKIP LOCKED // we use queryRawUnsafe because Prisma does not support tsrange // see https://www.prisma.io/docs/orm/overview/databases/postgresql - return await models.$queryRawUnsafe(` + return await models.$queryRaw` WITH failed AS ( UPDATE "Invoice" SET "lockedAt" = now() WHERE id IN ( SELECT id FROM "Invoice" - WHERE "userId" = $1 + WHERE "userId" = ${me.id} AND "actionState" = 'FAILED' AND "userCancel" = false - AND now()::timestamp <@ tsrange( - "cancelledAt" + $2::interval, - "cancelledAt" + $3::interval - ) + AND "cancelledAt" < now() - ${`${WALLET_RETRY_AFTER_MS} milliseconds`}::interval + AND "cancelledAt" > now() - ${`${WALLET_RETRY_BEFORE_MS} milliseconds`}::interval AND "lockedAt" IS NULL - AND "paymentAttempt" < $4 + AND "paymentAttempt" < ${WALLET_MAX_RETRIES} ORDER BY id DESC FOR UPDATE SKIP LOCKED ) @@ -496,11 +494,7 @@ const resolvers = { now() + interval '15 minutes' FROM failed ) - SELECT * FROM failed`, - me.id, - `${WALLET_RETRY_AFTER_MS} milliseconds`, - `${WALLET_RETRY_BEFORE_MS} milliseconds`, - WALLET_MAX_RETRIES) + SELECT * FROM failed` } }, Wallet: { diff --git a/prisma/migrations/20250107084543_automated_retries/migration.sql b/prisma/migrations/20250107084543_automated_retries/migration.sql index 338aae76e..17770e9e3 100644 --- a/prisma/migrations/20250107084543_automated_retries/migration.sql +++ b/prisma/migrations/20250107084543_automated_retries/migration.sql @@ -1,3 +1,4 @@ -- AlterTable ALTER TABLE "Invoice" ADD COLUMN "paymentAttempt" INTEGER NOT NULL DEFAULT 0; ALTER TABLE "Invoice" ADD COLUMN "lockedAt" TIMESTAMP(3); +CREATE INDEX "Invoice_cancelledAt_idx" ON "Invoice"("cancelledAt"); \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index ecaf17587..f1a189c1e 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -954,6 +954,7 @@ model Invoice { @@index([confirmedIndex], map: "Invoice.confirmedIndex_index") @@index([isHeld]) @@index([confirmedAt]) + @@index([cancelledAt]) @@index([actionType]) @@index([actionState]) }