From dfd9e9e33dbdfbf5900262d4f31317e04105ec3a Mon Sep 17 00:00:00 2001 From: shenlan Date: Sun, 10 Aug 2025 11:07:41 +0800 Subject: [PATCH] Ensure unique index for document upserts --- internal/rag/store/store.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/internal/rag/store/store.go b/internal/rag/store/store.go index 9c121ce..6cb439e 100644 --- a/internal/rag/store/store.go +++ b/internal/rag/store/store.go @@ -41,6 +41,13 @@ func EnsureSchema(ctx context.Context, conn *pgx.Conn, dim int, migrate bool) er return err } + // older deployments may lack the unique constraint required for + // upserts. Ensure a unique index exists on (repo,path,chunk_id) so + // the ON CONFLICT clause can work reliably. + if _, err := conn.Exec(ctx, `CREATE UNIQUE INDEX IF NOT EXISTS documents_repo_path_chunk_id_key ON documents (repo, path, chunk_id)`); err != nil { + return err + } + // ensure new columns // content_sha was added after initial releases, so older deployments may // lack the column. Add it when missing to avoid insert errors.