'use strict'; const TABLE_NAME = 'wms_print_platform'; const COLUMN_NAME = 'need_callback_url'; /** * 获取当前数据库已存在的表名集合。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise>} 表名集合。 */ const getExistingTableSet = async queryInterface => { const tables = await queryInterface.showAllTables(); return new Set( tables.map(table => (typeof table === 'string' ? table : table.tableName)) ); }; /** * 构建打印平台是否需要回调地址字段定义。 * @param {typeof import('sequelize')} Sequelize Sequelize 类型集合。 * @return {Object} 字段定义。 */ const buildNeedCallbackUrlColumn = Sequelize => ({ type: Sequelize.TINYINT(1), allowNull: false, defaultValue: 1, comment: '是否需要回调地址:0-否,1-是', }); module.exports = { /** * 执行迁移:为打印平台表新增 need_callback_url 字段。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {typeof import('sequelize')} Sequelize Sequelize 类型集合。 * @return {Promise} 无返回值。 */ async up(queryInterface, Sequelize) { const existingTableSet = await getExistingTableSet(queryInterface); if (!existingTableSet.has(TABLE_NAME)) { return; } const columns = await queryInterface.describeTable(TABLE_NAME); if (columns[COLUMN_NAME]) { return; } await queryInterface.addColumn(TABLE_NAME, COLUMN_NAME, buildNeedCallbackUrlColumn(Sequelize)); }, /** * 回滚迁移:删除打印平台表的 need_callback_url 字段。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise} 无返回值。 */ async down(queryInterface) { const existingTableSet = await getExistingTableSet(queryInterface); if (!existingTableSet.has(TABLE_NAME)) { return; } const columns = await queryInterface.describeTable(TABLE_NAME); if (!columns[COLUMN_NAME]) { return; } await queryInterface.removeColumn(TABLE_NAME, COLUMN_NAME); }, };