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

@ -5,11 +5,11 @@ import {
Column,
CreateDateColumn,
Entity,
Index,
ManyToOne,
OneToMany,
PrimaryGeneratedColumn,
Relation,
Unique,
UpdateDateColumn,
} from 'typeorm';
@ -28,7 +28,10 @@ registerEnumType(OnboardingStatus, {
@Entity({ name: 'user', schema: 'core' })
@ObjectType('User')
@Unique('UQ_USER_EMAIL', ['email', 'deletedAt'])
@Index('UQ_USER_EMAIL', ['email'], {
unique: true,
where: '"deletedAt" IS NULL',
})
export class User {
@IDField(() => UUIDScalarType)
@PrimaryGeneratedColumn('uuid')