feat(db): 0002 内容域——entry/locale/revision + 修订不可变 + 回填 content_entry FK
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
CREATE TYPE "public"."content_lifecycle_status" AS ENUM('active', 'archived');--> statement-breakpoint
|
||||
CREATE TYPE "public"."revision_source_kind" AS ENUM('import', 'manual', 'restore');--> statement-breakpoint
|
||||
CREATE TABLE "content_entries" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"site_id" uuid NOT NULL,
|
||||
"type_key" text NOT NULL,
|
||||
"lifecycle_status" "content_lifecycle_status" NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "content_revisions" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"site_id" uuid NOT NULL,
|
||||
"content_entry_id" uuid NOT NULL,
|
||||
"locale" text NOT NULL,
|
||||
"schema_version" integer NOT NULL,
|
||||
"data" jsonb NOT NULL,
|
||||
"checksum" text NOT NULL,
|
||||
"source_kind" "revision_source_kind" NOT NULL,
|
||||
"source_record_id" uuid,
|
||||
"import_job_id" uuid,
|
||||
"overridden_fields" jsonb DEFAULT '[]'::jsonb NOT NULL,
|
||||
"created_by" text NOT NULL,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "entry_locales" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"site_id" uuid NOT NULL,
|
||||
"content_entry_id" uuid NOT NULL,
|
||||
"locale" text NOT NULL,
|
||||
"slug" text NOT NULL,
|
||||
"current_revision_id" uuid,
|
||||
"published_revision_id" uuid,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "content_entries" ADD CONSTRAINT "content_entries_site_id_sites_id_fk" FOREIGN KEY ("site_id") REFERENCES "public"."sites"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "content_revisions" ADD CONSTRAINT "content_revisions_site_id_sites_id_fk" FOREIGN KEY ("site_id") REFERENCES "public"."sites"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "content_revisions" ADD CONSTRAINT "content_revisions_content_entry_id_content_entries_id_fk" FOREIGN KEY ("content_entry_id") REFERENCES "public"."content_entries"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "content_revisions" ADD CONSTRAINT "content_revisions_import_job_id_import_jobs_id_fk" FOREIGN KEY ("import_job_id") REFERENCES "public"."import_jobs"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "entry_locales" ADD CONSTRAINT "entry_locales_site_id_sites_id_fk" FOREIGN KEY ("site_id") REFERENCES "public"."sites"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "entry_locales" ADD CONSTRAINT "entry_locales_content_entry_id_content_entries_id_fk" FOREIGN KEY ("content_entry_id") REFERENCES "public"."content_entries"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
||||
CREATE INDEX "content_entries_site_type_idx" ON "content_entries" USING btree ("site_id","type_key");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "entry_locales_uq" ON "entry_locales" USING btree ("site_id","content_entry_id","locale");--> statement-breakpoint
|
||||
-- ↓ 手写追加(drizzle-kit 不生成触发器):修订不可变,复用 0001 的 sf_forbid_update。
|
||||
-- 发布 = 只 UPDATE entry_locales 双指针(current/published),修订行本身库级禁改禁删(ADR-010)。
|
||||
CREATE TRIGGER content_revisions_immutable BEFORE UPDATE OR DELETE ON content_revisions
|
||||
FOR EACH ROW EXECUTE FUNCTION sf_forbid_update();--> statement-breakpoint
|
||||
-- 补 source_records.content_entry_id → content_entries FK(Task 7 预留,schema 层不声明故由手写 SQL 回填)
|
||||
ALTER TABLE source_records
|
||||
ADD CONSTRAINT source_records_content_entry_id_fkey
|
||||
FOREIGN KEY (content_entry_id) REFERENCES content_entries(id);
|
||||
File diff suppressed because it is too large
Load Diff
@@ -15,6 +15,13 @@
|
||||
"when": 1792495504658,
|
||||
"tag": "0001_broken_microchip",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 2,
|
||||
"version": "7",
|
||||
"when": 1792496109431,
|
||||
"tag": "0002_fancy_silver_centurion",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
// 聚合导出:Task 6 起按域追加 pgTable 定义(站点域 = 迁移文件 0000_pale_sumo;来源域 = 0001_*)
|
||||
import { pgTable, pgEnum, uuid, text, integer, bigint, jsonb, timestamp, uniqueIndex } from 'drizzle-orm/pg-core'
|
||||
// 聚合导出:Task 6 起按域追加 pgTable 定义(站点域 = 迁移文件 0000_pale_sumo;来源域 = 0001_*;内容域 = 0002_*)
|
||||
import { pgTable, pgEnum, uuid, text, integer, bigint, jsonb, timestamp, uniqueIndex, index } from 'drizzle-orm/pg-core'
|
||||
|
||||
export const sites = pgTable('sites', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
@@ -83,8 +83,57 @@ export const sourceRecords = pgTable('source_records', {
|
||||
sourceChecksum: text('source_checksum'),
|
||||
normalizedChecksum: text('normalized_checksum'),
|
||||
parseStatus: text('parse_status').notNull(),
|
||||
// 暂无 FK:content_entries 于 Task 8(内容域)落地后再补引用
|
||||
// FK 由 0002 迁移手写 SQL 回填(Task 7 预留;schema 层不声明以避免与生成器 diff 漂移)
|
||||
contentEntryId: uuid('content_entry_id'),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
// 跨 Snapshot 稳定映射键 = site_id + source_connection_id + source_type + source_id + locale(snapshot_id 进键以允许跨快照再导入)
|
||||
}, (t) => [uniqueIndex('source_records_identity_uq').on(t.siteId, t.sourceConnectionId, t.sourceType, t.sourceId, t.locale, t.sourceSnapshotId)])
|
||||
|
||||
// ---- 内容域(spec §5.3;迁移文件 0002_*,末尾手写追加修订不可变触发器 + 回填 source_records.content_entry_id FK)----
|
||||
|
||||
export const contentLifecycleStatus = pgEnum('content_lifecycle_status', ['active', 'archived'])
|
||||
export const revisionSourceKind = pgEnum('revision_source_kind', ['import', 'manual', 'restore'])
|
||||
|
||||
// Entry = 内容聚合根(身份与 locale 无关;不用 Permalink 做身份)
|
||||
export const contentEntries = pgTable('content_entries', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
siteId: uuid('site_id').notNull().references(() => sites.id),
|
||||
typeKey: text('type_key').notNull(),
|
||||
lifecycleStatus: contentLifecycleStatus('lifecycle_status').notNull(),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
}, (t) => [index('content_entries_site_type_idx').on(t.siteId, t.typeKey)])
|
||||
|
||||
// 双指针:current=最新工作修订,published=公开修订;两者不同即"已发布但有未发布修改"(构建只读 published)。
|
||||
// 发布 = 只 UPDATE 本表指针,修订行永不 UPDATE(ADR-010)。
|
||||
export const entryLocales = pgTable('entry_locales', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
siteId: uuid('site_id').notNull().references(() => sites.id),
|
||||
contentEntryId: uuid('content_entry_id').notNull().references(() => contentEntries.id),
|
||||
locale: text('locale').notNull(),
|
||||
slug: text('slug').notNull(),
|
||||
// 双指针本任务不设 FK:修订表在同一迁移创建,循环引用让 drizzle 处理麻烦;
|
||||
// 指针目标合法性由应用层 + Task 13 测试保证,或后续迁移补 FK
|
||||
currentRevisionId: uuid('current_revision_id'),
|
||||
publishedRevisionId: uuid('published_revision_id'),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
// slug 唯一性由 Route Registry 决定,不在此假设共享 slug 空间
|
||||
}, (t) => [uniqueIndex('entry_locales_uq').on(t.siteId, t.contentEntryId, t.locale)])
|
||||
|
||||
// 修订创建后不可变(库内触发器强制,见 0002 迁移手写 SQL;只 INSERT,无 UPDATE/DELETE 业务接口)
|
||||
export const contentRevisions = pgTable('content_revisions', {
|
||||
id: uuid('id').primaryKey().defaultRandom(),
|
||||
siteId: uuid('site_id').notNull().references(() => sites.id),
|
||||
contentEntryId: uuid('content_entry_id').notNull().references(() => contentEntries.id),
|
||||
locale: text('locale').notNull(),
|
||||
schemaVersion: integer('schema_version').notNull(),
|
||||
data: jsonb('data').notNull(),
|
||||
checksum: text('checksum').notNull(),
|
||||
sourceKind: revisionSourceKind('source_kind').notNull(),
|
||||
sourceRecordId: uuid('source_record_id'),
|
||||
importJobId: uuid('import_job_id').references(() => importJobs.id),
|
||||
overriddenFields: jsonb('overridden_fields').notNull().default([]),
|
||||
createdBy: text('created_by').notNull(),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user