const mysql = require('mysql2/promise'); (async () => { const conn = await mysql.createConnection({ host: 'localhost', port: 33060, user: 'root', password: 'root', database: 'iot_platform' }); // 检查所有表是否缺少 create_by 和 update_by 列 const [tables] = await conn.query( `SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA='iot_platform' AND TABLE_NAME IN ('alarm','alarm_rule','device','device_data_record', 'device_credential','device_group','linkage_rule','sys_role')` ); console.log('=== 检查并修复缺失的列 ===\n'); for (const t of tables) { const tableName = t.TABLE_NAME; const [cols] = await conn.query( `SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='iot_platform' AND TABLE_NAME='${tableName}'` ); const colNames = cols.map(c => c.COLUMN_NAME); const fixes = []; if (!colNames.includes('create_by')) { await conn.query(`ALTER TABLE ${tableName} ADD COLUMN create_by bigint NULL COMMENT '创建人ID' AFTER deleted`); fixes.push('create_by'); } if (!colNames.includes('update_by')) { await conn.query(`ALTER TABLE ${tableName} ADD COLUMN update_by bigint NULL COMMENT '更新人ID' AFTER create_by`); fixes.push('update_by'); } if (fixes.length > 0) { console.log(`✅ ${tableName}: 添加了 ${fixes.join(', ')}`); } else { console.log(`✅ ${tableName}: 列完整`); } } console.log('\n修复完成!'); await conn.end(); })().catch(e => console.error('错误:', e.message));