'use strict'; /** * wms_order_express_trace.weight 由 INT 改为 DECIMAL(10,3)。 * * 背景:外部轨迹接口返回的 weight 为带小数的字符串(如 "0.99"), * 原 INT 列会把 0.99 四舍五入/截断为 1 或 0,丢失精度。 * * 变更:INT(11) DEFAULT 0 → DECIMAL(10,3) DEFAULT 0(保留原可空性与注释)。 * * 注意:MySQL 不支持 INT→DECIMAL 的 INPLACE 变更,只能 COPY(重建表)。 * 该表为 2026-11-11 新建(轨迹尚在接入期,数据量小),重建代价可接受。 * 幂等:已是 decimal 时跳过。 */ const TABLE_NAME = 'wms_order_express_trace'; const COLUMN_NAME = 'weight'; module.exports = { async up(queryInterface) { const { sequelize } = queryInterface; const [ rows ] = await sequelize.query( 'SELECT COLUMN_TYPE AS columnType, IS_NULLABLE AS isNullable, ' + 'COLUMN_DEFAULT AS columnDefault, COLUMN_COMMENT AS columnComment ' + 'FROM INFORMATION_SCHEMA.COLUMNS ' + 'WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ?', { replacements: [ TABLE_NAME, COLUMN_NAME ] } ); const column = (rows || [])[0]; if (!column) { console.log(`[${COLUMN_NAME}] skip: ${TABLE_NAME}.${COLUMN_NAME} 列不存在`); return; } if (String(column.columnType || '').toLowerCase().startsWith('decimal')) { console.log(`[${COLUMN_NAME}] skip: 已是 DECIMAL(${column.columnType})`); return; } const nullable = String(column.isNullable).toUpperCase() === 'YES' ? 'NULL' : 'NOT NULL'; const defaultValue = column.columnDefault === null || column.columnDefault === undefined ? '' : ` DEFAULT ${Number(column.columnDefault) || 0}`; const comment = String(column.columnComment || '').replace(/'/g, "''"); await sequelize.query( `ALTER TABLE \`${TABLE_NAME}\` MODIFY COLUMN \`${COLUMN_NAME}\` ` + `DECIMAL(10,3) ${nullable}${defaultValue} COMMENT '${comment}'` ); console.log(`[${COLUMN_NAME}] changed to DECIMAL(10,3) on ${TABLE_NAME}`); }, async down(queryInterface) { const { sequelize } = queryInterface; const [ rows ] = await sequelize.query( 'SELECT COLUMN_TYPE AS columnType, IS_NULLABLE AS isNullable, ' + 'COLUMN_DEFAULT AS columnDefault, COLUMN_COMMENT AS columnComment ' + 'FROM INFORMATION_SCHEMA.COLUMNS ' + 'WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ?', { replacements: [ TABLE_NAME, COLUMN_NAME ] } ); const column = (rows || [])[0]; if (!column) return; if (!String(column.columnType || '').toLowerCase().startsWith('decimal')) return; const nullable = String(column.isNullable).toUpperCase() === 'YES' ? 'NULL' : 'NOT NULL'; const comment = String(column.columnComment || '').replace(/'/g, "''"); await sequelize.query( `ALTER TABLE \`${TABLE_NAME}\` MODIFY COLUMN \`${COLUMN_NAME}\` ` + `INT(11) ${nullable} DEFAULT 0 COMMENT '${comment}'` ); console.log(`[${COLUMN_NAME}] reverted to INT(11)`); }, };