186 lines
6.0 KiB
JavaScript
186 lines
6.0 KiB
JavaScript
/**
|
|
* 系统配置数据迁移脚本 - Node.js
|
|
* Feature: database-migration, Property 2: 数据记录数一致性
|
|
* Validates: Requirements 9.1
|
|
*
|
|
* 源表: MySQL config (25 条记录)
|
|
* 目标表: SQL Server configs
|
|
*
|
|
* 字段映射:
|
|
* - id -> id
|
|
* - key -> config_key
|
|
* - value -> config_value
|
|
*/
|
|
|
|
const mysql = require('mysql2/promise');
|
|
const sql = require('mssql');
|
|
|
|
// MySQL 配置
|
|
const mysqlConfig = {
|
|
host: '192.168.195.16',
|
|
port: 1887,
|
|
user: 'root',
|
|
password: 'Dbt@com@123',
|
|
database: 'youdas',
|
|
charset: 'utf8mb4'
|
|
};
|
|
|
|
// SQL Server 配置
|
|
const sqlServerConfig = {
|
|
server: '192.168.195.15',
|
|
port: 1433,
|
|
user: 'sa',
|
|
password: 'Dbt@com@123',
|
|
database: 'honey_box',
|
|
options: {
|
|
encrypt: false,
|
|
trustServerCertificate: true
|
|
}
|
|
};
|
|
|
|
// 转义SQL字符串
|
|
function escapeString(str) {
|
|
if (str === null || str === undefined) return 'NULL';
|
|
return "N'" + String(str).replace(/'/g, "''") + "'";
|
|
}
|
|
|
|
// 获取已迁移的配置ID列表
|
|
async function getMigratedIds(pool) {
|
|
const result = await pool.request().query('SELECT id FROM configs');
|
|
return new Set(result.recordset.map(r => r.id));
|
|
}
|
|
|
|
// 批量插入配置数据
|
|
async function insertConfigsBatch(pool, configs) {
|
|
if (configs.length === 0) return 0;
|
|
|
|
let insertedCount = 0;
|
|
|
|
// 构建批量插入SQL
|
|
let sqlBatch = 'SET IDENTITY_INSERT configs ON;\n';
|
|
|
|
for (const config of configs) {
|
|
sqlBatch += `
|
|
INSERT INTO configs (id, config_key, config_value)
|
|
VALUES (${config.id}, ${escapeString(config.key)}, ${escapeString(config.value)});
|
|
`;
|
|
}
|
|
|
|
sqlBatch += 'SET IDENTITY_INSERT configs OFF;';
|
|
|
|
try {
|
|
await pool.request().batch(sqlBatch);
|
|
insertedCount = configs.length;
|
|
} catch (err) {
|
|
console.error('批量插入失败:', err.message);
|
|
// 如果批量失败,尝试逐条插入
|
|
for (const config of configs) {
|
|
try {
|
|
const singleSql = `
|
|
SET IDENTITY_INSERT configs ON;
|
|
INSERT INTO configs (id, config_key, config_value)
|
|
VALUES (${config.id}, ${escapeString(config.key)}, ${escapeString(config.value)});
|
|
SET IDENTITY_INSERT configs OFF;`;
|
|
|
|
await pool.request().batch(singleSql);
|
|
insertedCount++;
|
|
console.log(` ✓ 插入配置 ${config.id}: ${config.key}`);
|
|
} catch (singleErr) {
|
|
console.error(` ✗ 插入配置 ${config.id} (${config.key}) 失败:`, singleErr.message);
|
|
}
|
|
}
|
|
}
|
|
|
|
return insertedCount;
|
|
}
|
|
|
|
async function main() {
|
|
console.log('========================================');
|
|
console.log('系统配置数据迁移脚本 - Node.js');
|
|
console.log('========================================\n');
|
|
|
|
let mysqlConn = null;
|
|
let sqlPool = null;
|
|
|
|
try {
|
|
// 连接 MySQL
|
|
console.log('正在连接 MySQL...');
|
|
mysqlConn = await mysql.createConnection(mysqlConfig);
|
|
console.log('MySQL 连接成功\n');
|
|
|
|
// 连接 SQL Server
|
|
console.log('正在连接 SQL Server...');
|
|
sqlPool = await sql.connect(sqlServerConfig);
|
|
console.log('SQL Server 连接成功\n');
|
|
|
|
// 获取已迁移的ID
|
|
console.log('正在获取已迁移的配置ID...');
|
|
const migratedIds = await getMigratedIds(sqlPool);
|
|
console.log(`已迁移配置数: ${migratedIds.size}\n`);
|
|
|
|
// 从 MySQL 获取所有配置数据
|
|
console.log('正在从 MySQL 读取配置数据...');
|
|
const [rows] = await mysqlConn.execute(`
|
|
SELECT id, \`key\`, value
|
|
FROM config
|
|
ORDER BY id
|
|
`);
|
|
console.log(`MySQL 配置总数: ${rows.length}\n`);
|
|
|
|
// 显示配置列表
|
|
console.log('配置列表:');
|
|
for (const row of rows) {
|
|
const status = migratedIds.has(row.id) ? '已迁移' : '待迁移';
|
|
console.log(` ${row.id}. ${row.key} [${status}]`);
|
|
}
|
|
console.log('');
|
|
|
|
// 过滤出未迁移的配置
|
|
const configsToMigrate = rows.filter(config => !migratedIds.has(config.id));
|
|
console.log(`待迁移配置数: ${configsToMigrate.length}\n`);
|
|
|
|
if (configsToMigrate.length === 0) {
|
|
console.log('所有配置数据已迁移完成!');
|
|
} else {
|
|
// 批量迁移
|
|
console.log('开始迁移配置数据...');
|
|
const inserted = await insertConfigsBatch(sqlPool, configsToMigrate);
|
|
console.log(`\n迁移完成!共插入 ${inserted} 条记录`);
|
|
}
|
|
|
|
// 验证迁移结果
|
|
console.log('\n========================================');
|
|
console.log('迁移结果验证');
|
|
console.log('========================================');
|
|
|
|
const [mysqlCount] = await mysqlConn.execute('SELECT COUNT(*) as count FROM config');
|
|
const sqlResult = await sqlPool.request().query('SELECT COUNT(*) as count FROM configs');
|
|
|
|
console.log(`MySQL config 表记录数: ${mysqlCount[0].count}`);
|
|
console.log(`SQL Server configs 表记录数: ${sqlResult.recordset[0].count}`);
|
|
|
|
if (mysqlCount[0].count === sqlResult.recordset[0].count) {
|
|
console.log('\n✅ 数据迁移完成,记录数一致!');
|
|
} else {
|
|
console.log(`\n⚠️ 记录数不一致,差异: ${mysqlCount[0].count - sqlResult.recordset[0].count}`);
|
|
}
|
|
|
|
// 显示迁移后的配置列表
|
|
console.log('\n迁移后的配置列表:');
|
|
const migratedConfigs = await sqlPool.request().query('SELECT id, config_key FROM configs ORDER BY id');
|
|
for (const config of migratedConfigs.recordset) {
|
|
console.log(` ${config.id}. ${config.config_key}`);
|
|
}
|
|
|
|
} catch (err) {
|
|
console.error('迁移过程中发生错误:', err);
|
|
process.exit(1);
|
|
} finally {
|
|
// 关闭连接
|
|
if (mysqlConn) await mysqlConn.end();
|
|
if (sqlPool) await sqlPool.close();
|
|
}
|
|
}
|
|
|
|
main();
|