From b19790f621fd6544f3abdf944edde95697227f76 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 15 Dec 2021 14:56:13 +0000 Subject: [PATCH] fix: backup unique constraint (#795) --- packages/db/postgres/functions.sql | 2 +- packages/db/postgres/tables.sql | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/db/postgres/functions.sql b/packages/db/postgres/functions.sql index d5891c4fad..dba96a8ab1 100644 --- a/packages/db/postgres/functions.sql +++ b/packages/db/postgres/functions.sql @@ -138,7 +138,7 @@ BEGIN values (inserted_upload_id, backup_url, (data ->> 'inserted_at')::timestamptz) - ON CONFLICT ( url ) DO NOTHING; + ON CONFLICT ( upload_id, url ) DO NOTHING; end loop; return (inserted_upload_id)::TEXT; diff --git a/packages/db/postgres/tables.sql b/packages/db/postgres/tables.sql index fe24e2749f..bbe3520be0 100644 --- a/packages/db/postgres/tables.sql +++ b/packages/db/postgres/tables.sql @@ -169,8 +169,9 @@ CREATE TABLE IF NOT EXISTS backup -- Upload that resulted in this backup. upload_id BIGINT NOT NULL REFERENCES upload (id) ON DELETE CASCADE, -- Backup url location. - url TEXT NOT NULL UNIQUE, - inserted_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL + url TEXT NOT NULL, + inserted_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL, + UNIQUE (upload_id, url) ); CREATE INDEX IF NOT EXISTS backup_upload_id_idx ON backup (upload_id);