feat(db): 0005 路由注册表——exact/collision 双键 + NULLS NOT DISTINCT 唯一

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
浅念
2026-10-20 20:14:46 +08:00
parent de415e7301
commit 2b017ed197
5 changed files with 2111 additions and 1 deletions
+42 -1
View File
@@ -1,4 +1,4 @@
// 聚合导出:Task 6 起按域追加 pgTable 定义(站点域 = 迁移文件 0000_pale_sumo;来源域 = 0001_*;内容域 = 0002_*;分类域 = 0003_*;媒体域 = 0004_*)
// 聚合导出:Task 6 起按域追加 pgTable 定义(站点域 = 迁移文件 0000_pale_sumo;来源域 = 0001_*;内容域 = 0002_*;分类域 = 0003_*;媒体域 = 0004_*;路由域 = 0005_*)
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', {
@@ -213,3 +213,44 @@ export const mediaUsages = pgTable('media_usages', {
fieldPath: text('field_path').notNull(),
sourceUrl: text('source_url').notNull(),
}, (t) => [primaryKey({ columns: [t.mediaAssetId, t.contentRevisionId, t.fieldPath] })])
// ---- 路由域(spec §5.6;迁移文件 0005_*,末尾手写追加 NULLS NOT DISTINCT 唯一索引)----
export const routeKind = pgEnum('route_kind', ['html', 'asset', 'redirect', 'gone', 'dynamic', 'unresolved'])
export const routeQueryPolicy = pgEnum('route_query_policy', ['ignore', 'exact', 'preserve', 'dynamic'])
// ADR-007 双键:exact_route_key 保留大小写/尾斜杠语义用于请求匹配(真实路由不折叠——
// /About 与 /about 是两条路由);collision_key 只折叠给风险检测用(Windows 大小写不敏感、
// 尾斜杠、Unicode NFC),永不参与唯一约束。查询路由单独建模(query_key),不与 path 静默合并。
export const routeRegistry = pgTable('route_registry', {
id: uuid('id').primaryKey().defaultRandom(),
siteId: uuid('site_id').notNull().references(() => sites.id),
host: text('host').notNull(),
// source_path_raw 永远保存来源原文;route_path 是经安全校验的实际公开路径(spec §5.6 规则)
sourcePathRaw: text('source_path_raw').notNull(),
routePath: text('route_path').notNull(),
exactRouteKey: text('exact_route_key').notNull(),
collisionKey: text('collision_key').notNull(),
queryPolicy: routeQueryPolicy('query_policy').notNull(),
// 纯路径路由 query_key 为 NULL——其唯一性靠 0005 迁移手写索引的 NULLS NOT DISTINCT 兜底
queryKey: text('query_key'),
sourceStatus: text('source_status').notNull(),
targetStatus: text('target_status').notNull(),
routeKind: routeKind('route_kind').notNull(),
// target_content_id 指向 content_entries,但照 spec §5.6 与 Task 11 清单暂不设 FK
// (同 source_records.content_entry_id 先例:需要时由后续迁移手写回填)
targetContentId: uuid('target_content_id'),
targetFile: text('target_file'),
redirectStatus: integer('redirect_status'),
redirectLocation: text('redirect_location'),
indexable: boolean('indexable').notNull(),
priority: integer('priority').notNull(),
sourceSnapshotId: uuid('source_snapshot_id').references(() => sourceSnapshots.id),
verificationStatus: text('verification_status').notNull(),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
// 注意:唯一索引 route_exact_uq (site_id, host, exact_route_key, query_key) NULLS NOT DISTINCT
// 故意的在此不定义——drizzle-kit 0.31 无法生成 NULLS NOT DISTINCT 子句,声明普通唯一索引
// 会产出语义错误(默认 NULLS DISTINCT,纯路径路由两行 NULL 不冲突)的迁移。该索引由
// 0005 迁移文件末尾手写 SQL 追加,集成测试 db.routes.test.ts 守住其存在与语义。
})