From e5d94f961e1791b57da831dd3a2316ac2a51ccbd Mon Sep 17 00:00:00 2001 From: liangxu Date: Tue, 26 May 2026 18:43:15 +0800 Subject: [PATCH] feat(publish-dedup): add indexes for publish task dedup and lookup Add a unique index on desktop_tasks.dedup_key for active publish kinds (queued/in_progress/succeeded/unknown) so duplicate publish submissions can be rejected at the DB layer, plus supporting indexes on publish_records and the desktop_tasks publish_record_id payload expression used by the new dedup lookup query. Co-Authored-By: Claude Opus 4.7 (1M context) --- ...26110000_add_publish_task_dedup_index.down.sql | 1 + ...0526110000_add_publish_task_dedup_index.up.sql | 8 ++++++++ ...d_publish_records_active_lookup_index.down.sql | 1 + ...add_publish_records_active_lookup_index.up.sql | 3 +++ ...op_tasks_publish_record_payload_index.down.sql | 1 + ...ktop_tasks_publish_record_payload_index.up.sql | 15 +++++++++++++++ 6 files changed, 29 insertions(+) create mode 100644 server/migrations/20260526110000_add_publish_task_dedup_index.down.sql create mode 100644 server/migrations/20260526110000_add_publish_task_dedup_index.up.sql create mode 100644 server/migrations/20260526110001_add_publish_records_active_lookup_index.down.sql create mode 100644 server/migrations/20260526110001_add_publish_records_active_lookup_index.up.sql create mode 100644 server/migrations/20260526110002_add_desktop_tasks_publish_record_payload_index.down.sql create mode 100644 server/migrations/20260526110002_add_desktop_tasks_publish_record_payload_index.up.sql diff --git a/server/migrations/20260526110000_add_publish_task_dedup_index.down.sql b/server/migrations/20260526110000_add_publish_task_dedup_index.down.sql new file mode 100644 index 0000000..8cd5021 --- /dev/null +++ b/server/migrations/20260526110000_add_publish_task_dedup_index.down.sql @@ -0,0 +1 @@ +DROP INDEX CONCURRENTLY IF EXISTS uq_desktop_tasks_publish_dedup_active; diff --git a/server/migrations/20260526110000_add_publish_task_dedup_index.up.sql b/server/migrations/20260526110000_add_publish_task_dedup_index.up.sql new file mode 100644 index 0000000..7a936d0 --- /dev/null +++ b/server/migrations/20260526110000_add_publish_task_dedup_index.up.sql @@ -0,0 +1,8 @@ +-- Keep succeeded publish tasks in the active dedup set intentionally: the +-- product rule is "one article can be published to an account once"; repeat +-- publishing should use a copied/new article or an explicit admin status reset. +CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS uq_desktop_tasks_publish_dedup_active + ON desktop_tasks (dedup_key) + WHERE kind = 'publish' + AND dedup_key IS NOT NULL + AND status IN ('queued', 'in_progress', 'succeeded', 'unknown'); diff --git a/server/migrations/20260526110001_add_publish_records_active_lookup_index.down.sql b/server/migrations/20260526110001_add_publish_records_active_lookup_index.down.sql new file mode 100644 index 0000000..28ab4f9 --- /dev/null +++ b/server/migrations/20260526110001_add_publish_records_active_lookup_index.down.sql @@ -0,0 +1 @@ +DROP INDEX CONCURRENTLY IF EXISTS idx_publish_records_article_account_active; diff --git a/server/migrations/20260526110001_add_publish_records_active_lookup_index.up.sql b/server/migrations/20260526110001_add_publish_records_active_lookup_index.up.sql new file mode 100644 index 0000000..d83c19f --- /dev/null +++ b/server/migrations/20260526110001_add_publish_records_active_lookup_index.up.sql @@ -0,0 +1,3 @@ +CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_publish_records_article_account_active + ON publish_records (tenant_id, article_id, platform_account_id, updated_at DESC, id DESC) + WHERE status IN ('queued', 'publishing', 'success'); diff --git a/server/migrations/20260526110002_add_desktop_tasks_publish_record_payload_index.down.sql b/server/migrations/20260526110002_add_desktop_tasks_publish_record_payload_index.down.sql new file mode 100644 index 0000000..c829294 --- /dev/null +++ b/server/migrations/20260526110002_add_desktop_tasks_publish_record_payload_index.down.sql @@ -0,0 +1 @@ +DROP INDEX CONCURRENTLY IF EXISTS idx_desktop_tasks_publish_record_payload; diff --git a/server/migrations/20260526110002_add_desktop_tasks_publish_record_payload_index.up.sql b/server/migrations/20260526110002_add_desktop_tasks_publish_record_payload_index.up.sql new file mode 100644 index 0000000..0267832 --- /dev/null +++ b/server/migrations/20260526110002_add_desktop_tasks_publish_record_payload_index.up.sql @@ -0,0 +1,15 @@ +CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_desktop_tasks_publish_record_payload + ON desktop_tasks ( + tenant_id, + workspace_id, + ( + -- Keep this expression aligned with loadExistingPublishRecordTargets. + CASE + WHEN (payload->>'publish_record_id') ~ '^[0-9]+$' + THEN (payload->>'publish_record_id')::bigint + ELSE NULL + END + ) + ) + WHERE kind = 'publish' + AND payload ? 'publish_record_id';