/** * Ensure soft-delete field + unique email index only among active participants. * Usage (from backend/): node scripts/ensure-participant-soft-delete-index.js */ require('dotenv').config(); const mongoose = require('mongoose'); const Participant = require('../src/models/Participant'); 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 col = mongoose.connection.collection('participants'); const backfill = await col.updateMany( { deletedAt: { $exists: false } }, { $set: { deletedAt: null } } ); console.log(`Backfilled deletedAt=null on ${backfill.modifiedCount} participants`); const indexes = await col.indexes(); for (const idx of indexes) { if (idx.name === 'tenantOrganization_1_email_1') { try { await col.dropIndex('tenantOrganization_1_email_1'); console.log('Dropped legacy index tenantOrganization_1_email_1'); } catch (e) { console.warn('Could not drop legacy index:', e.message); } } } const synced = await Participant.syncIndexes(); console.log('syncIndexes:', synced); await mongoose.disconnect(); } main().catch((e) => { console.error(e); process.exit(1); });