Files
sena_db_api_layer/delete-all-context.js
silverpro89 5957636b07
All checks were successful
Deploy to Production / deploy (push) Successful in 29s
update new API
2026-02-09 23:17:03 +07:00

166 lines
4.1 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 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();