chore: Watchlist schema update (#24246)
* watchlist schema update * changes addressed * no backfill audit * fix type * fix flow of migration * fix err * type err fix * add if exists check in migration
This commit is contained in:
1 parent
654a865007
commit
7cd8dbf7d8
6 files changed
+200
-77
No files matched your search
@@ -0,0 +1,113 @@
|
||||
-- CreateEnum
|
||||
CREATE TYPE "WatchlistSource" AS ENUM ('MANUAL', 'FREE_DOMAIN_POLICY');
|
||||
|
||||
-- AlterEnum
|
||||
ALTER TYPE "WatchlistAction" ADD VALUE 'ALERT';
|
||||
|
||||
|
||||
-- 1) Add a temporary UUID column `uid` and backfill it without extensions
|
||||
ALTER TABLE "Watchlist"
|
||||
ADD COLUMN IF NOT EXISTS "uid" UUID;
|
||||
|
||||
-- Backfill existing rows with UUIDs using gen_random_uuid()
|
||||
UPDATE "Watchlist"
|
||||
SET "uid" = COALESCE("uid", gen_random_uuid());
|
||||
|
||||
-- Enforce NOT NULL now that all rows have a value
|
||||
ALTER TABLE "Watchlist"
|
||||
ALTER COLUMN "uid" SET NOT NULL;
|
||||
|
||||
-- 2) Adjust uniques/indexes that reference old id? (none in your snippet)
|
||||
-- If you had FKs or PK references elsewhere, you'd update them here.
|
||||
|
||||
-- 3) Switch primary key from old `id` to new `uid`:
|
||||
-- Drop old PK (name might vary in your DB; adjust if needed)
|
||||
DO $$ BEGIN
|
||||
ALTER TABLE "Watchlist" DROP CONSTRAINT "Watchlist_pkey";
|
||||
EXCEPTION
|
||||
WHEN undefined_object THEN NULL;
|
||||
END $$;
|
||||
|
||||
-- 4) Drop the old `id` column and rename `uid`→`id`
|
||||
ALTER TABLE "Watchlist" DROP COLUMN "id";
|
||||
ALTER TABLE "Watchlist" RENAME COLUMN "uid" TO "id";
|
||||
|
||||
-- 5) Add the new PK on `id` (UUID)
|
||||
ALTER TABLE "Watchlist" ADD CONSTRAINT "Watchlist_pkey" PRIMARY KEY ("id");
|
||||
|
||||
-- DropForeignKey - Do this before modifying Watchlist structure
|
||||
ALTER TABLE "Watchlist" DROP CONSTRAINT "Watchlist_createdById_fkey";
|
||||
|
||||
-- DropForeignKey
|
||||
ALTER TABLE "Watchlist" DROP CONSTRAINT "Watchlist_updatedById_fkey";
|
||||
|
||||
-- Modify Watchlist table structure FIRST
|
||||
-- AlterTable
|
||||
ALTER TABLE "Watchlist" DROP COLUMN "createdAt",
|
||||
DROP COLUMN "createdById",
|
||||
DROP COLUMN "severity",
|
||||
DROP COLUMN "updatedAt",
|
||||
DROP COLUMN "updatedById",
|
||||
ADD COLUMN "isGlobal" BOOLEAN NOT NULL DEFAULT false,
|
||||
ADD COLUMN "lastUpdatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
ADD COLUMN "source" "WatchlistSource" NOT NULL DEFAULT 'MANUAL';
|
||||
|
||||
-- Create WatchlistAudit table AFTER Watchlist has UUID id
|
||||
-- CreateTable
|
||||
CREATE TABLE "WatchlistAudit" (
|
||||
"id" UUID NOT NULL,
|
||||
"type" "WatchlistType" NOT NULL,
|
||||
"value" TEXT NOT NULL,
|
||||
"description" TEXT,
|
||||
"action" "WatchlistAction" NOT NULL DEFAULT 'REPORT',
|
||||
"changedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"changedByUserId" INTEGER,
|
||||
"watchlistId" UUID NOT NULL,
|
||||
|
||||
CONSTRAINT "WatchlistAudit_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- Drop the BlockedBookingLog table
|
||||
-- DropTable
|
||||
DROP TABLE "BlockedBookingLog";
|
||||
|
||||
-- Drop unused enum
|
||||
-- DropEnum
|
||||
DROP TYPE "WatchlistSeverity";
|
||||
|
||||
-- Create WatchlistEventAudit table
|
||||
-- CreateTable
|
||||
CREATE TABLE "WatchlistEventAudit" (
|
||||
"id" UUID NOT NULL,
|
||||
"watchlistId" UUID NOT NULL,
|
||||
"eventTypeId" INTEGER NOT NULL,
|
||||
"actionTaken" "WatchlistAction" NOT NULL,
|
||||
"timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "WatchlistEventAudit_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- Create indexes for performance
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "WatchlistAudit_id_key" ON "WatchlistAudit"("id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX IF NOT EXISTS "WatchlistAudit_watchlistId_changedAt_idx" ON "WatchlistAudit"("watchlistId", "changedAt");
|
||||
|
||||
-- CreateIndex - Main composite index matching schema
|
||||
CREATE INDEX IF NOT EXISTS "Watchlist_type_value_organizationId_action_idx" ON "Watchlist"("type", "value", "organizationId", "action");
|
||||
|
||||
-- CreateIndex - For organization-specific and global queries
|
||||
CREATE INDEX IF NOT EXISTS "Watchlist_organizationId_isGlobal_idx" ON "Watchlist"("organizationId", "isGlobal");
|
||||
|
||||
-- CreateIndex - For source-based queries
|
||||
CREATE INDEX IF NOT EXISTS "Watchlist_source_idx" ON "Watchlist"("source");
|
||||
|
||||
-- CreateIndex - Add unique constraint matching schema
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS "Watchlist_type_value_organizationId_key" ON "Watchlist"("type", "value", "organizationId");
|
||||
|
||||
-- CreateIndex - Performance index for WatchlistEventAudit queries
|
||||
CREATE INDEX IF NOT EXISTS "WatchlistEventAudit_watchlistId_timestamp_idx" ON "WatchlistEventAudit"("watchlistId", "timestamp");
|
||||
|
||||
-- CreateIndex - For eventType-based queries
|
||||
CREATE INDEX IF NOT EXISTS "WatchlistEventAudit_eventTypeId_timestamp_idx" ON "WatchlistEventAudit"("eventTypeId", "timestamp");
|
||||
@@ -451,8 +451,6 @@ model User {
|
||||
updatedAttributeToUsers AttributeToUser[] @relation("updatedBy")
|
||||
createdTranslations EventTypeTranslation[] @relation("CreatedEventTypeTranslations")
|
||||
updatedTranslations EventTypeTranslation[] @relation("UpdatedEventTypeTranslations")
|
||||
createdWatchlists Watchlist[] @relation("CreatedWatchlists")
|
||||
updatedWatchlists Watchlist[] @relation("UpdatedWatchlists")
|
||||
BookingInternalNote BookingInternalNote[]
|
||||
creationSource CreationSource?
|
||||
createdOrganizationOnboardings OrganizationOnboarding[] @relation("CreatedOrganizationOnboardings")
|
||||
@@ -2269,54 +2267,54 @@ enum WatchlistType {
|
||||
USERNAME
|
||||
}
|
||||
|
||||
enum WatchlistSeverity {
|
||||
LOW
|
||||
MEDIUM
|
||||
HIGH
|
||||
CRITICAL
|
||||
}
|
||||
|
||||
enum WatchlistAction {
|
||||
REPORT
|
||||
BLOCK
|
||||
ALERT
|
||||
}
|
||||
|
||||
enum WatchlistSource {
|
||||
MANUAL
|
||||
FREE_DOMAIN_POLICY
|
||||
}
|
||||
|
||||
model Watchlist {
|
||||
id String @id @unique @default(cuid())
|
||||
type WatchlistType
|
||||
|
||||
value String
|
||||
description String?
|
||||
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
type WatchlistType
|
||||
value String
|
||||
description String?
|
||||
isGlobal Boolean @default(false)
|
||||
organizationId Int?
|
||||
|
||||
action WatchlistAction @default(REPORT)
|
||||
severity WatchlistSeverity @default(LOW)
|
||||
createdAt DateTime @default(now())
|
||||
createdBy User? @relation("CreatedWatchlists", onDelete: SetNull, fields: [createdById], references: [id])
|
||||
createdById Int?
|
||||
updatedAt DateTime @updatedAt
|
||||
updatedBy User? @relation("UpdatedWatchlists", onDelete: SetNull, fields: [updatedById], references: [id])
|
||||
updatedById Int?
|
||||
action WatchlistAction @default(REPORT)
|
||||
source WatchlistSource @default(MANUAL)
|
||||
lastUpdatedAt DateTime @default(now())
|
||||
|
||||
@@unique([type, value, organizationId])
|
||||
@@index([type, value, organizationId, action])
|
||||
}
|
||||
|
||||
model BlockedBookingLog {
|
||||
id String @id @default(uuid())
|
||||
email String
|
||||
eventTypeId Int?
|
||||
model WatchlistAudit {
|
||||
id String @id @default(uuid(7)) @db.Uuid
|
||||
type WatchlistType
|
||||
value String
|
||||
description String?
|
||||
|
||||
organizationId Int?
|
||||
action WatchlistAction @default(REPORT)
|
||||
|
||||
bookingData Json?
|
||||
createdAt DateTime @default(now())
|
||||
watchlistId String?
|
||||
changedAt DateTime @default(now())
|
||||
changedByUserId Int?
|
||||
|
||||
@@index([organizationId, createdAt])
|
||||
@@index([email])
|
||||
@@index([watchlistId])
|
||||
watchlistId String @db.Uuid
|
||||
|
||||
@@index([watchlistId, changedAt])
|
||||
}
|
||||
|
||||
model WatchlistEventAudit {
|
||||
id String @id @default(uuid(7)) @db.Uuid
|
||||
watchlistId String @db.Uuid
|
||||
eventTypeId Int
|
||||
actionTaken WatchlistAction
|
||||
timestamp DateTime @default(now())
|
||||
}
|
||||
|
||||
enum BillingPeriod {
|
||||
|
||||
Reference in new issue
Block a user