'use strict'; /** * 扩大 wms_wave_batch.item_snapshot_key 列容量以容纳多商品订单的快照键。 * * 背景: * buildItemSnapshotKey 将订单中每个商品的 stock_id、item_code、 * item_name、barcode、quantity 用 :: 拼接,多个商品再用 | 连接。 * 当订单包含较多商品或商品名较长时,快照键极易超过原 VARCHAR(255) 上限, * 导致 INSERT 时报 "Data too long for column 'item_snapshot_key'"。 * * 变更: * 1. item_snapshot_key: VARCHAR(255) → VARCHAR(1024) * (与 item_detail_machine 容量保持一致) * 2. 索引 idx_wave_snapshot_key: 全列索引 → 前缀索引(191) * (与 idx_wave_item_machine_prefix 前缀长度一致, * 兼容 utf8mb4 下的 InnoDB 索引键长度限制) */ module.exports = { // eslint-disable-next-line no-unused-vars up: async (queryInterface, Sequelize) => { // ── 1. 先删旧全列索引(否则 changeColumn 1024 会触发 3072 字节限制)── try { await queryInterface.removeIndex('wms_wave_batch', 'idx_wave_snapshot_key'); console.log('[migrate] 已移除旧全列索引 idx_wave_snapshot_key'); } catch (e) { // 索引可能不存在(如全新库或已执行过),忽略 const msg = String(e.message || ''); if (!msg.includes('check that it exists') && !msg.includes('exist')) { console.warn('[migrate] 移除索引 idx_wave_snapshot_key 时出现非预期错误:', msg); } } // ── 2. 修改列容量 ── await queryInterface.changeColumn('wms_wave_batch', 'item_snapshot_key', { type: Sequelize.STRING(1024), allowNull: true, comment: '结构快照键(与 item_detail_machine 一致,1024 长度以容纳多商品订单的快照)', }); console.log('[migrate] wms_wave_batch.item_snapshot_key VARCHAR(255) → VARCHAR(1024)'); // ── 3. 创建前缀索引 ── try { await queryInterface.sequelize.query( 'ALTER TABLE wms_wave_batch ADD INDEX idx_wave_snapshot_key (item_snapshot_key(191))' ); console.log('[migrate] 已创建前缀索引 idx_wave_snapshot_key(item_snapshot_key(191))'); } catch (e) { if (e.message && e.message.includes('Duplicate')) { console.log('[migrate] 前缀索引 idx_wave_snapshot_key 已存在,跳过'); } else { throw e; } } }, // eslint-disable-next-line no-unused-vars down: async (queryInterface, Sequelize) => { // ── 还原索引为全列索引 ── try { await queryInterface.removeIndex('wms_wave_batch', 'idx_wave_snapshot_key'); console.log('[migrate:down] 已移除前缀索引 idx_wave_snapshot_key'); } catch (e) { if (!(e.message && e.message.includes('check that it exists'))) { console.warn('[migrate:down] 移除索引时出现非预期错误:', e.message); } } try { await queryInterface.sequelize.query( 'ALTER TABLE wms_wave_batch ADD INDEX idx_wave_snapshot_key (item_snapshot_key)' ); console.log('[migrate:down] 已还原全列索引 idx_wave_snapshot_key'); } catch (e) { if (e.message && e.message.includes('Duplicate')) { console.log('[migrate:down] 全列索引 idx_wave_snapshot_key 已存在,跳过'); } else { throw e; } } // ── 还原列容量 ── await queryInterface.changeColumn('wms_wave_batch', 'item_snapshot_key', { type: Sequelize.STRING(255), allowNull: true, comment: '结构快照键', }); console.log('[migrate:down] wms_wave_batch.item_snapshot_key VARCHAR(1024) → VARCHAR(255)'); }, };