'use strict'; const TABLE_NAME = 'wms_pack_task_packer'; module.exports = { up: async (queryInterface, Sequelize) => { // 0. 清空已有数据(字段结构变更,emp 字段无法映射到新字段) await queryInterface.bulkDelete(TABLE_NAME, {}); // 1. 删除旧索引 await queryInterface.removeIndex(TABLE_NAME, 'uix_pack_task_emp'); await queryInterface.removeIndex(TABLE_NAME, 'idx_emp_id'); // 2. 删除旧字段 await queryInterface.removeColumn(TABLE_NAME, 'emp_id'); await queryInterface.removeColumn(TABLE_NAME, 'emp_no'); await queryInterface.removeColumn(TABLE_NAME, 'emp_name'); // 3. 新增 account 字段 await queryInterface.addColumn(TABLE_NAME, 'account_id', { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '账号ID,关联 wms_account.id', }); await queryInterface.addColumn(TABLE_NAME, 'account_name', { type: Sequelize.STRING(128), allowNull: false, comment: '账号姓名快照', }); // 4. 建新索引 await queryInterface.addIndex(TABLE_NAME, [ 'pack_task_id', 'account_id' ], { name: 'uix_pack_task_account', unique: true, }); await queryInterface.addIndex(TABLE_NAME, [ 'account_id' ], { name: 'idx_account_id', }); }, down: async (queryInterface, Sequelize) => { // 1. 删除新索引 await queryInterface.removeIndex(TABLE_NAME, 'uix_pack_task_account'); await queryInterface.removeIndex(TABLE_NAME, 'idx_account_id'); // 2. 删除新字段 await queryInterface.removeColumn(TABLE_NAME, 'account_id'); await queryInterface.removeColumn(TABLE_NAME, 'account_name'); // 3. 恢复旧字段 await queryInterface.addColumn(TABLE_NAME, 'emp_id', { type: Sequelize.INTEGER.UNSIGNED, allowNull: false, comment: '打包人工号ID', }); await queryInterface.addColumn(TABLE_NAME, 'emp_no', { type: Sequelize.STRING(64), allowNull: false, comment: '打包人工号', }); await queryInterface.addColumn(TABLE_NAME, 'emp_name', { type: Sequelize.STRING(128), allowNull: false, comment: '打包人姓名', }); // 4. 恢复旧索引 await queryInterface.addIndex(TABLE_NAME, [ 'pack_task_id', 'emp_id' ], { name: 'uix_pack_task_emp', unique: true, }); await queryInterface.addIndex(TABLE_NAME, [ 'emp_id' ], { name: 'idx_emp_id', }); }, };