feat(db): add missing FK indexes on Listing, Payment, Subscription

- Add @@index([sellerId]) and @@index([propertyId]) to Listing model
- Add @@index([transactionId]) to Payment model
- Add @@index([planId]) to Subscription model
- Prevents full table scans on frequently-queried foreign keys

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Ho Ngoc Hai
2026-04-08 04:02:13 +07:00
parent 402b5b6810
commit f55c8a8788
2 changed files with 125 additions and 0 deletions

View File

@@ -0,0 +1,121 @@
-- CreateEnum
CREATE TYPE "OAuthProvider" AS ENUM ('GOOGLE', 'ZALO');
-- CreateEnum
CREATE TYPE "NotificationChannel" AS ENUM ('EMAIL', 'SMS', 'PUSH', 'ZALO_OA');
-- CreateEnum
CREATE TYPE "NotificationStatus" AS ENUM ('PENDING', 'SENT', 'FAILED', 'DELIVERED');
-- CreateTable
CREATE TABLE "RefreshToken" (
"id" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"token" TEXT NOT NULL,
"family" TEXT NOT NULL,
"expiresAt" TIMESTAMP(3) NOT NULL,
"revokedAt" TIMESTAMP(3),
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "RefreshToken_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "OAuthAccount" (
"id" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"provider" "OAuthProvider" NOT NULL,
"providerUserId" TEXT NOT NULL,
"accessToken" TEXT,
"refreshToken" TEXT,
"expiresAt" TIMESTAMP(3),
"profile" JSONB,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "OAuthAccount_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "NotificationLog" (
"id" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"channel" "NotificationChannel" NOT NULL,
"templateKey" TEXT NOT NULL,
"subject" TEXT,
"body" TEXT NOT NULL,
"metadata" JSONB,
"status" "NotificationStatus" NOT NULL DEFAULT 'PENDING',
"errorDetail" TEXT,
"sentAt" TIMESTAMP(3),
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "NotificationLog_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "NotificationPreference" (
"id" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"channel" "NotificationChannel" NOT NULL,
"eventType" TEXT NOT NULL,
"enabled" BOOLEAN NOT NULL DEFAULT true,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "NotificationPreference_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "RefreshToken_token_key" ON "RefreshToken"("token");
-- CreateIndex
CREATE INDEX "RefreshToken_userId_idx" ON "RefreshToken"("userId");
-- CreateIndex
CREATE INDEX "RefreshToken_family_idx" ON "RefreshToken"("family");
-- CreateIndex
CREATE INDEX "RefreshToken_expiresAt_idx" ON "RefreshToken"("expiresAt");
-- CreateIndex
CREATE INDEX "OAuthAccount_userId_idx" ON "OAuthAccount"("userId");
-- CreateIndex
CREATE UNIQUE INDEX "OAuthAccount_provider_providerUserId_key" ON "OAuthAccount"("provider", "providerUserId");
-- CreateIndex
CREATE INDEX "NotificationLog_userId_idx" ON "NotificationLog"("userId");
-- CreateIndex
CREATE INDEX "NotificationLog_channel_status_idx" ON "NotificationLog"("channel", "status");
-- CreateIndex
CREATE INDEX "NotificationLog_templateKey_idx" ON "NotificationLog"("templateKey");
-- CreateIndex
CREATE INDEX "NotificationLog_createdAt_idx" ON "NotificationLog"("createdAt");
-- CreateIndex
CREATE INDEX "NotificationPreference_userId_idx" ON "NotificationPreference"("userId");
-- CreateIndex
CREATE UNIQUE INDEX "NotificationPreference_userId_channel_eventType_key" ON "NotificationPreference"("userId", "channel", "eventType");
-- CreateIndex
CREATE INDEX "Listing_sellerId_idx" ON "Listing"("sellerId");
-- CreateIndex
CREATE INDEX "Listing_propertyId_idx" ON "Listing"("propertyId");
-- CreateIndex
CREATE INDEX "Payment_transactionId_idx" ON "Payment"("transactionId");
-- CreateIndex
CREATE INDEX "Subscription_planId_idx" ON "Subscription"("planId");
-- AddForeignKey
ALTER TABLE "RefreshToken" ADD CONSTRAINT "RefreshToken_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "OAuthAccount" ADD CONSTRAINT "OAuthAccount_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@@ -242,6 +242,8 @@ model Listing {
@@index([status]) @@index([status])
@@index([transactionType]) @@index([transactionType])
@@index([priceVND]) @@index([priceVND])
@@index([sellerId])
@@index([propertyId])
@@index([agentId]) @@index([agentId])
@@index([publishedAt]) @@index([publishedAt])
} }
@@ -372,6 +374,7 @@ model Payment {
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
@@index([userId]) @@index([userId])
@@index([transactionId])
@@index([status]) @@index([status])
@@index([providerTxId]) @@index([providerTxId])
} }
@@ -423,6 +426,7 @@ model Subscription {
usageRecords UsageRecord[] usageRecords UsageRecord[]
@@index([planId])
@@index([status]) @@index([status])
} }