Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix user email unique constraint #8898

Merged
merged 1 commit into from
Dec 5, 2024
Merged

Conversation

Weiko
Copy link
Member

@Weiko Weiko commented Dec 5, 2024

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

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

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

Modified the user email uniqueness constraint to allow reuse of email addresses for soft-deleted users while maintaining uniqueness for active users.

  • Changed unique constraint in /packages/twenty-server/src/database/typeorm/core/migrations/1733408604468-fixUserEmailUniqueConstraint.ts from composite (email, deletedAt) to partial index on email where deletedAt IS NULL
  • Updated /packages/twenty-server/src/engine/core-modules/user/user.entity.ts to use @Index decorator with WHERE clause instead of @unique for email constraint
  • Migration includes rollback functionality to restore original composite unique constraint if needed

2 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings | Greptile

@Weiko Weiko merged commit c993f2d into main Dec 5, 2024
19 checks passed
@Weiko Weiko deleted the c--fix-user-email-unique-constraint branch December 5, 2024 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants