Fix user email unique constraint (#8898)

## Context
Fix wrong unique index on user email as we don't want an index on
email/deletedAt but a partial on a where condition on deletedAt. This
should enforce email unicity excluding the ones that have a deletedAt

## Test
Run
```sql
SELECT email, COUNT(*) as duplicate_count
FROM core."user"
WHERE "deletedAt" IS NULL
GROUP BY email
HAVING COUNT(*) > 1
ORDER BY duplicate_count DESC;
```
to check duplicates before running the migration
This commit is contained in:
Weiko
2024-12-05 15:54:12 +01:00
committed by GitHub
parent 26ff344f56
commit c993f2de0b
2 changed files with 28 additions and 2 deletions

View File

@ -0,0 +1,23 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class FixUserEmailUniqueConstraint1733408604468
implements MigrationInterface
{
name = 'FixUserEmailUniqueConstraint1733408604468';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "core"."user" DROP CONSTRAINT "UQ_USER_EMAIL"`,
);
await queryRunner.query(
`CREATE UNIQUE INDEX "UQ_USER_EMAIL" ON "core"."user" ("email") WHERE "deletedAt" IS NULL`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX "core"."UQ_USER_EMAIL"`);
await queryRunner.query(
`ALTER TABLE "core"."user" ADD CONSTRAINT "UQ_USER_EMAIL" UNIQUE ("email", "deletedAt")`,
);
}
}