From 242165485d0a60fd41b2917287508d6c76d5fcd3 Mon Sep 17 00:00:00 2001 From: Jason Rasmussen Date: Fri, 9 Dec 2022 22:16:25 -0500 Subject: [PATCH] fix(server): unique email database constraint (#1082) --- server/libs/database/src/entities/user.entity.ts | 2 +- .../1670633210032-AddUserEmailUniqueConstraint.ts | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 server/libs/database/src/migrations/1670633210032-AddUserEmailUniqueConstraint.ts diff --git a/server/libs/database/src/entities/user.entity.ts b/server/libs/database/src/entities/user.entity.ts index 13bfc3d2e0..1d0a659bd9 100644 --- a/server/libs/database/src/entities/user.entity.ts +++ b/server/libs/database/src/entities/user.entity.ts @@ -15,7 +15,7 @@ export class UserEntity { @Column({ default: false }) isAdmin!: boolean; - @Column() + @Column({ unique: true }) email!: string; @Column({ default: '', select: false }) diff --git a/server/libs/database/src/migrations/1670633210032-AddUserEmailUniqueConstraint.ts b/server/libs/database/src/migrations/1670633210032-AddUserEmailUniqueConstraint.ts new file mode 100644 index 0000000000..50a67ae94f --- /dev/null +++ b/server/libs/database/src/migrations/1670633210032-AddUserEmailUniqueConstraint.ts @@ -0,0 +1,14 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddUserEmailUniqueConstraint1670633210032 implements MigrationInterface { + name = 'AddUserEmailUniqueConstraint1670633210032' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "users" ADD CONSTRAINT "UQ_97672ac88f789774dd47f7c8be3" UNIQUE ("email")`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "users" DROP CONSTRAINT "UQ_97672ac88f789774dd47f7c8be3"`); + } + +}