From fd74ad703b7efb25cb9d5984eb6e9c7ac4e77049 Mon Sep 17 00:00:00 2001 From: liangxu Date: Fri, 17 Apr 2026 08:04:56 +0800 Subject: [PATCH] feat(kol): enhance KOL constraints per review findings - Add composite FK kol_usage_logs(prompt_id, prompt_revision_no) -> kol_prompt_revisions to enforce audit replay integrity - Add CHECK constraint: active prompts must have a published revision - Add tenant-leading index on kol_packages for list path performance Addresses review feedback on commit 60fb8a4. --- ...0417100050_enhance_kol_constraints.down.sql | 7 +++++++ ...260417100050_enhance_kol_constraints.up.sql | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 server/migrations/20260417100050_enhance_kol_constraints.down.sql create mode 100644 server/migrations/20260417100050_enhance_kol_constraints.up.sql diff --git a/server/migrations/20260417100050_enhance_kol_constraints.down.sql b/server/migrations/20260417100050_enhance_kol_constraints.down.sql new file mode 100644 index 0000000..959ec06 --- /dev/null +++ b/server/migrations/20260417100050_enhance_kol_constraints.down.sql @@ -0,0 +1,7 @@ +DROP INDEX IF EXISTS idx_kol_package_tenant; + +ALTER TABLE kol_prompts + DROP CONSTRAINT IF EXISTS chk_kol_prompts_active_has_published_revision; + +ALTER TABLE kol_usage_logs + DROP CONSTRAINT IF EXISTS fk_kol_usage_logs_prompt_revision; diff --git a/server/migrations/20260417100050_enhance_kol_constraints.up.sql b/server/migrations/20260417100050_enhance_kol_constraints.up.sql new file mode 100644 index 0000000..23fb1ab --- /dev/null +++ b/server/migrations/20260417100050_enhance_kol_constraints.up.sql @@ -0,0 +1,18 @@ +-- Enhance KOL core tables with integrity constraints and missing index. +-- Follow-up to 20260417100000 addressing review findings: +-- 1. Composite FK for usage-log audit replay (prompt_id, prompt_revision_no) +-- 2. CHECK: active prompts must have a published revision +-- 3. Tenant-leading index on kol_packages for list paths + +ALTER TABLE kol_usage_logs + ADD CONSTRAINT fk_kol_usage_logs_prompt_revision + FOREIGN KEY (prompt_id, prompt_revision_no) + REFERENCES kol_prompt_revisions(prompt_id, revision_no); + +ALTER TABLE kol_prompts + ADD CONSTRAINT chk_kol_prompts_active_has_published_revision + CHECK (status <> 'active' OR published_revision_no IS NOT NULL); + +CREATE INDEX idx_kol_package_tenant + ON kol_packages(tenant_id, created_at DESC) + WHERE deleted_at IS NULL;