Skip to content

fix(data): stop rebuilding collation indexes on every startup - #419

Merged
omegascorp merged 1 commit into
modelence:mainfrom
poojagosika:fix/index-collation-compare
Oct 19, 2026
Merged

omegascorp merged 1 commit into
modelence:mainfrom
poojagosika:fix/index-collation-compare

Conversation

@poojagosika

@poojagosika poojagosika commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #420

Problem

On every startup, createIndexes drops and rebuilds any index declared with a collation, even when nothing changed. That includes the built-in unique users indexes on handle and emails.address.

MongoDB's listIndexes reports a collation fully expanded, with every option plus the ICU version:

{ locale: 'en', caseLevel: false, caseFirst: 'off', strength: 2, numericOrdering: false, alternate: 'non-ignorable', maxVariable: 'punct', normalization: false, backwards: false, version: '57.1' }

The code declares only { locale: 'en', strength: 2 }. isSameIndexDefinition deep-compares the two, so they never match and the index always looks stale.

With the default background creation mode, the drop runs under the lock and the rebuild runs later in the background. Until the rebuild finishes:

  • nothing enforces case-insensitive handle and email uniqueness, which is the signup race the emails.address index exists to prevent;
  • collation lookups fall back to collection scans;
  • if a duplicate slips in, the rebuild fails and is only logged as a warning.

You can check it on any existing app with db.users.getIndexes().

Fix

Compare only the collation fields the index declares (isSameCollation in src/data/store.ts). All other index options are still compared exactly as before.

Trade-off: removing a field from a declared collation (for example deleting strength to go back to the default) no longer triggers a rebuild on its own. Changing a declared field's value still does.

Tests

Two new tests in src/data/store.test.ts, following the existing createIndexes ones:

  • "keeps a collation index that MongoDB reports with its expanded collation": fails on main, passes with the fix.
  • "rebuilds a collation index when a declared collation field changes": checks that a real change still drops and recreates the index.

npm run lint:check, npm run format:check and npm run test all pass (116 files, 1388 tests).

MongoDB reports an index's collation fully expanded (every option plus the ICU
version), while code declares only the fields it sets, e.g. { locale: 'en',
strength: 2 }. The deep-equal comparison never matched, so every startup
dropped and rebuilt unchanged collation indexes, including the unique
users indexes on handle and emails.address. Compare only the collation
fields the code declares.

@omegascorp omegascorp left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good, thank you

@omegascorp
omegascorp merged commit a1c2054 into modelence:main Oct 19, 2026
4 checks passed
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.

Collation indexes (incl. users handle/email) are dropped and rebuilt on every startup

2 participants