From bc1dcc2ef93f46205a2ff63c95912a1c85adf574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=85=E5=BF=B5?= Date: Tue, 20 Oct 2026 19:48:04 +0800 Subject: [PATCH] =?UTF-8?q?feat(db):=200003=20=E5=88=86=E7=B1=BB=E5=9F=9F?= =?UTF-8?q?=E2=80=94=E2=80=94taxonomy/term/content=5Fterms?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Code --- apps/api/drizzle/0003_groovy_phil_sheldon.sql | 36 + apps/api/drizzle/meta/0003_snapshot.json | 1408 +++++++++++++++++ apps/api/drizzle/meta/_journal.json | 7 + apps/api/src/db/schema.ts | 39 +- tests/integration/db.taxonomy.test.ts | 132 ++ 5 files changed, 1620 insertions(+), 2 deletions(-) create mode 100644 apps/api/drizzle/0003_groovy_phil_sheldon.sql create mode 100644 apps/api/drizzle/meta/0003_snapshot.json create mode 100644 tests/integration/db.taxonomy.test.ts diff --git a/apps/api/drizzle/0003_groovy_phil_sheldon.sql b/apps/api/drizzle/0003_groovy_phil_sheldon.sql new file mode 100644 index 0000000..8a914ca --- /dev/null +++ b/apps/api/drizzle/0003_groovy_phil_sheldon.sql @@ -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"); \ No newline at end of file diff --git a/apps/api/drizzle/meta/0003_snapshot.json b/apps/api/drizzle/meta/0003_snapshot.json new file mode 100644 index 0000000..348ef0a --- /dev/null +++ b/apps/api/drizzle/meta/0003_snapshot.json @@ -0,0 +1,1408 @@ +{ + "id": "d41c9c0d-f4c9-4265-8482-fec7c3559852", + "prevId": "76da975d-44c9-4a1f-ab30-29fae9e227ed", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.content_entries": { + "name": "content_entries", + "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 + }, + "type_key": { + "name": "type_key", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "lifecycle_status": { + "name": "lifecycle_status", + "type": "content_lifecycle_status", + "typeSchema": "public", + "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": { + "content_entries_site_type_idx": { + "name": "content_entries_site_type_idx", + "columns": [ + { + "expression": "site_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "type_key", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "content_entries_site_id_sites_id_fk": { + "name": "content_entries_site_id_sites_id_fk", + "tableFrom": "content_entries", + "tableTo": "sites", + "columnsFrom": [ + "site_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.content_revisions": { + "name": "content_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 + }, + "content_entry_id": { + "name": "content_entry_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "locale": { + "name": "locale", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "schema_version": { + "name": "schema_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 + }, + "source_kind": { + "name": "source_kind", + "type": "revision_source_kind", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "source_record_id": { + "name": "source_record_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "import_job_id": { + "name": "import_job_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "overridden_fields": { + "name": "overridden_fields", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'[]'::jsonb" + }, + "created_by": { + "name": "created_by", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "content_revisions_site_id_sites_id_fk": { + "name": "content_revisions_site_id_sites_id_fk", + "tableFrom": "content_revisions", + "tableTo": "sites", + "columnsFrom": [ + "site_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "content_revisions_content_entry_id_content_entries_id_fk": { + "name": "content_revisions_content_entry_id_content_entries_id_fk", + "tableFrom": "content_revisions", + "tableTo": "content_entries", + "columnsFrom": [ + "content_entry_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "content_revisions_import_job_id_import_jobs_id_fk": { + "name": "content_revisions_import_job_id_import_jobs_id_fk", + "tableFrom": "content_revisions", + "tableTo": "import_jobs", + "columnsFrom": [ + "import_job_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.content_terms": { + "name": "content_terms", + "schema": "", + "columns": { + "site_id": { + "name": "site_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "content_entry_id": { + "name": "content_entry_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "term_id": { + "name": "term_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "is_primary": { + "name": "is_primary", + "type": "boolean", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "content_terms_site_id_sites_id_fk": { + "name": "content_terms_site_id_sites_id_fk", + "tableFrom": "content_terms", + "tableTo": "sites", + "columnsFrom": [ + "site_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "content_terms_content_entry_id_content_entries_id_fk": { + "name": "content_terms_content_entry_id_content_entries_id_fk", + "tableFrom": "content_terms", + "tableTo": "content_entries", + "columnsFrom": [ + "content_entry_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "content_terms_term_id_terms_id_fk": { + "name": "content_terms_term_id_terms_id_fk", + "tableFrom": "content_terms", + "tableTo": "terms", + "columnsFrom": [ + "term_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "content_terms_site_id_content_entry_id_term_id_pk": { + "name": "content_terms_site_id_content_entry_id_term_id_pk", + "columns": [ + "site_id", + "content_entry_id", + "term_id" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.entry_locales": { + "name": "entry_locales", + "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 + }, + "content_entry_id": { + "name": "content_entry_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "locale": { + "name": "locale", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "slug": { + "name": "slug", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "current_revision_id": { + "name": "current_revision_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "published_revision_id": { + "name": "published_revision_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "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": { + "entry_locales_uq": { + "name": "entry_locales_uq", + "columns": [ + { + "expression": "site_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "content_entry_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "locale", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "entry_locales_site_id_sites_id_fk": { + "name": "entry_locales_site_id_sites_id_fk", + "tableFrom": "entry_locales", + "tableTo": "sites", + "columnsFrom": [ + "site_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "entry_locales_content_entry_id_content_entries_id_fk": { + "name": "entry_locales_content_entry_id_content_entries_id_fk", + "tableFrom": "entry_locales", + "tableTo": "content_entries", + "columnsFrom": [ + "content_entry_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "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 + }, + "public.taxonomies": { + "name": "taxonomies", + "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 + }, + "key": { + "name": "key", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "object_type": { + "name": "object_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "public_base_path": { + "name": "public_base_path", + "type": "text", + "primaryKey": false, + "notNull": true + } + }, + "indexes": { + "taxonomies_uq": { + "name": "taxonomies_uq", + "columns": [ + { + "expression": "site_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "key", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "object_type", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "taxonomies_site_id_sites_id_fk": { + "name": "taxonomies_site_id_sites_id_fk", + "tableFrom": "taxonomies", + "tableTo": "sites", + "columnsFrom": [ + "site_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.terms": { + "name": "terms", + "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 + }, + "taxonomy_id": { + "name": "taxonomy_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "source_id": { + "name": "source_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "slug": { + "name": "slug", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "parent_term_id": { + "name": "parent_term_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "data": { + "name": "data", + "type": "jsonb", + "primaryKey": false, + "notNull": true + } + }, + "indexes": { + "terms_identity_uq": { + "name": "terms_identity_uq", + "columns": [ + { + "expression": "site_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "taxonomy_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "source_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "terms_site_id_sites_id_fk": { + "name": "terms_site_id_sites_id_fk", + "tableFrom": "terms", + "tableTo": "sites", + "columnsFrom": [ + "site_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "terms_taxonomy_id_taxonomies_id_fk": { + "name": "terms_taxonomy_id_taxonomies_id_fk", + "tableFrom": "terms", + "tableTo": "taxonomies", + "columnsFrom": [ + "taxonomy_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "terms_parent_term_id_terms_id_fk": { + "name": "terms_parent_term_id_terms_id_fk", + "tableFrom": "terms", + "tableTo": "terms", + "columnsFrom": [ + "parent_term_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + } + }, + "enums": { + "public.content_lifecycle_status": { + "name": "content_lifecycle_status", + "schema": "public", + "values": [ + "active", + "archived" + ] + }, + "public.import_job_status": { + "name": "import_job_status", + "schema": "public", + "values": [ + "queued", + "running", + "succeeded", + "succeeded_with_issues", + "failed", + "cancelled", + "stale" + ] + }, + "public.revision_source_kind": { + "name": "revision_source_kind", + "schema": "public", + "values": [ + "import", + "manual", + "restore" + ] + }, + "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": {} + } +} \ No newline at end of file diff --git a/apps/api/drizzle/meta/_journal.json b/apps/api/drizzle/meta/_journal.json index 2d02e0c..7faa7eb 100644 --- a/apps/api/drizzle/meta/_journal.json +++ b/apps/api/drizzle/meta/_journal.json @@ -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 } ] } \ No newline at end of file diff --git a/apps/api/src/db/schema.ts b/apps/api/src/db/schema.ts index a39804a..743fdc4 100644 --- a/apps/api/src/db/schema.ts +++ b/apps/api/src/db/schema.ts @@ -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] })]) diff --git a/tests/integration/db.taxonomy.test.ts b/tests/integration/db.taxonomy.test.ts new file mode 100644 index 0000000..63f16cf --- /dev/null +++ b/tests/integration/db.taxonomy.test.ts @@ -0,0 +1,132 @@ +// 分类域集成测试:taxonomies 三元组唯一(换 object_type 放行对照——同 key 服务不同对象类型是合法的, +// 如 post 与 product 各挂一套 category)+ terms 来源身份唯一 + content_terms 复合主键 + +// terms.parent_term_id 自引用 FK + is_primary 布尔列(Yoast 主分类)。 +// 生命周期纪律:随机后缀插入 + finally 反序清理,可重复运行不残留脏数据(本域无不可变表,常规删除即可; +// content_terms 依赖 content_entries,fixture 建链照 Task 8 模式只建到 entry 即可)。 +import { describe, it, expect } from 'vitest' +import { getDb } from '../../apps/api/src/db/client.js' + +type Db = ReturnType +type Fixture = { + siteId: string + entryId: string + taxonomyId: string + termId: string + termSourceId: string +} + +// 建链(FK 依赖顺序):site → content_entry → taxonomy → term(默认无 parent,层级在各测试内自建)。 +async function setupTaxonomyFixture(db: Db): Promise { + const rand = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}` + const site = (await db.execute(`insert into sites (id, key, name, default_locale, production_origin) + values (gen_random_uuid(), 'tax-${rand}', 'TaxFixture', 'en', 'https://tax.example') returning id`)) as any + const siteId = site[0].id + const entry = (await db.execute(`insert into content_entries (id, site_id, type_key, lifecycle_status) + values (gen_random_uuid(), '${siteId}', 'post', 'active') returning id`)) as any + const tax = (await db.execute(`insert into taxonomies (id, site_id, key, object_type, public_base_path) + values (gen_random_uuid(), '${siteId}', 'category', 'post', '/category') returning id`)) as any + const term = (await db.execute(`insert into terms (id, site_id, taxonomy_id, source_id, slug, name, data) + values (gen_random_uuid(), '${siteId}', '${tax[0].id}', 'term-${rand}', 'cat-${rand}', 'Cat ${rand}', '{}') returning id, source_id`)) as any + return { + siteId, + entryId: entry[0].id, + taxonomyId: tax[0].id, + termId: term[0].id, + termSourceId: term[0].source_id, + } +} + +// 反序清理:content_terms → terms(自引用 FK 为 NO ACTION,同站全量单条 DELETE 在语句末检查时已无悬挂引用)→ +// taxonomies → content_entries → site。 +async function cleanupTaxonomyFixture(db: Db, f: Fixture) { + await db.execute(`delete from content_terms where site_id = '${f.siteId}'`) + await db.execute(`delete from terms where site_id = '${f.siteId}'`) + await db.execute(`delete from taxonomies where site_id = '${f.siteId}'`) + await db.execute(`delete from content_entries where site_id = '${f.siteId}'`) + await db.execute(`delete from sites where id = '${f.siteId}'`) +} + +async function withTaxonomyFixture(fn: (db: Db, f: Fixture) => Promise) { + const db = getDb() + const f = await setupTaxonomyFixture(db) + try { + await fn(db, f) + } finally { + await cleanupTaxonomyFixture(db, f) + await (db as any).$client.end() + } +} + +// drizzle 会把 PostgresError 包成 DrizzleQueryError(message 只有 "Failed query: ..."), +// 服务端原始错误文本在 cause 里——断言约束身份时必须看 cause。 +async function rejectionMessage(db: Db, query: string): Promise { + try { + await db.execute(query) + } catch (e: any) { + return String(e?.cause?.message ?? e?.message ?? e) + } + throw new Error(`expected query to be rejected: ${query}`) +} + +describe('分类域约束', () => { + it('taxonomies UNIQUE(site_id, key, object_type):同三元组重复插入拒绝,换 object_type 放行', async () => { + await withTaxonomyFixture(async (db, f) => { + expect(await rejectionMessage(db, `insert into taxonomies (id, site_id, key, object_type, public_base_path) + values (gen_random_uuid(), '${f.siteId}', 'category', 'post', '/category')`)) + .toMatch(/taxonomies_uq/) + // 对照组:同 site + 同 key + 换 object_type → 允许(category 可同时服务 post 与 product 两类对象) + await db.execute(`insert into taxonomies (id, site_id, key, object_type, public_base_path) + values (gen_random_uuid(), '${f.siteId}', 'category', 'product', '/product-category')`) + }) + }) + + it('terms UNIQUE(site_id, taxonomy_id, source_id):同三元组重复插入拒绝,换 source_id 放行', async () => { + await withTaxonomyFixture(async (db, f) => { + expect(await rejectionMessage(db, `insert into terms (id, site_id, taxonomy_id, source_id, slug, name, data) + values (gen_random_uuid(), '${f.siteId}', '${f.taxonomyId}', '${f.termSourceId}', 'dup', 'Dup', '{}')`)) + .toMatch(/terms_identity_uq/) + // 对照组:同 taxonomy 换 source_id → 允许(唯一键打在来源身份三元组上,非 taxonomy 级唯一) + await db.execute(`insert into terms (id, site_id, taxonomy_id, source_id, slug, name, data) + values (gen_random_uuid(), '${f.siteId}', '${f.taxonomyId}', 'term-other-${f.termSourceId}', 'other', 'Other', '{}')`) + }) + }) + + it('content_terms 复合主键 (site_id, content_entry_id, term_id):同三元组重复插入拒绝', async () => { + await withTaxonomyFixture(async (db, f) => { + await db.execute(`insert into content_terms (site_id, content_entry_id, term_id, is_primary) + values ('${f.siteId}', '${f.entryId}', '${f.termId}', false)`) + expect(await rejectionMessage(db, `insert into content_terms (site_id, content_entry_id, term_id, is_primary) + values ('${f.siteId}', '${f.entryId}', '${f.termId}', true)`)) + .toMatch(/content_terms_site_id_content_entry_id_term_id_pk/) + }) + }) + + it('terms.parent_term_id 自引用 FK:指向不存在 term 的插入被拒绝,指向真实 term 放行', async () => { + await withTaxonomyFixture(async (db, f) => { + expect(await rejectionMessage(db, `insert into terms (id, site_id, taxonomy_id, source_id, slug, name, parent_term_id, data) + values (gen_random_uuid(), '${f.siteId}', '${f.taxonomyId}', 'orphan-1', 'orphan', 'Orphan', gen_random_uuid(), '{}')`)) + .toMatch(/terms_parent_term_id_terms_id_fk/) + // 对照组:parent 指向本站真实 term → 允许(层级是自引用 FK 的合法用法,非空 parent 不影响唯一键) + await db.execute(`insert into terms (id, site_id, taxonomy_id, source_id, slug, name, parent_term_id, data) + values (gen_random_uuid(), '${f.siteId}', '${f.taxonomyId}', 'child-1', 'child', 'Child', '${f.termId}', '{}')`) + }) + }) + + it('content_terms.is_primary 布尔列存在且可写(Yoast 主分类)', async () => { + await withTaxonomyFixture(async (db, f) => { + const col = (await db.execute(`select data_type from information_schema.columns + where table_name = 'content_terms' and column_name = 'is_primary'`)) as any + expect(col[0].data_type).toBe('boolean') + await db.execute(`insert into content_terms (site_id, content_entry_id, term_id, is_primary) + values ('${f.siteId}', '${f.entryId}', '${f.termId}', true)`) + const before = (await db.execute(`select is_primary from content_terms + where site_id = '${f.siteId}' and content_entry_id = '${f.entryId}'`)) as any + expect(before[0].is_primary).toBe(true) + await db.execute(`update content_terms set is_primary = false + where site_id = '${f.siteId}' and content_entry_id = '${f.entryId}'`) + const after = (await db.execute(`select is_primary from content_terms + where site_id = '${f.siteId}' and content_entry_id = '${f.entryId}'`)) as any + expect(after[0].is_primary).toBe(false) + }) + }) +})