From 9e07ea1a4af198bbbed1c03f263d44b3fc85700b Mon Sep 17 00:00:00 2001 From: Luke Vella Date: Thu, 19 Dec 2024 16:46:32 +0000 Subject: [PATCH] Squashing --- .../migration.sql | 26 +++++++++++++++ .../migration.sql | 11 ------- .../20241218162726_test/migration.sql | 5 --- .../migration.sql | 33 ------------------- .../migration.sql | 21 ++++++++++++ 5 files changed, 47 insertions(+), 49 deletions(-) rename packages/database/prisma/migrations/{20241218160237_create_guest_users_for_participants => 20241218160237_create_guest_users}/migration.sql (50%) delete mode 100644 packages/database/prisma/migrations/20241218162444_participant_user_required/migration.sql delete mode 100644 packages/database/prisma/migrations/20241218162726_test/migration.sql delete mode 100644 packages/database/prisma/migrations/20241219153556_comments_user/migration.sql create mode 100644 packages/database/prisma/migrations/20241219164853_make_user_non_optional/migration.sql diff --git a/packages/database/prisma/migrations/20241218160237_create_guest_users_for_participants/migration.sql b/packages/database/prisma/migrations/20241218160237_create_guest_users/migration.sql similarity index 50% rename from packages/database/prisma/migrations/20241218160237_create_guest_users_for_participants/migration.sql rename to packages/database/prisma/migrations/20241218160237_create_guest_users/migration.sql index ae24a8e0a..a737688a8 100644 --- a/packages/database/prisma/migrations/20241218160237_create_guest_users_for_participants/migration.sql +++ b/packages/database/prisma/migrations/20241218160237_create_guest_users/migration.sql @@ -22,4 +22,30 @@ BEGIN WHERE id = participant_record.id; END LOOP; END; +$$; + +-- Create guest users for comments without user_id and link them +DO $$ +DECLARE + comment_record RECORD; +BEGIN + FOR comment_record IN + SELECT id, author_name + FROM comments + WHERE user_id IS NULL + LOOP + WITH new_user AS ( + INSERT INTO users (id, is_guest, created_at) + VALUES ( + gen_random_uuid()::text, + TRUE, + NOW() + ) + RETURNING id + ) + UPDATE comments + SET user_id = (SELECT id FROM new_user) + WHERE id = comment_record.id; + END LOOP; +END; $$; \ No newline at end of file diff --git a/packages/database/prisma/migrations/20241218162444_participant_user_required/migration.sql b/packages/database/prisma/migrations/20241218162444_participant_user_required/migration.sql deleted file mode 100644 index ff7b0d7ac..000000000 --- a/packages/database/prisma/migrations/20241218162444_participant_user_required/migration.sql +++ /dev/null @@ -1,11 +0,0 @@ -/* - Warnings: - - - Made the column `user_id` on table `participants` required. This step will fail if there are existing NULL values in that column. - -*/ --- AlterTable -ALTER TABLE "participants" ALTER COLUMN "user_id" SET NOT NULL; - --- AddForeignKey -ALTER TABLE "participants" ADD CONSTRAINT "participants_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/packages/database/prisma/migrations/20241218162726_test/migration.sql b/packages/database/prisma/migrations/20241218162726_test/migration.sql deleted file mode 100644 index 984e59734..000000000 --- a/packages/database/prisma/migrations/20241218162726_test/migration.sql +++ /dev/null @@ -1,5 +0,0 @@ --- DropForeignKey -ALTER TABLE "participants" DROP CONSTRAINT "participants_user_id_fkey"; - --- AddForeignKey -ALTER TABLE "participants" ADD CONSTRAINT "participants_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/packages/database/prisma/migrations/20241219153556_comments_user/migration.sql b/packages/database/prisma/migrations/20241219153556_comments_user/migration.sql deleted file mode 100644 index 838d63eb5..000000000 --- a/packages/database/prisma/migrations/20241219153556_comments_user/migration.sql +++ /dev/null @@ -1,33 +0,0 @@ --- Create guest users for comments without user_id and link them -DO $$ -DECLARE - comment_record RECORD; -BEGIN - FOR comment_record IN - SELECT id, author_name - FROM comments - WHERE user_id IS NULL - LOOP - WITH new_user AS ( - INSERT INTO users (id, is_guest, created_at) - VALUES ( - gen_random_uuid()::text, - TRUE, - NOW() - ) - RETURNING id - ) - UPDATE comments - SET user_id = (SELECT id FROM new_user) - WHERE id = comment_record.id; - END LOOP; -END; -$$; --- DropForeignKey -ALTER TABLE "comments" DROP CONSTRAINT "comments_user_id_fkey"; - --- AlterTable -ALTER TABLE "comments" ALTER COLUMN "user_id" SET NOT NULL; - --- AddForeignKey -ALTER TABLE "comments" ADD CONSTRAINT "comments_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/packages/database/prisma/migrations/20241219164853_make_user_non_optional/migration.sql b/packages/database/prisma/migrations/20241219164853_make_user_non_optional/migration.sql new file mode 100644 index 000000000..f4e66cf9a --- /dev/null +++ b/packages/database/prisma/migrations/20241219164853_make_user_non_optional/migration.sql @@ -0,0 +1,21 @@ +/* + Warnings: + + - Made the column `user_id` on table `comments` required. This step will fail if there are existing NULL values in that column. + - Made the column `user_id` on table `participants` required. This step will fail if there are existing NULL values in that column. + +*/ +-- DropForeignKey +ALTER TABLE "comments" DROP CONSTRAINT "comments_user_id_fkey"; + +-- AlterTable +ALTER TABLE "comments" ALTER COLUMN "user_id" SET NOT NULL; + +-- AlterTable +ALTER TABLE "participants" ALTER COLUMN "user_id" SET NOT NULL; + +-- AddForeignKey +ALTER TABLE "participants" ADD CONSTRAINT "participants_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "comments" ADD CONSTRAINT "comments_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;