Remove users with duplicate emails

This commit is contained in:
Luke Vella 2022-04-14 13:04:47 +01:00
parent bb7f9fa348
commit 1e466718fc
4 changed files with 79 additions and 28 deletions

View file

@ -0,0 +1,43 @@
-- AlterTable
ALTER TABLE "Comment" ADD COLUMN "updatedAt" TIMESTAMP(3);
-- AlterTable
ALTER TABLE "Option" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN "updatedAt" TIMESTAMP(3);
-- AlterTable
ALTER TABLE "Participant" ADD COLUMN "updatedAt" TIMESTAMP(3);
-- AlterTable
ALTER TABLE "User" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN "updatedAt" TIMESTAMP(3);
-- AlterTable
ALTER TABLE "Vote" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN "updatedAt" TIMESTAMP(3);
-- We need to get rid of duplicate email addresses in the users table
-- because the index was previously case sensitive
-- First we update all polls created by users with a duplicate email address
-- to a single user
UPDATE "Poll" p SET "userId" = u.id
FROM (
SELECT min(id) id, array_agg(id) as "userIds"
FROM "User" u
GROUP BY lower(email)
HAVING count(*) > 1
) u
WHERE p."userId" = any(u."userIds")
AND p."userId" <> u.id;
-- Remove all users that do not have polls
DELETE FROM "User" u
WHERE NOT EXISTS (SELECT * FROM "Poll" p WHERE u.id = p."userId");
-- Add citext extension
CREATE EXTENSION citext;
-- Change email to citext
ALTER TABLE "User"
ALTER COLUMN email TYPE citext;

View file

@ -15,7 +15,7 @@
"@headlessui/react": "^1.5.0", "@headlessui/react": "^1.5.0",
"@next/bundle-analyzer": "^12.1.0", "@next/bundle-analyzer": "^12.1.0",
"@popperjs/core": "^2.11.4", "@popperjs/core": "^2.11.4",
"@prisma/client": "^3.11.0", "@prisma/client": "^3.12.0",
"@sentry/nextjs": "^6.19.3", "@sentry/nextjs": "^6.19.3",
"@svgr/webpack": "^6.2.1", "@svgr/webpack": "^6.2.1",
"@tailwindcss/forms": "^0.4.0", "@tailwindcss/forms": "^0.4.0",
@ -35,7 +35,7 @@
"next-i18next": "^10.5.0", "next-i18next": "^10.5.0",
"next-plausible": "^3.1.9", "next-plausible": "^3.1.9",
"nodemailer": "^6.7.2", "nodemailer": "^6.7.2",
"prisma": "^3.11.0", "prisma": "^3.12.0",
"react": "17.0.2", "react": "17.0.2",
"react-big-calendar": "^0.38.9", "react-big-calendar": "^0.38.9",
"react-dom": "17.0.2", "react-dom": "17.0.2",

View file

@ -10,7 +10,9 @@ generator client {
model User { model User {
id String @id @default(cuid()) id String @id @default(cuid())
name String name String
email String @unique email String @unique() @db.Citext
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
polls Poll[] polls Poll[]
participants Participant[] participants Participant[]
comments Comment[] comments Comment[]
@ -72,6 +74,7 @@ model Participant {
pollId String pollId String
votes Vote[] votes Vote[]
createdAt DateTime @default(now()) createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
@@unique([id, pollId]) @@unique([id, pollId])
} }
@ -80,6 +83,8 @@ model Option {
value String value String
pollId String pollId String
poll Poll @relation(fields: [pollId], references: [urlId], onDelete: Cascade) poll Poll @relation(fields: [pollId], references: [urlId], onDelete: Cascade)
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
votes Vote[] votes Vote[]
} }
@ -91,6 +96,8 @@ model Vote {
optionId String optionId String
poll Poll @relation(fields: [pollId], references: [urlId], onDelete: Cascade) poll Poll @relation(fields: [pollId], references: [urlId], onDelete: Cascade)
pollId String pollId String
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
} }
model Comment { model Comment {
@ -102,6 +109,7 @@ model Comment {
user User? @relation(fields: [userId], references: [id]) user User? @relation(fields: [userId], references: [id])
userId String? userId String?
createdAt DateTime @default(now()) createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
@@unique([id, pollId]) @@unique([id, pollId])
} }

View file

@ -1334,22 +1334,22 @@
resolved "https://registry.npmjs.org/@popperjs/core/-/core-2.9.2.tgz" resolved "https://registry.npmjs.org/@popperjs/core/-/core-2.9.2.tgz"
integrity sha512-VZMYa7+fXHdwIq1TDhSXoVmSPEGM/aa+6Aiq3nVVJ9bXr24zScr+NlKFKC3iPljA7ho/GAZr+d2jOf5GIRC30Q== integrity sha512-VZMYa7+fXHdwIq1TDhSXoVmSPEGM/aa+6Aiq3nVVJ9bXr24zScr+NlKFKC3iPljA7ho/GAZr+d2jOf5GIRC30Q==
"@prisma/client@^3.11.0": "@prisma/client@^3.12.0":
version "3.11.0" version "3.12.0"
resolved "https://registry.yarnpkg.com/@prisma/client/-/client-3.11.0.tgz#6aa05165b641578c74816aaa15b389e0062318ee" resolved "https://registry.yarnpkg.com/@prisma/client/-/client-3.12.0.tgz#a0eb49ffea5c128dd11dffb896d7139a60073d12"
integrity sha512-d42o/tlalaWMmNOR4r5BiR6YYTYEV82eZ2lNKOm5ht3WyYwI9e+zy2MyZnNO4Fx5e08RAhW+GRVcEgKl5faUaQ== integrity sha512-4NEQjUcWja/NVBvfuDFscWSk1/rXg3+wj+TSkqXCb1tKlx/bsUE00rxsvOvGg7VZ6lw1JFpGkwjwmsOIc4zvQw==
dependencies: dependencies:
"@prisma/engines-version" "3.11.0-48.b371888aaf8f51357c7457d836b86d12da91658b" "@prisma/engines-version" "3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980"
"@prisma/engines-version@3.11.0-48.b371888aaf8f51357c7457d836b86d12da91658b": "@prisma/engines-version@3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980":
version "3.11.0-48.b371888aaf8f51357c7457d836b86d12da91658b" version "3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980"
resolved "https://registry.yarnpkg.com/@prisma/engines-version/-/engines-version-3.11.0-48.b371888aaf8f51357c7457d836b86d12da91658b.tgz#808ab9636184e4a0b2bf79016d6ed72c4a1ed54f" resolved "https://registry.yarnpkg.com/@prisma/engines-version/-/engines-version-3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980.tgz#829ca3d9d0d92555f44644606d4edfd45b2f5886"
integrity sha512-bhMW1XybXZyqCf+9QqjP7Oi7xgVHcISVyOZNMm51qeZsy12M1RtHaCcXUFeMMV0JOCZZuPFVr3+0KVpQqK35CQ== integrity sha512-o+jo8d7ZEiVpcpNWUDh3fj2uPQpBxl79XE9ih9nkogJbhw6P33274SHnqheedZ7PyvPIK/mvU8MLNYgetgXPYw==
"@prisma/engines@3.11.0-48.b371888aaf8f51357c7457d836b86d12da91658b": "@prisma/engines@3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980":
version "3.11.0-48.b371888aaf8f51357c7457d836b86d12da91658b" version "3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980"
resolved "https://registry.yarnpkg.com/@prisma/engines/-/engines-3.11.0-48.b371888aaf8f51357c7457d836b86d12da91658b.tgz#4c1093f7b24c433a9cef2e60f13f57f7c89b5cfa" resolved "https://registry.yarnpkg.com/@prisma/engines/-/engines-3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980.tgz#e52e364084c4d05278f62768047b788665e64a45"
integrity sha512-m9iZd5F5vP6A2IvKWfHpOO/qK8OOO9nbsV/pdyEkF/1WNe0E8SIWFBKb+HcMLkG9OFbDDBy8QItXmp/mIULuwQ== integrity sha512-zULjkN8yhzS7B3yeEz4aIym4E2w1ChrV12i14pht3ePFufvsAvBSoZ+tuXMvfSoNTgBS5E4bolRzLbMmbwkkMQ==
"@restart/hooks@^0.3.25": "@restart/hooks@^0.3.25":
version "0.3.26" version "0.3.26"
@ -4764,12 +4764,12 @@ pretty-format@^27.2.5, pretty-format@^27.5.1:
ansi-styles "^5.0.0" ansi-styles "^5.0.0"
react-is "^17.0.1" react-is "^17.0.1"
prisma@^3.11.0: prisma@^3.12.0:
version "3.11.0" version "3.12.0"
resolved "https://registry.yarnpkg.com/prisma/-/prisma-3.11.0.tgz#0e384e9d36785711b579fda60117a2d41f79c72c" resolved "https://registry.yarnpkg.com/prisma/-/prisma-3.12.0.tgz#9675e0e72407122759d3eadcb6d27cdccd3497bd"
integrity sha512-8SdsLPhKR3mOfoo2o73h9mNn3v5kA/RqGA26Sv6qDS78Eh2uepPqt5e8/nwj5EOblYm5HEGuitaXQrOCLb6uTw== integrity sha512-ltCMZAx1i0i9xuPM692Srj8McC665h6E5RqJom999sjtVSccHSD8Z+HSdBN2183h9PJKvC5dapkn78dd0NWMBg==
dependencies: dependencies:
"@prisma/engines" "3.11.0-48.b371888aaf8f51357c7457d836b86d12da91658b" "@prisma/engines" "3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980"
process-nextick-args@~2.0.0: process-nextick-args@~2.0.0:
version "2.0.1" version "2.0.1"