EventBuzz_Backend/scripts/drop-legacy-participant-ind...

55 lines
1.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Drop legacy Participant indexes from the pre–multi-event schema.
* The old unique index on { event, email } blocks guest sign-in (no email) because
* MongoDB treats missing fields as null and only allows one { event: null, email: null } row.
*
* Usage (from backend/): node scripts/drop-legacy-participant-indexes.js
*/
require('dotenv').config();
const mongoose = require('mongoose');
const Participant = require('../src/models/Participant');
const LEGACY_INDEX_NAMES = [
'event_1_email_1',
'event_1',
'email_1',
];
async function main() {
const uri = process.env.MONGODB_URI || process.env.MONGO_URI;
if (!uri) {
console.error('Set MONGODB_URI or MONGO_URI');
process.exit(1);
}
await mongoose.connect(uri);
const coll = mongoose.connection.db.collection('participants');
const existing = await coll.indexes();
const names = new Set(existing.map((i) => i.name));
for (const name of LEGACY_INDEX_NAMES) {
if (!names.has(name)) {
console.log(`Skip (not present): ${name}`);
continue;
}
await coll.dropIndex(name);
console.log(`Dropped legacy index: ${name}`);
}
const synced = await Participant.syncIndexes();
console.log('syncIndexes result:', synced);
const after = await coll.indexes();
console.log(
'Current indexes:',
after.map((i) => i.name).join(', ')
);
await mongoose.disconnect();
}
main().catch((e) => {
console.error(e);
process.exit(1);
});