'use strict'; /** * 外部仓库拆分为两张表: * 1) warehouses 仅保留仓库基础信息(code / name / type / status / remark) * 2) warehouse_apis 存放仓库的接口配置,一个仓库可有多条接口配置 * 原 warehouses 上的接口字段数据迁移到 warehouse_apis(每仓库一条「默认配置」),随后删除这些字段 */ module.exports = { async up(queryInterface, Sequelize) { await queryInterface.createTable( 'warehouse_apis', { id: { type: Sequelize.BIGINT.UNSIGNED, autoIncrement: true, primaryKey: true }, principal_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID', references: { model: 'principals', key: 'id' }, onDelete: 'CASCADE', }, warehouse_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, comment: '所属仓库ID', references: { model: 'warehouses', key: 'id' }, onDelete: 'CASCADE', }, name: { type: Sequelize.STRING(100), allowNull: false, comment: '接口配置名称' }, type: { type: Sequelize.STRING(50), comment: '接口类型:wms/erp/other' }, base_url: { type: Sequelize.STRING(255), comment: '接口基础地址' }, app_key: { type: Sequelize.STRING(100), comment: '应用Key' }, app_secret: { type: Sequelize.STRING(255), comment: '应用密钥' }, extra: { type: Sequelize.JSON, comment: '扩展配置' }, status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1启用 0禁用' }, remark: { type: Sequelize.STRING(255), comment: '备注' }, created_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), }, updated_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), }, }, { charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '仓库接口配置表' } ); await queryInterface.addIndex('warehouse_apis', { fields: [ 'principal_id' ], name: 'idx_warehouse_apis_principal' }); await queryInterface.addIndex('warehouse_apis', { fields: [ 'warehouse_id' ], name: 'idx_warehouse_apis_warehouse' }); // 迁移仓库上已有的接口配置(存在任一接口字段即生成一条配置) await queryInterface.sequelize.query(` INSERT INTO warehouse_apis (principal_id, warehouse_id, name, type, base_url, app_key, app_secret, extra, status, remark, created_at, updated_at) SELECT principal_id, id, '默认配置', type, base_url, app_key, app_secret, extra, status, '由仓库配置迁移', NOW(), NOW() FROM warehouses WHERE base_url IS NOT NULL OR app_key IS NOT NULL OR app_secret IS NOT NULL OR extra IS NOT NULL `); // 仓库表不再承载接口配置字段 await queryInterface.removeColumn('warehouses', 'base_url'); await queryInterface.removeColumn('warehouses', 'app_key'); await queryInterface.removeColumn('warehouses', 'app_secret'); await queryInterface.removeColumn('warehouses', 'extra'); }, async down(queryInterface, Sequelize) { await queryInterface.addColumn('warehouses', 'base_url', { type: Sequelize.STRING(255), comment: '接口基础地址' }); await queryInterface.addColumn('warehouses', 'app_key', { type: Sequelize.STRING(100), comment: '应用Key' }); await queryInterface.addColumn('warehouses', 'app_secret', { type: Sequelize.STRING(255), comment: '应用密钥' }); await queryInterface.addColumn('warehouses', 'extra', { type: Sequelize.JSON, comment: '扩展配置' }); await queryInterface.dropTable('warehouse_apis'); }, };