Skip to content

[_]: fix(migrations): add partial unique indexes for mail accounts, addresses, and provider accounts#85

Open
jzunigax2 wants to merge 1 commit into
masterfrom
fix/partial-unique-indexes
Open

[_]: fix(migrations): add partial unique indexes for mail accounts, addresses, and provider accounts#85
jzunigax2 wants to merge 1 commit into
masterfrom
fix/partial-unique-indexes

Conversation

@jzunigax2

@jzunigax2 jzunigax2 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor
  • Created migration files to implement partial unique indexes on mail_accounts, mail_addresses, and mail_provider_accounts tables.
  • Updated the AccountService to handle unique constraint violations more effectively, ensuring proper conflict handling during account provisioning.
  • Enhanced model definitions to include the new unique indexes, allowing for better management of soft-deleted records.

Summary by CodeRabbit

  • New Features

    • Added support for soft-deleted records while keeping active account, address, and provider-account entries unique.
    • Account provisioning now handles duplicate-account races more gracefully and returns a clear conflict when needed.
  • Bug Fixes

    • Prevented duplicate active mail accounts, mail addresses, and provider accounts from being created.
    • Improved handling when account creation conflicts with an existing record.
  • Tests

    • Added coverage for the duplicate-account provisioning conflict scenario.

…ses, and provider accounts

- Created migration files to implement partial unique indexes on `mail_accounts`, `mail_addresses`, and `mail_provider_accounts` tables.
- Updated the `AccountService` to handle unique constraint violations more effectively, ensuring proper conflict handling during account provisioning.
- Enhanced model definitions to include the new unique indexes, allowing for better management of soft-deleted records.
@jzunigax2 jzunigax2 added the enhancement New feature or request label Jul 8, 2026
@jzunigax2 jzunigax2 requested a review from xabg2 July 8, 2026 20:42
@jzunigax2 jzunigax2 self-assigned this Jul 8, 2026
@sonarqubecloud

sonarqubecloud Bot commented Jul 8, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
16.7% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: abc58018-fbe5-4e5d-9372-059c48520f8f

📥 Commits

Reviewing files that changed from the base of the PR and between 1e57acc and dabad3e.

📒 Files selected for processing (8)
  • migrations/20260708134528-mail-accounts-partial-unique-user-id.js
  • migrations/20260708134529-mail-addresses-partial-unique-address.js
  • migrations/20260708134530-mail-provider-accounts-partial-unique.js
  • src/modules/account/account.service.spec.ts
  • src/modules/account/account.service.ts
  • src/modules/account/models/mail-account.model.ts
  • src/modules/account/models/mail-address.model.ts
  • src/modules/account/models/mail-provider-account.model.ts

📝 Walkthrough

Walkthrough

Migrations and Sequelize models replace unconditional unique constraints on mail_accounts.user_id, mail_addresses.address, and mail_provider_accounts (mail_address_id, provider+external_id) with partial unique indexes scoped to non-deleted rows. provisionAccount's unique-constraint conflict handling is updated with a new test.

Changes

Partial Unique Constraints Migration

Layer / File(s) Summary
mail_accounts partial unique constraint
migrations/20260708134528-mail-accounts-partial-unique-user-id.js, src/modules/account/models/mail-account.model.ts
Migration drops existing unique constraints and creates a partial unique index on user_id where deleted_at IS NULL, with a matching rollback; model moves uniqueness from @Unique to a table-level partial index and adds @AllowNull(false) to userId.
mail_addresses partial unique constraint
migrations/20260708134529-mail-addresses-partial-unique-address.js, src/modules/account/models/mail-address.model.ts
Migration replaces mail_addresses_address_key with a partial unique index mail_addresses_address_active_unique scoped to non-deleted rows, with rollback; model removes @Unique on address and adds a matching table-level partial index.
mail_provider_accounts partial unique constraints
migrations/20260708134530-mail-provider-accounts-partial-unique.js, src/modules/account/models/mail-provider-account.model.ts
Migration replaces unique constraints on mail_address_id and (provider, external_id) with partial unique indexes for non-deleted rows, with rollback; model removes @Unique on mailAddressId and defines both partial indexes at the table level.
provisionAccount conflict handling
src/modules/account/account.service.ts, src/modules/account/account.service.spec.ts
On SequelizeUniqueConstraintError, the service now calls findByUserId and returns the existing account if found, otherwise throws ConflictException; a new test verifies this behavior and that createAccount is not called.

Estimated code review effort: 3 (Moderate) | ~25 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding partial unique indexes for the mail account, address, and provider account tables.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/partial-unique-indexes

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant