From 6456bd27554d6b44e85797ba82a5300a72ee6973 Mon Sep 17 00:00:00 2001 From: Rayyanah0 Date: Sun, 30 Aug 2026 09:27:34 +0100 Subject: [PATCH] fix: add missing db indexes for webhook queue, overdue sweep, and invoice lists Add composite indexes supporting the hot queries: - webhook_deliveries(status, next_attempt_at) for the queue worker polling pending deliveries due for retry - invoices(status, due_date) for the daily overdue sweep - invoices(merchant_id, is_draft, status, created_at) for the merchant invoice list/dashboard filters ordered by creation date --- .../migration.sql | 12 ++++++++++++ backend/prisma/schema.prisma | 3 +++ 2 files changed, 15 insertions(+) create mode 100644 backend/prisma/migrations/20260830000000_add_webhook_overdue_invoice_indexes/migration.sql diff --git a/backend/prisma/migrations/20260830000000_add_webhook_overdue_invoice_indexes/migration.sql b/backend/prisma/migrations/20260830000000_add_webhook_overdue_invoice_indexes/migration.sql new file mode 100644 index 0000000..4fc00f1 --- /dev/null +++ b/backend/prisma/migrations/20260830000000_add_webhook_overdue_invoice_indexes/migration.sql @@ -0,0 +1,12 @@ +-- Add missing database indexes for the webhook queue, overdue sweep, and +-- merchant invoice list queries. + +-- Webhook queue: the worker polls pending deliveries whose next attempt is due. +CREATE INDEX "webhook_deliveries_status_next_attempt_at_idx" ON "webhook_deliveries"("status", "next_attempt_at"); + +-- Overdue sweep: the daily cron marks pending invoices past their due date as overdue. +CREATE INDEX "invoices_status_due_date_idx" ON "invoices"("status", "due_date"); + +-- Merchant invoice list: the dashboard lists a merchant's non-draft invoices by +-- status, ordered by creation date. +CREATE INDEX "invoices_merchant_id_is_draft_status_created_at_idx" ON "invoices"("merchant_id", "is_draft", "status", "created_at"); diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prisma index 45b2042..4e133bd 100644 --- a/backend/prisma/schema.prisma +++ b/backend/prisma/schema.prisma @@ -128,6 +128,7 @@ model WebhookDelivery { createdAt DateTime @default(now()) @map("created_at") updatedAt DateTime @updatedAt @map("updated_at") + @@index([status, nextAttemptAt]) @@map("webhook_deliveries") } @@ -246,6 +247,8 @@ model Invoice { @@index([merchantId]) @@index([isDraft, merchantId]) @@index([lastAutoSavedAt]) + @@index([status, dueDate]) + @@index([merchantId, isDraft, status, createdAt]) @@map("invoices") }