'use strict'; const TABLE_NAME = 'wms_stock_static_notice'; module.exports = { up: async (queryInterface, Sequelize) => { const tables = await queryInterface.showAllTables(); const exists = tables.some(t => (typeof t === 'string' ? t : t.tableName) === TABLE_NAME); if (exists) return; await queryInterface.createTable(TABLE_NAME, { id: { type: Sequelize.BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键ID', }, stock_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '商品ID,关联 wms_stock_warehouse.id', }, notice_time: { type: Sequelize.DATE, allowNull: false, comment: '通知时间', }, created_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, updated_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), comment: '更新时间', }, deleted_at: { type: Sequelize.DATE, allowNull: true, comment: '软删除时间', }, }, { comment: '静态商品通知关联表(记录静态商品已通知时间,避免重复通知)', }); // 核心查询:查某商品最近一次通知时间 await queryInterface.addIndex(TABLE_NAME, [ 'stock_id', 'notice_time' ], { name: 'idx_stock_notice_time', }); // 时间范围扫描:按通知时间清理/统计 await queryInterface.addIndex(TABLE_NAME, [ 'notice_time' ], { name: 'idx_notice_time', }); }, down: async queryInterface => { await queryInterface.dropTable(TABLE_NAME); }, };