'use strict'; /** * wms_order_express 三个 Y/N 标记列补默认值与存量回填: * * - `is_back`(是否退回)、`is_intercept`(是否拦截)、`is_galaxy_receive`(是否揽收) * 建表时均为 `ENUM('Y','N') NULL` 且无默认值,业务创建运单时多数不显式赋值 → * 存量约 100% 为 NULL,导致运单列表按「是否退回 / 是否拦截 / 是否揽收 = N」筛选时 * 直接返回空(NULL 不参与 ='N' 比较)。 * * 本迁移做两件事: * 1) 三列补 `DEFAULT 'N'`(保留 NULL 允许,避免历史裸 SQL 显式写 NULL 时报错); * 2) 存量 NULL 回填为 'N',已为 'Y' 的一律不动。 * * 回填按主键区间分批(每批 ≤ 20000 个 id),走 PK 范围扫描, * 避免单条大 UPDATE 长时间持锁(生产约 159 万行 × 3 列)。 * * 幂等:列默认值已是 'N' 时跳过 MODIFY;无 NULL 时跳过该批 UPDATE,可重复执行。 */ const TABLE_NAME = 'wms_order_express'; const COLUMNS = [ 'is_back', 'is_intercept', 'is_galaxy_receive' ]; const FILL_VALUE = 'N'; /** 单批主键区间宽度(控制单条 UPDATE 的锁持有时间) */ const ID_WINDOW = 20000; /** * 读取列定义(类型 / 可空 / 默认值 / 注释),用于构造 MODIFY COLUMN。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {string} tableName 表名。 * @param {string} columnName 列名。 * @return {Promise} 列定义,列不存在时返回 null。 */ async function describeColumn(queryInterface, tableName, columnName) { const [ rows ] = await queryInterface.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: [ tableName, columnName ] } ); return (rows || [])[0] || null; } /** * 将列默认值设置为 'N'。 * * 使用 `ALTER COLUMN ... SET DEFAULT`(MySQL 5.7 元数据变更,不重建表、不锁写), * 而不是 `MODIFY COLUMN`(5.7 下会触发表重建,生产 wms_order_express 约 159 万行)。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {string} columnName 列名。 * @return {Promise} 无返回值。 */ async function applyDefault(queryInterface, columnName) { const column = await describeColumn(queryInterface, TABLE_NAME, columnName); if (!column) { console.log(`[${columnName}] skip: ${TABLE_NAME}.${columnName} 列不存在`); return; } if (String(column.columnDefault || '').toUpperCase() === FILL_VALUE) { console.log(`[${columnName}] skip: 默认值已是 '${FILL_VALUE}'`); return; } await queryInterface.sequelize.query( `ALTER TABLE \`${TABLE_NAME}\` ALTER COLUMN \`${columnName}\` SET DEFAULT '${FILL_VALUE}'` ); console.log(`[${columnName}] default '${FILL_VALUE}' applied`); } /** * 按主键区间分批把 NULL 回填为 'N'(已为 'Y'/'N' 的行不受影响)。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {string} columnName 列名。 * @return {Promise} 实际回填行数。 */ async function backfillColumn(queryInterface, columnName) { const sequelize = queryInterface.sequelize; const [ rangeRows ] = await sequelize.query( `SELECT MIN(id) AS minId, MAX(id) AS maxId FROM \`${TABLE_NAME}\` ` + `WHERE \`${columnName}\` IS NULL`, { type: sequelize.QueryTypes.SELECT } ); const range = (rangeRows && rangeRows[0]) || rangeRows || {}; const minId = Number(range.minId); const maxId = Number(range.maxId); if (!Number.isFinite(minId) || !Number.isFinite(maxId)) { console.log(`[${columnName}] skip: 无 NULL 数据`); return 0; } let filled = 0; for (let start = minId; start <= maxId; start += ID_WINDOW) { const end = start + ID_WINDOW - 1; const [ , affected ] = await sequelize.query( `UPDATE \`${TABLE_NAME}\` SET \`${columnName}\` = ? ` + 'WHERE `id` BETWEEN ? AND ? AND `' + columnName + '` IS NULL', { replacements: [ FILL_VALUE, start, end ] } ); const count = Number(affected && affected.affectedRows ? affected.affectedRows : affected) || 0; filled += count; } console.log(`[${columnName}] backfilled ${filled} rows`); return filled; } module.exports = { /** * 执行迁移:补默认值 + 存量 NULL 回填为 'N'。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise} 无返回值。 */ async up(queryInterface) { for (const columnName of COLUMNS) { await applyDefault(queryInterface, columnName); await backfillColumn(queryInterface, columnName); } }, /** * 回滚迁移:仅移除列默认值;数据回填不可逆('N' 与 NULL 在查询口径上等价, * 反向置 NULL 会让筛选再次失效,故不还原数据)。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise} 无返回值。 */ async down(queryInterface) { for (const columnName of COLUMNS) { const column = await describeColumn(queryInterface, TABLE_NAME, columnName); if (!column) continue; if (String(column.columnDefault || '').toUpperCase() !== FILL_VALUE) continue; await queryInterface.sequelize.query( `ALTER TABLE \`${TABLE_NAME}\` ALTER COLUMN \`${columnName}\` DROP DEFAULT` ); console.log(`[${columnName}] default dropped`); } }, };