55 lines
1.4 KiB
JavaScript
55 lines
1.4 KiB
JavaScript
/**
|
||
* 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);
|
||
});
|