'use strict'; /** * 收件登记表 wms_return_received 增加收件任务ID。 * * - addColumn receiving_task_id(BIGINT UNSIGNED NOT NULL DEFAULT 1,关联 wms_receiving_task.id) * - 回填:所有现有行 receiving_task_id 统一更新为 1(即系统预设「历史收件」任务) * - 索引 idx_receiving_task_id(按收件任务查询) */ const TABLE_NAME = 'wms_return_received'; module.exports = { async up(queryInterface, Sequelize) { const { BIGINT } = Sequelize; const tableInfo = await queryInterface.describeTable(TABLE_NAME); if (!tableInfo.receiving_task_id) { await queryInterface.addColumn(TABLE_NAME, 'receiving_task_id', { type: BIGINT(20).UNSIGNED, allowNull: false, defaultValue: 1, comment: '收件任务ID,关联 wms_receiving_task.id;默认 1=系统预设「历史收件」', }); } // 显式回填:所有现有行统一为 1(系统预设「历史收件」任务) await queryInterface.sequelize.query( 'UPDATE `' + TABLE_NAME + '` SET receiving_task_id = 1 WHERE receiving_task_id IS NULL OR receiving_task_id = 0' ); const indexes = await queryInterface.showIndex(TABLE_NAME); const hasIndex = (indexes || []).some(i => i.name === 'idx_receiving_task_id'); if (!hasIndex) { await queryInterface.addIndex(TABLE_NAME, [ 'receiving_task_id' ], { name: 'idx_receiving_task_id' }); } }, async down(queryInterface) { const indexes = await queryInterface.showIndex(TABLE_NAME); const hasIndex = (indexes || []).some(i => i.name === 'idx_receiving_task_id'); if (hasIndex) { await queryInterface.removeIndex(TABLE_NAME, 'idx_receiving_task_id'); } await queryInterface.removeColumn(TABLE_NAME, 'receiving_task_id'); }, };