Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,44 +1,16 @@
/**
* Migration: Fix email index to allow multiple null emails
*
* The original email_1 index was { unique: true } which prevents
* multiple users from having email: null (e.g. social/phone login users).
* This changes it to { unique: true, sparse: true } so null values are
* excluded from the uniqueness constraint.
* SUPERSEDED by 20260407000003_fix_email_index_partial.js which uses
* partialFilterExpression instead of sparse. This migration is now a
* no-op β€” it exists only so migrate-mongo records it in the changelog
* and stops retrying it on every deploy.
*/

export const up = async (db, client) => {
const indexes = await db.collection("users").indexes();
const hasEmailIndex = indexes.some((idx) => idx.key?.email === 1);

if (hasEmailIndex) {
// Index already exists (possibly replaced by a later migration) β€” skip
console.log("βœ… email index already exists, skipping sparse migration");
return;
}

// Drop the old non-sparse unique index
await db
.collection("users")
.dropIndex("email_1")
.catch(() => {});

// Recreate as sparse unique β€” allows multiple null emails
await db
.collection("users")
.createIndex({ email: 1 }, { unique: true, sparse: true });

console.log("βœ… Recreated email index as sparse unique");
console.log("βœ… email sparse migration skipped (superseded by 000003 partial filter)");
};

export const down = async (db, client) => {
// Revert to original non-sparse unique index
await db
.collection("users")
.dropIndex("email_1")
.catch(() => {});

await db.collection("users").createIndex({ email: 1 }, { unique: true });

console.log("βœ… Reverted email index to non-sparse unique");
// Nothing to revert β€” this migration is a no-op
};
Loading