/** * Delete all or specific Context records from database * WARNING: This will permanently delete data! */ const { sequelize } = require('./config/database'); const { Context } = require('./models'); // ============================================ // CONFIGURATION // ============================================ const DELETE_MODE = 'all'; // Options: 'all', 'vocabulary_only', 'by_type' // For 'by_type' mode - specify which type to delete const TYPE_TO_DELETE = 'vocabulary'; // e.g., 'vocabulary', 'grammar', etc. // For safety confirmation const CONFIRM_DELETE = true; // Set to true to actually delete // ============================================ // MAIN FUNCTIONS // ============================================ async function deleteAllContext() { console.log('šŸ—‘ļø Deleting ALL context records...'); const count = await Context.count(); console.log(`šŸ“Š Found ${count} records to delete`); if (!CONFIRM_DELETE) { console.log('āš ļø CONFIRM_DELETE is false. Set it to true to proceed.'); return 0; } const result = await Context.destroy({ where: {}, truncate: true }); console.log(`āœ… Deleted all context records`); return count; } async function deleteVocabularyOnly() { console.log('šŸ—‘ļø Deleting vocabulary context records only...'); const count = await Context.count({ where: { type: 'vocabulary' } }); console.log(`šŸ“Š Found ${count} vocabulary records to delete`); if (!CONFIRM_DELETE) { console.log('āš ļø CONFIRM_DELETE is false. Set it to true to proceed.'); return 0; } const result = await Context.destroy({ where: { type: 'vocabulary' } }); console.log(`āœ… Deleted ${result} vocabulary records`); return result; } async function deleteByType(type) { console.log(`šŸ—‘ļø Deleting context records with type: ${type}...`); const count = await Context.count({ where: { type: type } }); console.log(`šŸ“Š Found ${count} records to delete`); if (!CONFIRM_DELETE) { console.log('āš ļø CONFIRM_DELETE is false. Set it to true to proceed.'); return 0; } const result = await Context.destroy({ where: { type: type } }); console.log(`āœ… Deleted ${result} records with type '${type}'`); return result; } async function showStats() { console.log('\nšŸ“Š Current Context Statistics:'); const total = await Context.count(); console.log(`Total records: ${total}`); // Count by type const types = await sequelize.query( 'SELECT type, COUNT(*) as count FROM context GROUP BY type', { type: sequelize.QueryTypes.SELECT } ); console.log('\nBy Type:'); types.forEach(t => { console.log(` - ${t.type}: ${t.count}`); }); return total; } // ============================================ // MAIN EXECUTION // ============================================ async function main() { try { console.log('šŸ”„ Starting Context deletion script...\n'); await sequelize.authenticate(); console.log('āœ… Database connection OK\n'); // Show current stats await showStats(); console.log('\n---\n'); let result = 0; switch (DELETE_MODE) { case 'all': result = await deleteAllContext(); break; case 'vocabulary_only': result = await deleteVocabularyOnly(); break; case 'by_type': result = await deleteByType(TYPE_TO_DELETE); break; default: console.log('āŒ Invalid DELETE_MODE'); } console.log('\n---\n'); // Show stats after deletion await showStats(); console.log('\nāœ… Operation complete!'); console.log(`šŸ“Š Records deleted: ${result}`); process.exit(0); } catch (error) { console.error('āŒ Error during deletion:', error.message); console.error(error.stack); process.exit(1); } } // Safety check if (!CONFIRM_DELETE) { console.log('\nāš ļø WARNING: CONFIRM_DELETE is set to false'); console.log('This is a DRY RUN. No data will be deleted.'); console.log('Set CONFIRM_DELETE = true to actually delete data.\n'); } // Run the script main();