diff --git a/packages/database/prisma/migrations/20241216173316_make_name_email_optional/migration.sql b/packages/database/prisma/migrations/20241216173316_make_name_email_optional/migration.sql new file mode 100644 index 000000000..d51541de3 --- /dev/null +++ b/packages/database/prisma/migrations/20241216173316_make_name_email_optional/migration.sql @@ -0,0 +1,3 @@ +-- AlterTable +ALTER TABLE "users" ALTER COLUMN "name" DROP NOT NULL, +ALTER COLUMN "email" DROP NOT NULL; diff --git a/packages/database/prisma/migrations/20241216173702_add_is_guest_column/migration.sql b/packages/database/prisma/migrations/20241216173702_add_is_guest_column/migration.sql new file mode 100644 index 000000000..87ec783ab --- /dev/null +++ b/packages/database/prisma/migrations/20241216173702_add_is_guest_column/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "users" ADD COLUMN "is_guest" BOOLEAN NOT NULL DEFAULT false; diff --git a/packages/database/prisma/migrations/20241217102725_create_guest_users/migration.sql b/packages/database/prisma/migrations/20241217102725_create_guest_users/migration.sql new file mode 100644 index 000000000..358285ea3 --- /dev/null +++ b/packages/database/prisma/migrations/20241217102725_create_guest_users/migration.sql @@ -0,0 +1,49 @@ +DO $$ +DECLARE + batch_size INTEGER := 10000; + total_rows INTEGER; + offset_count INTEGER := 0; +BEGIN + -- First create a temporary table with all distinct user_ids + CREATE TEMP TABLE missing_users AS + WITH all_users AS ( + SELECT user_id FROM polls + UNION + SELECT user_id FROM comments WHERE user_id IS NOT NULL + UNION + SELECT user_id FROM participants WHERE user_id IS NOT NULL + ) + SELECT DISTINCT user_id + FROM all_users a + WHERE NOT EXISTS ( + SELECT 1 FROM users u WHERE u.id = a.user_id + ); + + -- Get the count + SELECT COUNT(*) INTO total_rows FROM missing_users; + + RAISE NOTICE 'Starting migration of % missing users', total_rows; + + -- Process in batches + WHILE offset_count < total_rows LOOP + INSERT INTO users (id, is_guest, created_at) + SELECT user_id as id, true as is_guest, NOW() as created_at + FROM missing_users + OFFSET offset_count + LIMIT batch_size + ON CONFLICT (id) DO NOTHING; + + offset_count := offset_count + batch_size; + + IF offset_count % 50000 = 0 THEN + RAISE NOTICE 'Processed up to row %', offset_count; + END IF; + + COMMIT; + END LOOP; + + -- Clean up + DROP TABLE missing_users; + + RAISE NOTICE 'Migration complete. Processed % rows', offset_count; +END $$; \ No newline at end of file diff --git a/packages/database/prisma/migrations/20241217113050_remove_orphaned_rows/migration.sql b/packages/database/prisma/migrations/20241217113050_remove_orphaned_rows/migration.sql new file mode 100644 index 000000000..84bf8a912 --- /dev/null +++ b/packages/database/prisma/migrations/20241217113050_remove_orphaned_rows/migration.sql @@ -0,0 +1,34 @@ +-- Delete votes that reference non-existent polls +DELETE FROM votes v +WHERE NOT EXISTS ( + SELECT 1 FROM polls p + WHERE p.id = v.poll_id +); + +-- Delete comments that reference non-existent polls +DELETE FROM comments c +WHERE NOT EXISTS ( + SELECT 1 FROM polls p + WHERE p.id = c.poll_id +); + +-- Delete participants that reference non-existent polls +DELETE FROM participants p +WHERE NOT EXISTS ( + SELECT 1 FROM polls poll + WHERE poll.id = p.poll_id +); + +-- Delete options that reference non-existent polls +DELETE FROM options o +WHERE NOT EXISTS ( + SELECT 1 FROM polls p + WHERE p.id = o.poll_id +); + +-- Delete watchers that reference non-existent polls +DELETE FROM watchers w +WHERE NOT EXISTS ( + SELECT 1 FROM polls p + WHERE p.id = w.poll_id +); diff --git a/packages/database/prisma/migrations/20241217113516_update_relation_mode/migration.sql b/packages/database/prisma/migrations/20241217113516_update_relation_mode/migration.sql new file mode 100644 index 000000000..fe39f6890 --- /dev/null +++ b/packages/database/prisma/migrations/20241217113516_update_relation_mode/migration.sql @@ -0,0 +1,71 @@ +-- DropIndex +DROP INDEX "accounts_user_id_idx"; + +-- DropIndex +DROP INDEX "comments_poll_id_idx"; + +-- DropIndex +DROP INDEX "comments_user_id_idx"; + +-- DropIndex +DROP INDEX "events_user_id_idx"; + +-- DropIndex +DROP INDEX "options_poll_id_idx"; + +-- DropIndex +DROP INDEX "participants_poll_id_idx"; + +-- DropIndex +DROP INDEX "polls_user_id_idx"; + +-- DropIndex +DROP INDEX "votes_option_id_idx"; + +-- DropIndex +DROP INDEX "votes_participant_id_idx"; + +-- DropIndex +DROP INDEX "votes_poll_id_idx"; + +-- DropIndex +DROP INDEX "watchers_poll_id_idx"; + +-- DropIndex +DROP INDEX "watchers_user_id_idx"; + +-- AddForeignKey +ALTER TABLE "accounts" ADD CONSTRAINT "accounts_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "users" ADD CONSTRAINT "users_subscription_id_fkey" FOREIGN KEY ("subscription_id") REFERENCES "subscriptions"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "polls" ADD CONSTRAINT "polls_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "polls" ADD CONSTRAINT "polls_event_id_fkey" FOREIGN KEY ("event_id") REFERENCES "events"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "events" ADD CONSTRAINT "events_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "watchers" ADD CONSTRAINT "watchers_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "watchers" ADD CONSTRAINT "watchers_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "polls"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "participants" ADD CONSTRAINT "participants_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "polls"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "options" ADD CONSTRAINT "options_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "polls"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "votes" ADD CONSTRAINT "votes_participant_id_fkey" FOREIGN KEY ("participant_id") REFERENCES "participants"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "comments" ADD CONSTRAINT "comments_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "polls"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "comments" ADD CONSTRAINT "comments_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/packages/database/prisma/schema.prisma b/packages/database/prisma/schema.prisma index 79223bca4..366447e94 100644 --- a/packages/database/prisma/schema.prisma +++ b/packages/database/prisma/schema.prisma @@ -2,7 +2,6 @@ datasource db { provider = "postgresql" url = env("DATABASE_URL") directUrl = env("DIRECT_DATABASE_URL") - relationMode = "prisma" } generator client { @@ -34,15 +33,15 @@ model Account { user User @relation(fields: [userId], references: [id]) @@unique([provider, providerAccountId]) - @@index([userId], type: Hash) @@map("accounts") } model User { id String @id @default(cuid()) - name String - email String @unique() @db.Citext - emailVerified DateTime? @map("email_verified") + name String? + email String? @unique() @db.Citext + emailVerified DateTime? @map("email_verified") + isGuest Boolean @default(false) @map("is_guest") image String? timeZone String? @map("time_zone") weekStart Int? @map("week_start") @@ -146,7 +145,6 @@ model Poll { watchers Watcher[] comments Comment[] - @@index([userId], type: Hash) @@map("polls") } @@ -162,7 +160,6 @@ model Event { poll Poll? - @@index([userId], type: Hash) @@map("events") } @@ -174,8 +171,6 @@ model Watcher { createdAt DateTime @default(now()) @map("created_at") poll Poll @relation(fields: [pollId], references: [id]) - @@index([userId], type: Hash) - @@index([pollId], type: Hash) @@map("watchers") } @@ -193,7 +188,6 @@ model Participant { deleted Boolean @default(false) deletedAt DateTime? @map("deleted_at") - @@index([pollId], type: Hash) @@map("participants") } @@ -205,7 +199,6 @@ model Option { poll Poll @relation(fields: [pollId], references: [id]) createdAt DateTime @default(now()) @map("created_at") - @@index([pollId], type: Hash) @@map("options") } @@ -227,9 +220,6 @@ model Vote { createdAt DateTime @default(now()) @map("created_at") updatedAt DateTime? @updatedAt @map("updated_at") - @@index([participantId], type: Hash) - @@index([optionId], type: Hash) - @@index([pollId], type: Hash) @@map("votes") } @@ -244,8 +234,6 @@ model Comment { createdAt DateTime @default(now()) @map("created_at") updatedAt DateTime? @updatedAt @map("updated_at") - @@index([userId], type: Hash) - @@index([pollId], type: Hash) @@map("comments") }