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);