/** * Truncate Context table and sync model with database * This will: * 1. Delete all data in Context table * 2. Sync model schema with database (update structure if needed) */ const { sequelize } = require('./config/database'); const { Context } = require('./models'); // Configuration const CONFIRM_TRUNCATE = true; // Set to true to actually truncate const FORCE_SYNC = false; // Set to true to drop and recreate table async function truncateAndSyncContext() { try { console.log('🔄 Starting Context truncate and sync...\n'); await sequelize.authenticate(); console.log('✅ Database connection OK\n'); // Show current stats before truncate try { const count = await Context.count(); console.log(`📊 Current Context records: ${count}\n`); } catch (error) { console.log('⚠️ Table may not exist yet\n'); } // Step 1: Truncate table if (CONFIRM_TRUNCATE) { console.log('🗑️ Truncating Context table...'); try { await sequelize.query('SET FOREIGN_KEY_CHECKS = 0'); await Context.destroy({ where: {}, truncate: true }); await sequelize.query('SET FOREIGN_KEY_CHECKS = 1'); console.log('✅ Context table truncated successfully\n'); } catch (error) { console.log(`⚠️ Truncate warning: ${error.message}\n`); } } else { console.log('⏭️ Skipping truncate (CONFIRM_TRUNCATE is false)\n'); } // Step 2: Sync model with database console.log('🔄 Syncing Context model with database...'); console.log(` Force mode: ${FORCE_SYNC ? 'ON (will drop table)' : 'OFF (will alter)'}\n`); await Context.sync({ force: FORCE_SYNC, alter: !FORCE_SYNC }); console.log('✅ Context model synced successfully\n'); // Show table structure const [tableInfo] = await sequelize.query('DESCRIBE context'); console.log('📋 Current Context table structure:'); console.log('---'); tableInfo.forEach(field => { console.log(` ${field.Field}: ${field.Type}${field.Null === 'NO' ? ' NOT NULL' : ''}`); }); console.log('---\n'); // Final count const finalCount = await Context.count(); console.log(`📊 Final Context records: ${finalCount}\n`); console.log('✅ Operation complete!'); process.exit(0); } catch (error) { console.error('❌ Error:', error.message); console.error(error.stack); process.exit(1); } } // Safety warning if (!CONFIRM_TRUNCATE) { console.log('⚠️ WARNING: CONFIRM_TRUNCATE is set to false'); console.log('Only sync will be performed. No data will be deleted.\n'); } if (FORCE_SYNC) { console.log('⚠️ WARNING: FORCE_SYNC is set to true'); console.log('This will DROP and RECREATE the table!\n'); } // Run the script truncateAndSyncContext();