96 lines
3.2 KiB
JavaScript
96 lines
3.2 KiB
JavaScript
const mysql = require('mysql2/promise');
|
|
const sql = require('mssql');
|
|
|
|
// MySQL配置
|
|
const mysqlConfig = {
|
|
host: '127.0.0.1',
|
|
port: 3306,
|
|
user: 'root',
|
|
password: 'root',
|
|
database: 'mhbox'
|
|
};
|
|
|
|
// SQL Server配置
|
|
const sqlServerConfig = {
|
|
server: '127.0.0.1',
|
|
port: 1433,
|
|
database: 'HoneyBox',
|
|
user: 'sa',
|
|
password: 'YourStrong@Passw0rd',
|
|
options: {
|
|
encrypt: false,
|
|
trustServerCertificate: true
|
|
}
|
|
};
|
|
|
|
async function migrateRewards() {
|
|
let mysqlConn;
|
|
let sqlPool;
|
|
|
|
try {
|
|
console.log('连接MySQL...');
|
|
mysqlConn = await mysql.createConnection(mysqlConfig);
|
|
|
|
console.log('连接SQL Server...');
|
|
sqlPool = await sql.connect(sqlServerConfig);
|
|
|
|
// 获取MySQL数据
|
|
console.log('获取MySQL reward数据...');
|
|
const [rows] = await mysqlConn.execute(`
|
|
SELECT id, reward_id, reward_type, reward_extend, reward_value, description,
|
|
FROM_UNIXTIME(create_time) as created_at,
|
|
FROM_UNIXTIME(update_time) as updated_at
|
|
FROM reward
|
|
`);
|
|
|
|
console.log(`共 ${rows.length} 条记录需要迁移`);
|
|
|
|
// 清空目标表
|
|
console.log('清空SQL Server rewards表...');
|
|
await sqlPool.request().query('DELETE FROM rewards');
|
|
|
|
// 批量插入
|
|
const batchSize = 100;
|
|
let inserted = 0;
|
|
|
|
for (let i = 0; i < rows.length; i += batchSize) {
|
|
const batch = rows.slice(i, i + batchSize);
|
|
|
|
for (const row of batch) {
|
|
await sqlPool.request()
|
|
.input('id', sql.Int, row.id)
|
|
.input('reward_id', sql.NVarChar(64), row.reward_id)
|
|
.input('reward_type', sql.Int, row.reward_type)
|
|
.input('reward_extend', sql.Int, row.reward_extend)
|
|
.input('reward_value', sql.Decimal(10, 2), row.reward_value)
|
|
.input('description', sql.NVarChar(255), row.description)
|
|
.input('created_at', sql.DateTime2, row.created_at)
|
|
.input('updated_at', sql.DateTime2, row.updated_at)
|
|
.query(`
|
|
SET IDENTITY_INSERT rewards ON;
|
|
INSERT INTO rewards (id, reward_id, reward_type, reward_extend, reward_value, description, created_at, updated_at)
|
|
VALUES (@id, @reward_id, @reward_type, @reward_extend, @reward_value, @description, @created_at, @updated_at);
|
|
SET IDENTITY_INSERT rewards OFF;
|
|
`);
|
|
inserted++;
|
|
}
|
|
|
|
console.log(`已插入 ${inserted}/${rows.length} 条记录`);
|
|
}
|
|
|
|
console.log('迁移完成!');
|
|
|
|
// 验证
|
|
const result = await sqlPool.request().query('SELECT COUNT(*) as count FROM rewards');
|
|
console.log(`SQL Server rewards表现有 ${result.recordset[0].count} 条记录`);
|
|
|
|
} catch (error) {
|
|
console.error('迁移失败:', error);
|
|
} finally {
|
|
if (mysqlConn) await mysqlConn.end();
|
|
if (sqlPool) await sqlPool.close();
|
|
}
|
|
}
|
|
|
|
migrateRewards();
|