feat(db): 0003 分类域——taxonomy/term/content_terms

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
浅念
2026-10-20 19:48:04 +08:00
parent 60581047ef
commit bc1dcc2ef9
5 changed files with 1620 additions and 2 deletions
@@ -0,0 +1,36 @@
CREATE TABLE "content_terms" (
"site_id" uuid NOT NULL,
"content_entry_id" uuid NOT NULL,
"term_id" uuid NOT NULL,
"is_primary" boolean NOT NULL,
CONSTRAINT "content_terms_site_id_content_entry_id_term_id_pk" PRIMARY KEY("site_id","content_entry_id","term_id")
);
--> statement-breakpoint
CREATE TABLE "taxonomies" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"site_id" uuid NOT NULL,
"key" text NOT NULL,
"object_type" text NOT NULL,
"public_base_path" text NOT NULL
);
--> statement-breakpoint
CREATE TABLE "terms" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"site_id" uuid NOT NULL,
"taxonomy_id" uuid NOT NULL,
"source_id" text NOT NULL,
"slug" text NOT NULL,
"name" text NOT NULL,
"parent_term_id" uuid,
"data" jsonb NOT NULL
);
--> statement-breakpoint
ALTER TABLE "content_terms" ADD CONSTRAINT "content_terms_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_terms" ADD CONSTRAINT "content_terms_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_terms" ADD CONSTRAINT "content_terms_term_id_terms_id_fk" FOREIGN KEY ("term_id") REFERENCES "public"."terms"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "taxonomies" ADD CONSTRAINT "taxonomies_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 "terms" ADD CONSTRAINT "terms_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 "terms" ADD CONSTRAINT "terms_taxonomy_id_taxonomies_id_fk" FOREIGN KEY ("taxonomy_id") REFERENCES "public"."taxonomies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "terms" ADD CONSTRAINT "terms_parent_term_id_terms_id_fk" FOREIGN KEY ("parent_term_id") REFERENCES "public"."terms"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "taxonomies_uq" ON "taxonomies" USING btree ("site_id","key","object_type");--> statement-breakpoint
CREATE UNIQUE INDEX "terms_identity_uq" ON "terms" USING btree ("site_id","taxonomy_id","source_id");
File diff suppressed because it is too large Load Diff
+7
View File
@@ -22,6 +22,13 @@
"when": 1792496109431,
"tag": "0002_fancy_silver_centurion",
"breakpoints": true
},
{
"idx": 3,
"version": "7",
"when": 1792496827818,
"tag": "0003_groovy_phil_sheldon",
"breakpoints": true
}
]
}
+37 -2
View File
@@ -1,5 +1,5 @@
// 聚合导出:Task 6 起按域追加 pgTable 定义(站点域 = 迁移文件 0000_pale_sumo;来源域 = 0001_*;内容域 = 0002_*)
import { pgTable, pgEnum, uuid, text, integer, bigint, jsonb, timestamp, uniqueIndex, index } from 'drizzle-orm/pg-core'
// 聚合导出:Task 6 起按域追加 pgTable 定义(站点域 = 迁移文件 0000_pale_sumo;来源域 = 0001_*;内容域 = 0002_*;分类域 = 0003_*)
import { pgTable, pgEnum, uuid, text, integer, bigint, jsonb, timestamp, uniqueIndex, index, boolean, primaryKey, type AnyPgColumn } from 'drizzle-orm/pg-core'
export const sites = pgTable('sites', {
id: uuid('id').primaryKey().defaultRandom(),
@@ -137,3 +137,38 @@ export const contentRevisions = pgTable('content_revisions', {
createdBy: text('created_by').notNull(),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
})
// ---- 分类域(spec §5.4;迁移文件 0003_*,无手写 SQL)----
// 支撑分类列表页、产品分类、分页和 Yoast 主分类——不允许临时塞进内容 JSON 后再重构(spec 原话)。
// 分类体系定义(category / tag / product_cat 等):同 key 可服务不同 object_type
// (如 post 与 product 各挂一套 category),故唯一键是 site+key+object_type 三元组。
export const taxonomies = pgTable('taxonomies', {
id: uuid('id').primaryKey().defaultRandom(),
siteId: uuid('site_id').notNull().references(() => sites.id),
key: text('key').notNull(),
objectType: text('object_type').notNull(),
publicBasePath: text('public_base_path').notNull(),
}, (t) => [uniqueIndex('taxonomies_uq').on(t.siteId, t.key, t.objectType)])
// Term 挂在 taxonomy 下;source_id 是来源系统内稳定标识(如 WP term_id),
// 跨 Snapshot 再导入靠 site+taxonomy+source_id 唯一键幂等;层级用可空自引用 FK。
export const terms = pgTable('terms', {
id: uuid('id').primaryKey().defaultRandom(),
siteId: uuid('site_id').notNull().references(() => sites.id),
taxonomyId: uuid('taxonomy_id').notNull().references(() => taxonomies.id),
sourceId: text('source_id').notNull(),
slug: text('slug').notNull(),
name: text('name').notNull(),
parentTermId: uuid('parent_term_id').references((): AnyPgColumn => terms.id),
data: jsonb('data').notNull(),
}, (t) => [uniqueIndex('terms_identity_uq').on(t.siteId, t.taxonomyId, t.sourceId)])
// 内容-分类关联:复合主键 (site, entry, term) 天然防重(同一 entry 对同一 term 只挂一次,
// is_primary 可在不同关联间翻转);"每 entry 每 taxonomy 至多一个 primary" 由应用层保证,库层不设部分唯一索引。
export const contentTerms = pgTable('content_terms', {
siteId: uuid('site_id').notNull().references(() => sites.id),
contentEntryId: uuid('content_entry_id').notNull().references(() => contentEntries.id),
termId: uuid('term_id').notNull().references(() => terms.id),
isPrimary: boolean('is_primary').notNull(),
}, (t) => [primaryKey({ columns: [t.siteId, t.contentEntryId, t.termId] })])