'use strict'; module.exports = { async up(queryInterface, Sequelize) { const { BIGINT, STRING, DATE } = Sequelize; const tableName = 'wms_picking'; const [ tables ] = await queryInterface.sequelize.query( `SHOW TABLES LIKE '${tableName}'` ); if (tables.length === 0) { await queryInterface.createTable(tableName, { id: { type: BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键ID', }, order_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '订单ID', }, order_item_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '订单明细ID', }, storage_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '库存记录ID', }, space_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '库位ID', }, space_name: { type: STRING(100), allowNull: true, comment: '库位名称', }, status: { type: STRING(20), allowNull: false, defaultValue: 'pending', comment: '状态', }, created_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, updated_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), comment: '更新时间', }, }, { engine: 'InnoDB', charset: 'utf8mb4', collate: 'utf8mb4_general_ci', comment: '订单储位分配表', }); } else { console.log('Table wms_picking already exists, ensuring indexes'); } const safeCreateIndex = async (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([ 'order_id' ], { name: 'idx_wms_picking_order_id', }); await safeCreateIndex([ 'order_item_id' ], { name: 'idx_wms_picking_order_item_id', }); await safeCreateIndex([ 'storage_id' ], { name: 'idx_wms_picking_storage_id', }); await safeCreateIndex([ 'space_id' ], { name: 'idx_wms_picking_space_id', }); await safeCreateIndex([ 'status' ], { name: 'idx_wms_picking_status', }); await safeCreateIndex([ 'order_item_id', 'space_id' ], { name: 'idx_wms_picking_order_item_space', }); }, async down(queryInterface) { const tableName = 'wms_picking'; const [ tables ] = await queryInterface.sequelize.query( `SHOW TABLES LIKE '${tableName}'` ); if (tables.length > 0) { await queryInterface.dropTable(tableName); } }, };