246 lines
7.6 KiB
JavaScript
246 lines
7.6 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const mysql = require('mysql2/promise');
|
|
|
|
const ROOT = process.cwd();
|
|
const DEFAULT_CONFIG_PATH = path.join(ROOT, 'config', 'config.local.js');
|
|
|
|
const SLOT_LOCATIONS = [
|
|
{ suffix: '_dispatch', location: 'dispatch' },
|
|
{ suffix: '_history', location: 'history' },
|
|
{ suffix: '_outsourced', location: 'outsourced' },
|
|
];
|
|
|
|
function parseArgs(argv) {
|
|
const args = {
|
|
dryRun: false,
|
|
dbClient: 'wms',
|
|
configPath: DEFAULT_CONFIG_PATH,
|
|
warehouseId: null,
|
|
batchSize: 1000,
|
|
};
|
|
|
|
for (const item of argv) {
|
|
if (item === '--dry-run') {
|
|
args.dryRun = true;
|
|
continue;
|
|
}
|
|
|
|
if (item.startsWith('--warehouse-id=')) {
|
|
const raw = String(item.split('=')[1] || '').trim();
|
|
args.warehouseId = Number(raw);
|
|
continue;
|
|
}
|
|
|
|
if (item.startsWith('--batch-size=')) {
|
|
const raw = Number(item.split('=')[1] || 0);
|
|
if (Number.isFinite(raw) && raw > 0) {
|
|
args.batchSize = Math.min(50000, Math.max(100, Math.floor(raw)));
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (item.startsWith('--db-client=')) {
|
|
args.dbClient = String(item.split('=')[1] || '').trim() || 'wms';
|
|
continue;
|
|
}
|
|
|
|
if (item.startsWith('--config-path=')) {
|
|
args.configPath = path.resolve(ROOT, String(item.split('=')[1] || '').trim());
|
|
continue;
|
|
}
|
|
}
|
|
|
|
return args;
|
|
}
|
|
|
|
function loadDbClient(configPath, dbClientName) {
|
|
if (!fs.existsSync(configPath)) {
|
|
throw new Error(`Config file not found: ${configPath}`);
|
|
}
|
|
|
|
const configFactory = require(configPath);
|
|
const cfg = typeof configFactory === 'function'
|
|
? configFactory({ name: 'wms-order-id-location-backfill' })
|
|
: configFactory;
|
|
const dbClient = cfg && cfg.mysql && cfg.mysql.clients && cfg.mysql.clients[dbClientName];
|
|
|
|
if (!dbClient) {
|
|
throw new Error(`MySQL client not found in config.mysql.clients: ${dbClientName}`);
|
|
}
|
|
|
|
return dbClient;
|
|
}
|
|
|
|
function parseManagedTableName(tableName) {
|
|
const suffixMatch = /^wms_order_delivery_(\d+)(?:_(dispatch|history|outsourced))?$/.exec(tableName);
|
|
if (!suffixMatch) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
warehouseId: Number(suffixMatch[1]),
|
|
location: suffixMatch[2] || 'current',
|
|
};
|
|
}
|
|
|
|
async function ensureSequenceTable(conn, warehouseId, dryRun) {
|
|
const tableName = `wms_order_id_location_${warehouseId}`;
|
|
const [ rows ] = await conn.query(
|
|
`SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?`,
|
|
[ tableName ]
|
|
);
|
|
|
|
if (rows.length > 0) {
|
|
return tableName;
|
|
}
|
|
|
|
if (dryRun) {
|
|
console.log(`[dry-run] would create sequence table ${tableName}`);
|
|
return tableName;
|
|
}
|
|
|
|
await conn.query(
|
|
`CREATE TABLE \`${tableName}\` (
|
|
\`id\` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
\`location\` VARCHAR(20) NOT NULL DEFAULT 'current',
|
|
PRIMARY KEY (\`id\`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='仓库订单全局id序列与位置索引(极简,不迁移)'`
|
|
);
|
|
console.log(`created sequence table ${tableName}`);
|
|
return tableName;
|
|
}
|
|
|
|
async function countMissing(conn, sourceTable, seqTable) {
|
|
const [ rows ] = await conn.query(
|
|
`SELECT COUNT(*) AS cnt FROM \`${sourceTable}\` d `
|
|
+ `LEFT JOIN \`${seqTable}\` s ON s.id = d.id WHERE s.id IS NULL`
|
|
);
|
|
return Number(rows[0].cnt || 0);
|
|
}
|
|
|
|
async function fetchMissingBatch(conn, sourceTable, seqTable, batchSize) {
|
|
const [ rows ] = await conn.query(
|
|
`SELECT d.id FROM \`${sourceTable}\` d `
|
|
+ `LEFT JOIN \`${seqTable}\` s ON s.id = d.id WHERE s.id IS NULL `
|
|
+ `ORDER BY d.id ASC LIMIT ${batchSize}`
|
|
);
|
|
return (rows || []).map(row => Number(row.id)).filter(id => Number.isFinite(id) && id > 0);
|
|
}
|
|
|
|
async function upsertIds(conn, seqTable, location, ids, dryRun) {
|
|
if (ids.length === 0) {
|
|
return 0;
|
|
}
|
|
|
|
if (dryRun) {
|
|
console.log(`[dry-run] would insert ${ids.length} rows into ${seqTable} (location=${location}, first=${ids[0]}, last=${ids[ids.length - 1]})`);
|
|
return ids.length;
|
|
}
|
|
|
|
const values = ids.map(() => '(?, ?)').join(',');
|
|
const replacements = [];
|
|
for (const id of ids) {
|
|
replacements.push(id, location);
|
|
}
|
|
|
|
const [ result ] = await conn.query(
|
|
`INSERT INTO \`${seqTable}\` (\`id\`, \`location\`) VALUES ${values} `
|
|
+ 'ON DUPLICATE KEY UPDATE `location` = VALUES(`location`)',
|
|
replacements
|
|
);
|
|
return Number((result && result.affectedRows) || 0);
|
|
}
|
|
|
|
async function main() {
|
|
const args = parseArgs(process.argv.slice(2));
|
|
const dbClient = loadDbClient(args.configPath, args.dbClient);
|
|
|
|
const conn = await mysql.createConnection({
|
|
host: dbClient.host,
|
|
port: Number(dbClient.port),
|
|
user: dbClient.user,
|
|
password: dbClient.password,
|
|
database: dbClient.database,
|
|
charset: dbClient.charset || 'utf8mb4',
|
|
});
|
|
|
|
try {
|
|
console.log(`connected: ${dbClient.host}:${dbClient.port}/${dbClient.database}, dryRun=${args.dryRun}`);
|
|
|
|
const [ tableRows ] = await conn.query(
|
|
"SELECT TABLE_NAME FROM information_schema.TABLES "
|
|
+ "WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME LIKE 'wms_order_delivery\\\\_%' "
|
|
+ "ORDER BY TABLE_NAME"
|
|
);
|
|
|
|
const tables = (tableRows || [])
|
|
.map(row => ({ tableName: row.TABLE_NAME, parsed: parseManagedTableName(row.TABLE_NAME) }))
|
|
.filter(item => item.parsed !== null)
|
|
.filter(item => !args.warehouseId || item.parsed.warehouseId === args.warehouseId);
|
|
|
|
const warehouseIds = [ ...new Set(tables.map(item => item.parsed.warehouseId)) ].sort((a, b) => a - b);
|
|
console.log(`found warehouses: [${warehouseIds.join(', ')}], tables: ${tables.length}`);
|
|
|
|
let totalInserted = 0;
|
|
|
|
for (const warehouseId of warehouseIds) {
|
|
const seqTable = await ensureSequenceTable(conn, warehouseId, args.dryRun);
|
|
const warehouseTables = tables.filter(item => item.parsed.warehouseId === warehouseId);
|
|
|
|
for (const { tableName, parsed } of warehouseTables) {
|
|
const missingCount = await countMissing(conn, tableName, seqTable);
|
|
if (missingCount === 0) {
|
|
console.log(`[${tableName}] no missing ids`);
|
|
continue;
|
|
}
|
|
|
|
console.log(`[${tableName}] missing ids: ${missingCount} (location=${parsed.location})`);
|
|
|
|
let inserted = 0;
|
|
for (;;) {
|
|
const batch = await fetchMissingBatch(conn, tableName, seqTable, args.batchSize);
|
|
if (batch.length === 0) {
|
|
break;
|
|
}
|
|
|
|
const affected = await upsertIds(conn, seqTable, parsed.location, batch, args.dryRun);
|
|
inserted += affected;
|
|
|
|
if (batch.length < args.batchSize) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
console.log(`[${tableName}] done: inserted=${inserted}`);
|
|
totalInserted += inserted;
|
|
}
|
|
|
|
// 回填(显式 id)可能不会推进序列表自增计数器,结束时统一把水位
|
|
// 抬升到 max(id)+安全余量,保证后续发号不与存量 id 冲突
|
|
const [ seqMaxRows ] = await conn.query(
|
|
`SELECT COALESCE(MAX(id), 0) AS maxId FROM \`${seqTable}\``
|
|
);
|
|
const seqMaxId = Number(seqMaxRows[0].maxId || 0);
|
|
if (seqMaxId > 0) {
|
|
const safeTarget = seqMaxId + 50;
|
|
if (args.dryRun) {
|
|
console.log(`[dry-run] would raise ${seqTable} auto_increment to ${safeTarget}`);
|
|
} else {
|
|
await conn.query(`ALTER TABLE \`${seqTable}\` AUTO_INCREMENT = ${safeTarget}`);
|
|
console.log(`[${seqTable}] auto_increment raised to ${safeTarget}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(`TOTAL inserted (or would insert): ${totalInserted}`);
|
|
} finally {
|
|
await conn.end();
|
|
}
|
|
}
|
|
|
|
main().catch(error => {
|
|
console.error('FAILED:', error && error.message);
|
|
process.exit(1);
|
|
});
|