oms/backend/database/migrations/20260911000300-create-warehouse-apis.js
liufumingren cbd16752a5 feat(backend): 商品、店铺、组合商品、仓库与库存模块
- 商品/商品图片、店铺、店铺商品、组合商品及明细
- 仓库、仓库商品(WMS itemId 映射)、仓库接口配置与字段定义(warehouseApi/warehouseApiField)
- 库存:stock_items、product_stocks(ZC/CC 双维度)与库存重算工具(utils/recalcStock.js)
- 库存单据与对接日志:stock orders、api_outbound_logs
- 相关迁移:shops/shop_products/stock_orders/warehouses/warehouse_apis/warehouse_products/product_stocks/files-images 等表与字段
2026-09-13 02:23:39 +08:00

76 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'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');
},
};