From 1e364f17abfdcc32a34164d4297d46beb36bed18 Mon Sep 17 00:00:00 2001 From: weichen Date: Sun, 13 Sep 2026 11:12:03 +0000 Subject: [PATCH] feat(db): add freeform client_key for idempotent upload registration Persist owner-scoped client_key on freeform_documents so upload create can retry safely without inventing local doc-upload orphans. Co-authored-by: Cursor --- .../00029_2026-09-13-freeform-client-key.sql | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 deploy/postgres/goose/00029_2026-09-13-freeform-client-key.sql diff --git a/deploy/postgres/goose/00029_2026-09-13-freeform-client-key.sql b/deploy/postgres/goose/00029_2026-09-13-freeform-client-key.sql new file mode 100644 index 00000000..2a0ca1b4 --- /dev/null +++ b/deploy/postgres/goose/00029_2026-09-13-freeform-client-key.sql @@ -0,0 +1,18 @@ +-- +goose Up +-- 2026-09-13 freeform upload registration: formal client_key for idempotent server fact-source create +-- Schema 28 → 29 + +ALTER TABLE freeform_documents + ADD COLUMN IF NOT EXISTS client_key VARCHAR(128) NULL; + +COMMENT ON COLUMN freeform_documents.client_key IS + 'Client-supplied idempotency key for direct create (e.g. upload batch id); NULL for message-derived docs'; + +-- Same owner cannot register the same client_key twice while the doc is alive. +CREATE UNIQUE INDEX IF NOT EXISTS uq_freeform_owner_client_key + ON freeform_documents (owner_user_id, client_key) + WHERE is_deleted = FALSE AND client_key IS NOT NULL AND length(trim(client_key)) > 0; + +-- +goose Down +DROP INDEX IF EXISTS uq_freeform_owner_client_key; +ALTER TABLE freeform_documents DROP COLUMN IF EXISTS client_key;