Summary
Add a pre-migration audit script that detects any pre-existing duplicate email values
in the users collection. ADR-0001 plans to flip the compound
{email:1, regionId:1} UNIQUE index to a single-field {email:1} UNIQUE; this
script must return an empty result before the migration is safe to execute.
Spec
The decision lives at https://github.com/cargoffer/education_backend/blob/feat/adr-0001-global-email-uniqueness/docs/adr/0001-global-email-uniqueness.md. The audit step maps to step § Implementation Plan → 1. Pre-migration audit of the ADR.
Acceptance criteria
Out of scope
- Migration 002 itself (issue B).
- Schema or controller changes.
Dependencies
References
Summary
Add a pre-migration audit script that detects any pre-existing duplicate email values
in the
userscollection. ADR-0001 plans to flip the compound{email:1, regionId:1}UNIQUE index to a single-field{email:1}UNIQUE; thisscript must return an empty result before the migration is safe to execute.
Spec
The decision lives at https://github.com/cargoffer/education_backend/blob/feat/adr-0001-global-email-uniqueness/docs/adr/0001-global-email-uniqueness.md. The audit step maps to step § Implementation Plan → 1. Pre-migration audit of the ADR.
Acceptance criteria
backend/scripts/audit-email-duplicates.tsthat connects to the configured MongoDB (usingsrc/config/env.ts).db.users.aggregate([{ $group: { _id: '$email', count: { $sum: 1 }, docs: { $push: { _id: '$_id', regionId: '$regionId', role: '$role' } } } }, { $match: { count: { $gt: 1 } } }]).stdout: human-readable table grouped by email.backend/docs/migrations/0001/email-duplicates-audit.json: machine-readable array.Out of scope
Dependencies
References
fix(auth): reject duplicate emails in registration)