feat(db): 0001 来源域——connection/snapshot/job/record + 快照不可变触发器

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
浅念
2026-10-20 19:27:31 +08:00
parent 5da883cd40
commit 32ceba58f0
5 changed files with 1002 additions and 2 deletions
@@ -0,0 +1,78 @@
CREATE TYPE "public"."import_job_status" AS ENUM('queued', 'running', 'succeeded', 'succeeded_with_issues', 'failed', 'cancelled', 'stale');--> statement-breakpoint
CREATE TYPE "public"."source_connection_kind" AS ENUM('wxr');--> statement-breakpoint
CREATE TYPE "public"."source_snapshot_kind" AS ENUM('wxr', 'sitemap', 'media_bundle', 'manual_url_list');--> statement-breakpoint
CREATE TABLE "import_jobs" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"site_id" uuid NOT NULL,
"source_snapshot_id" uuid NOT NULL,
"status" "import_job_status" NOT NULL,
"mapping_version" text NOT NULL,
"idempotency_key" text NOT NULL,
"attempt" integer DEFAULT 0 NOT NULL,
"started_at" timestamp with time zone,
"finished_at" timestamp with time zone,
"error_code" text,
"error_summary" text,
"report_path" text,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT "import_jobs_idempotency_key_unique" UNIQUE("idempotency_key")
);
--> statement-breakpoint
CREATE TABLE "source_connections" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"site_id" uuid NOT NULL,
"kind" "source_connection_kind" NOT NULL,
"name" text NOT NULL,
"configuration" jsonb NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "source_records" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"site_id" uuid NOT NULL,
"source_connection_id" uuid NOT NULL,
"source_snapshot_id" uuid NOT NULL,
"import_job_id" uuid NOT NULL,
"source_type" text NOT NULL,
"source_id" text NOT NULL,
"locale" text NOT NULL,
"source_url" text,
"raw_location" text NOT NULL,
"source_checksum" text,
"normalized_checksum" text,
"parse_status" text NOT NULL,
"content_entry_id" uuid,
"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "source_snapshots" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"site_id" uuid NOT NULL,
"source_connection_id" uuid NOT NULL,
"kind" "source_snapshot_kind" NOT NULL,
"storage_path" text NOT NULL,
"sha256" text NOT NULL,
"byte_size" bigint NOT NULL,
"adapter_version" text NOT NULL,
"captured_at" timestamp with time zone NOT NULL,
"metadata" jsonb NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "import_jobs" ADD CONSTRAINT "import_jobs_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 "import_jobs" ADD CONSTRAINT "import_jobs_source_snapshot_id_source_snapshots_id_fk" FOREIGN KEY ("source_snapshot_id") REFERENCES "public"."source_snapshots"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "source_connections" ADD CONSTRAINT "source_connections_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 "source_records" ADD CONSTRAINT "source_records_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 "source_records" ADD CONSTRAINT "source_records_source_connection_id_source_connections_id_fk" FOREIGN KEY ("source_connection_id") REFERENCES "public"."source_connections"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "source_records" ADD CONSTRAINT "source_records_source_snapshot_id_source_snapshots_id_fk" FOREIGN KEY ("source_snapshot_id") REFERENCES "public"."source_snapshots"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "source_records" ADD CONSTRAINT "source_records_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 "source_snapshots" ADD CONSTRAINT "source_snapshots_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 "source_snapshots" ADD CONSTRAINT "source_snapshots_source_connection_id_source_connections_id_fk" FOREIGN KEY ("source_connection_id") REFERENCES "public"."source_connections"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "source_records_identity_uq" ON "source_records" USING btree ("site_id","source_connection_id","source_type","source_id","locale","source_snapshot_id");--> statement-breakpoint
CREATE UNIQUE INDEX "source_snapshots_uq" ON "source_snapshots" USING btree ("site_id","kind","sha256");--> statement-breakpoint
-- ↓ 手写追加(drizzle-kit 不生成触发器):快照不可变。sf_forbid_update 为共享函数,
-- 后续 content_revisions / audit_logs / build_input_snapshots 三张不可变表复用(Task 8/12)。
CREATE OR REPLACE FUNCTION sf_forbid_update() RETURNS trigger AS $$
BEGIN RAISE EXCEPTION 'immutable table %: UPDATE forbidden', TG_TABLE_NAME; END $$ LANGUAGE plpgsql;--> statement-breakpoint
CREATE TRIGGER source_snapshots_immutable BEFORE UPDATE OR DELETE ON source_snapshots
FOR EACH ROW EXECUTE FUNCTION sf_forbid_update();
+740
View File
@@ -0,0 +1,740 @@
{
"id": "6b9e4c4b-1851-48c7-b5c7-df804ef5477a",
"prevId": "b744b7d5-e0a8-4940-ab52-2c0b46abbb50",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.import_jobs": {
"name": "import_jobs",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"site_id": {
"name": "site_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"source_snapshot_id": {
"name": "source_snapshot_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"status": {
"name": "status",
"type": "import_job_status",
"typeSchema": "public",
"primaryKey": false,
"notNull": true
},
"mapping_version": {
"name": "mapping_version",
"type": "text",
"primaryKey": false,
"notNull": true
},
"idempotency_key": {
"name": "idempotency_key",
"type": "text",
"primaryKey": false,
"notNull": true
},
"attempt": {
"name": "attempt",
"type": "integer",
"primaryKey": false,
"notNull": true,
"default": 0
},
"started_at": {
"name": "started_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false
},
"finished_at": {
"name": "finished_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false
},
"error_code": {
"name": "error_code",
"type": "text",
"primaryKey": false,
"notNull": false
},
"error_summary": {
"name": "error_summary",
"type": "text",
"primaryKey": false,
"notNull": false
},
"report_path": {
"name": "report_path",
"type": "text",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {
"import_jobs_site_id_sites_id_fk": {
"name": "import_jobs_site_id_sites_id_fk",
"tableFrom": "import_jobs",
"tableTo": "sites",
"columnsFrom": [
"site_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
},
"import_jobs_source_snapshot_id_source_snapshots_id_fk": {
"name": "import_jobs_source_snapshot_id_source_snapshots_id_fk",
"tableFrom": "import_jobs",
"tableTo": "source_snapshots",
"columnsFrom": [
"source_snapshot_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"import_jobs_idempotency_key_unique": {
"name": "import_jobs_idempotency_key_unique",
"nullsNotDistinct": false,
"columns": [
"idempotency_key"
]
}
},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.site_config_revisions": {
"name": "site_config_revisions",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"site_id": {
"name": "site_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"version": {
"name": "version",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"data": {
"name": "data",
"type": "jsonb",
"primaryKey": false,
"notNull": true
},
"checksum": {
"name": "checksum",
"type": "text",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {
"site_config_rev_uq": {
"name": "site_config_rev_uq",
"columns": [
{
"expression": "site_id",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "version",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": true,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {
"site_config_revisions_site_id_sites_id_fk": {
"name": "site_config_revisions_site_id_sites_id_fk",
"tableFrom": "site_config_revisions",
"tableTo": "sites",
"columnsFrom": [
"site_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.sites": {
"name": "sites",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"key": {
"name": "key",
"type": "text",
"primaryKey": false,
"notNull": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true
},
"default_locale": {
"name": "default_locale",
"type": "text",
"primaryKey": false,
"notNull": true
},
"production_origin": {
"name": "production_origin",
"type": "text",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"sites_key_unique": {
"name": "sites_key_unique",
"nullsNotDistinct": false,
"columns": [
"key"
]
}
},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.source_connections": {
"name": "source_connections",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"site_id": {
"name": "site_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"kind": {
"name": "kind",
"type": "source_connection_kind",
"typeSchema": "public",
"primaryKey": false,
"notNull": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true
},
"configuration": {
"name": "configuration",
"type": "jsonb",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {
"source_connections_site_id_sites_id_fk": {
"name": "source_connections_site_id_sites_id_fk",
"tableFrom": "source_connections",
"tableTo": "sites",
"columnsFrom": [
"site_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.source_records": {
"name": "source_records",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"site_id": {
"name": "site_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"source_connection_id": {
"name": "source_connection_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"source_snapshot_id": {
"name": "source_snapshot_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"import_job_id": {
"name": "import_job_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"source_type": {
"name": "source_type",
"type": "text",
"primaryKey": false,
"notNull": true
},
"source_id": {
"name": "source_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"locale": {
"name": "locale",
"type": "text",
"primaryKey": false,
"notNull": true
},
"source_url": {
"name": "source_url",
"type": "text",
"primaryKey": false,
"notNull": false
},
"raw_location": {
"name": "raw_location",
"type": "text",
"primaryKey": false,
"notNull": true
},
"source_checksum": {
"name": "source_checksum",
"type": "text",
"primaryKey": false,
"notNull": false
},
"normalized_checksum": {
"name": "normalized_checksum",
"type": "text",
"primaryKey": false,
"notNull": false
},
"parse_status": {
"name": "parse_status",
"type": "text",
"primaryKey": false,
"notNull": true
},
"content_entry_id": {
"name": "content_entry_id",
"type": "uuid",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {
"source_records_identity_uq": {
"name": "source_records_identity_uq",
"columns": [
{
"expression": "site_id",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "source_connection_id",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "source_type",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "source_id",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "locale",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "source_snapshot_id",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": true,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {
"source_records_site_id_sites_id_fk": {
"name": "source_records_site_id_sites_id_fk",
"tableFrom": "source_records",
"tableTo": "sites",
"columnsFrom": [
"site_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
},
"source_records_source_connection_id_source_connections_id_fk": {
"name": "source_records_source_connection_id_source_connections_id_fk",
"tableFrom": "source_records",
"tableTo": "source_connections",
"columnsFrom": [
"source_connection_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
},
"source_records_source_snapshot_id_source_snapshots_id_fk": {
"name": "source_records_source_snapshot_id_source_snapshots_id_fk",
"tableFrom": "source_records",
"tableTo": "source_snapshots",
"columnsFrom": [
"source_snapshot_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
},
"source_records_import_job_id_import_jobs_id_fk": {
"name": "source_records_import_job_id_import_jobs_id_fk",
"tableFrom": "source_records",
"tableTo": "import_jobs",
"columnsFrom": [
"import_job_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.source_snapshots": {
"name": "source_snapshots",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"site_id": {
"name": "site_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"source_connection_id": {
"name": "source_connection_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"kind": {
"name": "kind",
"type": "source_snapshot_kind",
"typeSchema": "public",
"primaryKey": false,
"notNull": true
},
"storage_path": {
"name": "storage_path",
"type": "text",
"primaryKey": false,
"notNull": true
},
"sha256": {
"name": "sha256",
"type": "text",
"primaryKey": false,
"notNull": true
},
"byte_size": {
"name": "byte_size",
"type": "bigint",
"primaryKey": false,
"notNull": true
},
"adapter_version": {
"name": "adapter_version",
"type": "text",
"primaryKey": false,
"notNull": true
},
"captured_at": {
"name": "captured_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true
},
"metadata": {
"name": "metadata",
"type": "jsonb",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {
"source_snapshots_uq": {
"name": "source_snapshots_uq",
"columns": [
{
"expression": "site_id",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "kind",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "sha256",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": true,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {
"source_snapshots_site_id_sites_id_fk": {
"name": "source_snapshots_site_id_sites_id_fk",
"tableFrom": "source_snapshots",
"tableTo": "sites",
"columnsFrom": [
"site_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
},
"source_snapshots_source_connection_id_source_connections_id_fk": {
"name": "source_snapshots_source_connection_id_source_connections_id_fk",
"tableFrom": "source_snapshots",
"tableTo": "source_connections",
"columnsFrom": [
"source_connection_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
}
},
"enums": {
"public.import_job_status": {
"name": "import_job_status",
"schema": "public",
"values": [
"queued",
"running",
"succeeded",
"succeeded_with_issues",
"failed",
"cancelled",
"stale"
]
},
"public.source_connection_kind": {
"name": "source_connection_kind",
"schema": "public",
"values": [
"wxr"
]
},
"public.source_snapshot_kind": {
"name": "source_snapshot_kind",
"schema": "public",
"values": [
"wxr",
"sitemap",
"media_bundle",
"manual_url_list"
]
}
},
"schemas": {},
"sequences": {},
"roles": {},
"policies": {},
"views": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
+7
View File
@@ -8,6 +8,13 @@
"when": 1792494963444,
"tag": "0000_pale_sumo",
"breakpoints": true
},
{
"idx": 1,
"version": "7",
"when": 1792495504658,
"tag": "0001_broken_microchip",
"breakpoints": true
}
]
}
+71 -2
View File
@@ -1,5 +1,5 @@
// 聚合导出:Task 6 起按域追加 pgTable 定义(站点域 = 迁移文件 0000_pale_sumo)
import { pgTable, uuid, text, integer, jsonb, timestamp, uniqueIndex } from 'drizzle-orm/pg-core'
// 聚合导出:Task 6 起按域追加 pgTable 定义(站点域 = 迁移文件 0000_pale_sumo;来源域 = 0001_*)
import { pgTable, pgEnum, uuid, text, integer, bigint, jsonb, timestamp, uniqueIndex } from 'drizzle-orm/pg-core'
export const sites = pgTable('sites', {
id: uuid('id').primaryKey().defaultRandom(),
@@ -19,3 +19,72 @@ export const siteConfigRevisions = pgTable('site_config_revisions', {
checksum: text('checksum').notNull(),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
}, (t) => [uniqueIndex('site_config_rev_uq').on(t.siteId, t.version)])
// ---- 来源域(spec §5.2;迁移文件 0001_*,末尾手写追加快照不可变触发器)----
export const sourceConnectionKind = pgEnum('source_connection_kind', ['wxr'])
export const sourceSnapshotKind = pgEnum('source_snapshot_kind', ['wxr', 'sitemap', 'media_bundle', 'manual_url_list'])
export const importJobStatus = pgEnum('import_job_status', [
'queued', 'running', 'succeeded', 'succeeded_with_issues', 'failed', 'cancelled', 'stale',
])
// Connection 只描述来源,不保存某次文件路径和 Hash
export const sourceConnections = pgTable('source_connections', {
id: uuid('id').primaryKey().defaultRandom(),
siteId: uuid('site_id').notNull().references(() => sites.id),
kind: sourceConnectionKind('kind').notNull(),
name: text('name').notNull(),
configuration: jsonb('configuration').notNull(),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
})
// Snapshot 创建后不可变(库内触发器强制,见 0001 迁移手写 SQL);替换文件必须创建新 Snapshot
export const sourceSnapshots = pgTable('source_snapshots', {
id: uuid('id').primaryKey().defaultRandom(),
siteId: uuid('site_id').notNull().references(() => sites.id),
sourceConnectionId: uuid('source_connection_id').notNull().references(() => sourceConnections.id),
kind: sourceSnapshotKind('kind').notNull(),
storagePath: text('storage_path').notNull(),
sha256: text('sha256').notNull(),
byteSize: bigint('byte_size', { mode: 'number' }).notNull(),
adapterVersion: text('adapter_version').notNull(),
capturedAt: timestamp('captured_at', { withTimezone: true }).notNull(),
metadata: jsonb('metadata').notNull(),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
}, (t) => [uniqueIndex('source_snapshots_uq').on(t.siteId, t.kind, t.sha256)])
export const importJobs = pgTable('import_jobs', {
id: uuid('id').primaryKey().defaultRandom(),
siteId: uuid('site_id').notNull().references(() => sites.id),
sourceSnapshotId: uuid('source_snapshot_id').notNull().references(() => sourceSnapshots.id),
status: importJobStatus('status').notNull(),
mappingVersion: text('mapping_version').notNull(),
idempotencyKey: text('idempotency_key').notNull().unique(),
attempt: integer('attempt').notNull().default(0),
startedAt: timestamp('started_at', { withTimezone: true }),
finishedAt: timestamp('finished_at', { withTimezone: true }),
errorCode: text('error_code'),
errorSummary: text('error_summary'),
reportPath: text('report_path'),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
})
export const sourceRecords = pgTable('source_records', {
id: uuid('id').primaryKey().defaultRandom(),
siteId: uuid('site_id').notNull().references(() => sites.id),
sourceConnectionId: uuid('source_connection_id').notNull().references(() => sourceConnections.id),
sourceSnapshotId: uuid('source_snapshot_id').notNull().references(() => sourceSnapshots.id),
importJobId: uuid('import_job_id').notNull().references(() => importJobs.id),
sourceType: text('source_type').notNull(),
sourceId: text('source_id').notNull(),
locale: text('locale').notNull(),
sourceUrl: text('source_url'),
rawLocation: text('raw_location').notNull(),
sourceChecksum: text('source_checksum'),
normalizedChecksum: text('normalized_checksum'),
parseStatus: text('parse_status').notNull(),
// 暂无 FK:content_entries 于 Task 8(内容域)落地后再补引用
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)])