'use strict'; /** * 获取表字段定义。 * * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {string} tableName 表名。 * @return {Promise} 字段定义对象。 */ const getTableColumns = async (queryInterface, tableName) => { return queryInterface.describeTable(tableName); }; module.exports = { /** * 正向迁移:为波次表新增 source 字段。 * * 字段语义:0-普通波次 1-管理后台策略任务 2-PDA策略任务 * * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise} 无返回值。 */ up: async queryInterface => { const tableName = 'wms_wave_batch'; const columnsBefore = await getTableColumns(queryInterface, tableName); if (!columnsBefore.source) { await queryInterface.sequelize.query(` ALTER TABLE wms_wave_batch ADD COLUMN source TINYINT NOT NULL DEFAULT 0 COMMENT '来源:0-普通波次 1-管理后台策略任务 2-PDA策略任务' AFTER update_time `); } const columnsAfter = await getTableColumns(queryInterface, tableName); if (!columnsAfter.source) { throw new Error('迁移失败:wms_wave_batch.source 字段不存在'); } }, /** * 回滚迁移:删除波次表 source 字段。 * * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise} 无返回值。 */ down: async queryInterface => { const tableName = 'wms_wave_batch'; const columnsBefore = await getTableColumns(queryInterface, tableName); if (columnsBefore.source) { await queryInterface.removeColumn(tableName, 'source'); } }, };