Skip to content

Commit

Permalink
Fix column names, add comments to migration
Browse files Browse the repository at this point in the history
  • Loading branch information
wilwong89 committed Sep 4, 2024
1 parent 8aac4e2 commit 591b698
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
15 changes: 8 additions & 7 deletions app/src/db/migrations/20240828000000_008-user-id-constraint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ import type { Knex } from 'knex';
export async function up(knex: Knex): Promise<void> {
return (
Promise.resolve()
// Drop public schema functions
// Find all duplicated user entries in "user" table (by querying by "identity_id")
// Then delete the most recent duplicated entry
.then(() =>
knex.raw(`DELETE FROM "public"."user" AS u
WHERE u."userId" IN (
SELECT DISTINCT ON ("public"."user"."identityId") "public"."user"."userId"
WHERE u."user_id" IN (
SELECT DISTINCT ON ("public"."user"."identity_id") "public"."user"."user_id"
FROM "public"."user"
WHERE (
SELECT count(*)
FROM "public"."user" AS user2
WHERE user2."identityId" = "public"."user"."identityId") > 1
ORDER BY "public"."user"."identityId", "public"."user"."createdAt" DESC
WHERE user2."identity_id" = "public"."user"."identity_id") > 1
ORDER BY "public"."user"."identity_id", "public"."user"."created_at" DESC
)`)
)

// prevent further duplicate users - add unique index over columns identityId and idp in user table
.then(() =>
knex.schema.alterTable('user', (table) => {
table.unique(['identityId', 'idp']);
table.unique(['identity_id', 'idp']);
})
)
);
Expand All @@ -30,7 +31,7 @@ export async function up(knex: Knex): Promise<void> {
export async function down(knex: Knex): Promise<void> {
return Promise.resolve().then(() =>
knex.schema.alterTable('user', (table) => {
table.dropUnique(['identityId', 'idp']);
table.dropUnique(['identity_id', 'idp']);
})
);
}
1 change: 1 addition & 0 deletions app/src/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ model user {
submission submission[]
identity_provider identity_provider? @relation(fields: [idp], references: [idp], onDelete: Cascade, map: "user_idp_foreign")
@@unique([identity_id, idp], map: "user_identity_id_idp_unique")
@@index([email], map: "user_email_index")
@@index([identity_id], map: "user_identity_id_index")
@@index([username], map: "user_username_index")
Expand Down

0 comments on commit 591b698

Please sign in to comment.