// 清理临时userId数据脚本 // 此脚本用于删除chat_conversations和chat_messages表中所有临时userId的数据 // 临时userId格式:temp_1765852836234 const mysql = require('mysql2/promise'); async function cleanupTempUserIds() { let connection; try { // 创建数据库连接 connection = await mysql.createConnection({ host: '1.95.162.61', port: 3306, user: 'root', password: 'schl@2025', database: 'wechat_app' }); console.log('✓ 数据库连接成功'); // ============== 清理chat_conversations表中的临时userId数据 ============== console.log('\n=== 清理chat_conversations表中的临时userId数据 ==='); // 1. 统计需要删除的临时userId会话记录 const [convTempCount] = await connection.execute( 'SELECT COUNT(*) as count FROM chat_conversations WHERE userId LIKE ? OR managerId LIKE ?', ['temp_%', 'temp_%'] ); console.log(`找到 ${convTempCount[0].count} 条临时userId的会话记录`); // 2. 删除临时userId的会话记录 if (convTempCount[0].count > 0) { const [convResult] = await connection.execute( 'DELETE FROM chat_conversations WHERE userId LIKE ? OR managerId LIKE ?', ['temp_%', 'temp_%'] ); console.log(`成功删除chat_conversations表中的 ${convResult.affectedRows} 条临时userId记录`); } // ============== 清理chat_messages表中的临时userId数据 ============== console.log('\n=== 清理chat_messages表中的临时userId数据 ==='); // 1. 统计需要删除的临时userId消息记录 const [msgTempCount] = await connection.execute( 'SELECT COUNT(*) as count FROM chat_messages WHERE sender_id LIKE ? OR receiver_id LIKE ?', ['temp_%', 'temp_%'] ); console.log(`找到 ${msgTempCount[0].count} 条临时userId的消息记录`); // 2. 删除临时userId的消息记录 if (msgTempCount[0].count > 0) { const [msgResult] = await connection.execute( 'DELETE FROM chat_messages WHERE sender_id LIKE ? OR receiver_id LIKE ?', ['temp_%', 'temp_%'] ); console.log(`成功删除chat_messages表中的 ${msgResult.affectedRows} 条临时userId记录`); } // ============== 验证清理结果 ============== console.log('\n=== 验证清理结果 ==='); // 再次检查chat_conversations表中是否还有临时userId记录 const [convVerify] = await connection.execute( 'SELECT COUNT(*) as count FROM chat_conversations WHERE userId LIKE ? OR managerId LIKE ?', ['temp_%', 'temp_%'] ); console.log(`chat_conversations表中剩余临时userId记录数: ${convVerify[0].count}`); // 再次检查chat_messages表中是否还有临时userId记录 const [msgVerify] = await connection.execute( 'SELECT COUNT(*) as count FROM chat_messages WHERE sender_id LIKE ? OR receiver_id LIKE ?', ['temp_%', 'temp_%'] ); console.log(`chat_messages表中剩余临时userId记录数: ${msgVerify[0].count}`); // ============== 检查剩余的有效数据 ============== console.log('\n=== 检查剩余的有效数据 ==='); // 检查chat_conversations表中的有效记录数 const [convValid] = await connection.execute( 'SELECT COUNT(*) as count FROM chat_conversations' ); console.log(`chat_conversations表中有效会话记录数: ${convValid[0].count}`); // 检查chat_messages表中的有效记录数 const [msgValid] = await connection.execute( 'SELECT COUNT(*) as count FROM chat_messages' ); console.log(`chat_messages表中有效消息记录数: ${msgValid[0].count}`); console.log('\n✅ 清理完成!所有临时userId数据已被删除。'); console.log('✅ 现在数据库中只保留了真实userId的数据。'); } catch (error) { console.error('❌ 清理过程中发生错误:', error); } finally { if (connection) { await connection.end(); console.log('✅ 数据库连接已关闭'); } } } // 执行清理操作 console.log('========== 开始清理临时userId数据 =========='); cleanupTempUserIds().catch(console.error);