// database/migrations/20241201000008-create-wms-stock-store.js 'use strict'; module.exports = { async up(queryInterface, Sequelize) { const { BIGINT, STRING, DECIMAL, INTEGER, DATE, ENUM, CHAR } = Sequelize; // 先检查表是否存在,避免重复创建 const [ tables ] = await queryInterface.sequelize.query( "SHOW TABLES LIKE 'wms_stock_store'" ); if (tables.length > 0) { console.log('Table wms_stock_store already exists, skipping creation'); return; } await queryInterface.createTable('wms_stock_store', { id: { type: BIGINT(20), primaryKey: true, autoIncrement: true, allowNull: false, comment: '库存商品ID', }, warehouse_code: { type: BIGINT(20), allowNull: false, comment: '仓库编码', }, owner_code: { type: STRING(50), allowNull: false, comment: '货主编码', }, app_key: { type: STRING(50), allowNull: true, comment: '应用密钥', }, is_area_sale: { type: CHAR(1), allowNull: false, defaultValue: 'N', comment: '是否区域销售', }, item_code: { type: STRING(50), allowNull: false, comment: '商品编码', }, goods_code: { type: STRING(50), allowNull: false, comment: '货物编码', }, item_name: { type: STRING(255), allowNull: false, comment: '商品名称', }, sku_property: { type: STRING(100), allowNull: true, comment: 'SKU规格', }, stock_unit: { type: STRING(20), allowNull: false, comment: '库存单位', }, length: { type: DECIMAL(10, 2), allowNull: true, defaultValue: 0.00, comment: '长度', }, width: { type: DECIMAL(10, 2), allowNull: true, defaultValue: 0.00, comment: '宽度', }, height: { type: DECIMAL(10, 2), allowNull: true, defaultValue: 0.00, comment: '高度', }, volume: { type: DECIMAL(10, 2), allowNull: true, defaultValue: 0.00, comment: '体积', }, gross_weight: { type: DECIMAL(10, 2), allowNull: true, defaultValue: 0.00, comment: '毛重', }, net_weight: { type: DECIMAL(10, 2), allowNull: true, defaultValue: 0.00, comment: '净重', }, category_id: { type: INTEGER(11), allowNull: false, comment: '分类ID', }, category_name: { type: STRING(100), allowNull: false, comment: '分类名称', }, item_type: { type: STRING(10), allowNull: false, comment: '商品类型', }, tag_price: { type: DECIMAL(10, 2), allowNull: true, defaultValue: 0.00, comment: '标价', }, retail_price: { type: DECIMAL(10, 2), allowNull: true, defaultValue: 0.00, comment: '零售价', }, is_sn_mgmt: { type: CHAR(1), allowNull: false, defaultValue: 'N', comment: '是否序列号管理', }, is_shelf_life_mgmt: { type: CHAR(1), allowNull: false, defaultValue: 'N', comment: '是否保质期管理', }, is_batch_mgmt: { type: CHAR(1), allowNull: false, defaultValue: 'N', comment: '是否批次管理', }, create_time: { type: DATE, allowNull: false, comment: '创建时间', }, update_time: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), comment: '更新时间', }, is_valid: { type: CHAR(1), allowNull: false, defaultValue: 'Y', comment: '是否有效', }, barcode: { type: STRING(255), allowNull: true, comment: '条形码', }, stock_id: { type: BIGINT(20), allowNull: true, comment: '库存ID', }, warehouse_id: { type: BIGINT(20), allowNull: false, references: { model: 'wms_store', key: 'id', }, onUpdate: 'RESTRICT', onDelete: 'NO ACTION', comment: '仓库ID', }, store_id: { type: BIGINT(20), allowNull: false, references: { model: 'wms_store', key: 'id', }, onUpdate: 'RESTRICT', onDelete: 'NO ACTION', comment: '商家ID', }, create_from: { type: ENUM('qimen', 'api'), allowNull: true, comment: '创建来源', }, delete_at: { type: DATE, allowNull: true, comment: '删除时间', }, }, { engine: 'InnoDB', charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '库存商品信息表', }); // 安全创建索引的函数 const safeCreateIndex = async (tableName, fields, options) => { const [ existingIndexes ] = await queryInterface.sequelize.query( `SHOW INDEX FROM ${tableName} WHERE Key_name = '${options.name}'` ); if (existingIndexes.length === 0) { await queryInterface.addIndex(tableName, fields, options); } }; // 添加唯一索引 await safeCreateIndex('wms_stock_store', [ 'warehouse_code', 'owner_code', 'item_code', 'app_key', 'warehouse_id', 'store_id', ], { name: 'uk_warehouse_owner_item', unique: true, }); // 添加普通索引 await safeCreateIndex('wms_stock_store', [ 'owner_code' ], { name: 'idx_owner_code', }); await safeCreateIndex('wms_stock_store', [ 'goods_code' ], { name: 'idx_goods_code', }); await safeCreateIndex('wms_stock_store', [ 'category_id' ], { name: 'idx_category_id', }); await safeCreateIndex('wms_stock_store', [ 'create_time' ], { name: 'idx_create_time', }); await safeCreateIndex('wms_stock_store', [ 'is_valid' ], { name: 'idx_is_valid', }); await safeCreateIndex('wms_stock_store', [ 'item_code' ], { name: 'uk_item_code', }); await safeCreateIndex('wms_stock_store', [ 'warehouse_id' ], { name: 'idx_warehouse_id', }); await safeCreateIndex('wms_stock_store', [ 'store_id' ], { name: 'idx_store_id', }); await safeCreateIndex('wms_stock_store', [ 'barcode' ], { name: 'idx_barcode', }); // 添加复合索引 await safeCreateIndex('wms_stock_store', [ 'warehouse_id', 'is_valid' ], { name: 'idx_warehouse_valid', }); await safeCreateIndex('wms_stock_store', [ 'store_id', 'is_valid' ], { name: 'idx_store_valid', }); await safeCreateIndex('wms_stock_store', [ 'item_code', 'is_valid' ], { name: 'idx_item_valid', }); }, async down(queryInterface) { // 安全删除表 const [ tables ] = await queryInterface.sequelize.query( "SHOW TABLES LIKE 'wms_stock_store'" ); if (tables.length > 0) { await queryInterface.dropTable('wms_stock_store'); } }, };