From ffb210babbca73a25fb36f7dfb3d7db7e6d9c676 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Wed, 25 Dec 2024 08:02:36 -0800 Subject: [PATCH] server: ensure uniqueness on post links --- .../20241225054216_post-link-unique.down.sql | 3 ++ .../20241225054216_post-link-unique.up.sql | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 server/migrations/20241225054216_post-link-unique.down.sql create mode 100644 server/migrations/20241225054216_post-link-unique.up.sql diff --git a/server/migrations/20241225054216_post-link-unique.down.sql b/server/migrations/20241225054216_post-link-unique.down.sql new file mode 100644 index 0000000..810bde0 --- /dev/null +++ b/server/migrations/20241225054216_post-link-unique.down.sql @@ -0,0 +1,3 @@ +-- Add down migration script here +ALTER TABLE + post DROP CONSTRAINT post_link_key; diff --git a/server/migrations/20241225054216_post-link-unique.up.sql b/server/migrations/20241225054216_post-link-unique.up.sql new file mode 100644 index 0000000..aeb407b --- /dev/null +++ b/server/migrations/20241225054216_post-link-unique.up.sql @@ -0,0 +1,28 @@ +WITH dupes AS ( + SELECT + uid, + link, + Row_number() over( + PARTITION by link + ORDER BY + link + ) AS RowNumber + FROM + post +) +DELETE FROM + post +WHERE + uid IN ( + SELECT + uid + FROM + dupes + WHERE + RowNumber > 1 + ); + +ALTER TABLE + post +ADD + UNIQUE (link);