'use strict'; /** * wms_order_b2b 增加 is_urgent 列(是否加急,默认 N)与 idx_is_urgent 索引。 * * 背景:B2B 作业明细需要区分加急备货订单;列表(order.pendingOrder2b.list / * order.deliveryOrder2b.list)输出该字段并支持 where.is_urgent 筛选, * 转换时以订单表 is_urgency 作为初始值。 * * 幂等:列/索引存在性判断后再执行,重复运行安全。 */ const TABLE_NAME = 'wms_order_b2b'; const COLUMN_NAME = 'is_urgent'; const INDEX_NAME = 'idx_is_urgent'; /** * 判断索引是否存在。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {string} indexName 索引名。 * @return {Promise} 是否存在。 */ async function indexExists(queryInterface, indexName) { const indexes = await queryInterface.showIndex(TABLE_NAME); return (Array.isArray(indexes) ? indexes : []).some(index => { const name = String(index && (index.name || index.Key_name || index.key_name) || ''); return name.toLowerCase() === String(indexName).toLowerCase(); }); } module.exports = { /** * 执行迁移:新增 is_urgent 列与索引。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {Object} Sequelize Sequelize 类型集合。 * @return {Promise} 无返回值。 */ async up(queryInterface, Sequelize) { const { ENUM } = Sequelize; const log = []; const columns = await queryInterface.describeTable(TABLE_NAME); if (!columns || Object.keys(columns).length === 0) { throw new Error(`表 ${TABLE_NAME} 不存在,无法新增 ${COLUMN_NAME}`); } if (!Object.prototype.hasOwnProperty.call(columns, COLUMN_NAME)) { await queryInterface.addColumn(TABLE_NAME, COLUMN_NAME, { type: ENUM('Y', 'N'), allowNull: false, defaultValue: 'N', comment: '是否加急:Y=加急 N=普通', }); log.push({ action: 'add-column', columnName: COLUMN_NAME }); } else { log.push({ action: 'skip-add-column', columnName: COLUMN_NAME, reason: 'exists' }); } if (!(await indexExists(queryInterface, INDEX_NAME))) { await queryInterface.addIndex(TABLE_NAME, [ COLUMN_NAME ], { name: INDEX_NAME }); log.push({ action: 'add-index', indexName: INDEX_NAME }); } else { log.push({ action: 'skip-add-index', indexName: INDEX_NAME, reason: 'exists' }); } console.log('[20261117180000-add-is-urgent-to-wms-order-b2b] up results:', JSON.stringify(log)); }, /** * 回滚迁移:移除 is_urgent 索引与列。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise} 无返回值。 */ async down(queryInterface) { const log = []; if (await indexExists(queryInterface, INDEX_NAME)) { await queryInterface.removeIndex(TABLE_NAME, INDEX_NAME); log.push({ action: 'drop-index', indexName: INDEX_NAME }); } const columns = await queryInterface.describeTable(TABLE_NAME); if (Object.prototype.hasOwnProperty.call(columns, COLUMN_NAME)) { await queryInterface.removeColumn(TABLE_NAME, COLUMN_NAME); log.push({ action: 'drop-column', columnName: COLUMN_NAME }); } console.log('[20261117180000-add-is-urgent-to-wms-order-b2b] down results:', JSON.stringify(log)); }, };