'use strict'; /** * 扫码记录:带上「当时的工作台快照」+ 运单查询要用的索引 * * 为什么要快照:录像回放要按**当时的**工作台配置(哪台录像机、哪些摄像头、怎么布局、 * 录制范围多少秒)去拉流。工作台后来被改/被删了,历史运单照样要能查回来, * 所以扫码入库时就把配置冻一份 JSON 存这里。 * * - `workstation_id`:便于按工作台统计/过滤(快照里也有,这个列只是方便 JOIN) * - `workstation_snapshot`:JSON,结构见 `app/service/pack.js` 的 `buildSnapshot()` * * 索引: * - `(subject_id, barcode)`:按运单号查(主体内精确匹配) * - `(scanner_id, channel_id, scan_time)`:散单要取同通道的「上一条/下一条」扫码时间 * * 注意:`sql/schema.sql` 基线已经包含这些列和索引,空库执行时必须幂等。 */ async function hasColumn(queryInterface, table, column) { try { const description = await queryInterface.describeTable(table); return Boolean(description[column]); } catch (error) { return false; } } async function hasIndex(queryInterface, table, name) { try { const indexes = await queryInterface.showIndex(table); return indexes.some(item => item.name === name); } catch (error) { return false; } } module.exports = { async up(queryInterface, Sequelize) { const { BIGINT, JSON: JSONType } = Sequelize; if (!await hasColumn(queryInterface, 'scan_records', 'workstation_id')) { await queryInterface.addColumn('scan_records', 'workstation_id', { type: BIGINT.UNSIGNED, allowNull: true, comment: '当时绑定的工作台 workstations.id(快照里有完整信息)', }); } if (!await hasColumn(queryInterface, 'scan_records', 'workstation_snapshot')) { await queryInterface.addColumn('scan_records', 'workstation_snapshot', { type: JSONType, allowNull: true, comment: '工作台快照(录像机/摄像头/布局/录制范围),回放按它拉流', }); } if (!await hasIndex(queryInterface, 'scan_records', 'idx_scan_subject_barcode')) { await queryInterface.addIndex('scan_records', [ 'subject_id', 'barcode' ], { name: 'idx_scan_subject_barcode', }); } if (!await hasIndex(queryInterface, 'scan_records', 'idx_scan_channel_seq')) { await queryInterface.addIndex('scan_records', [ 'scanner_id', 'channel_id', 'scan_time' ], { name: 'idx_scan_channel_seq', }); } // 中控上报时按 (scanner_id, channel_id) 找工作台,没索引会全表扫 if (!await hasIndex(queryInterface, 'workstation_scan_channels', 'idx_wsc_scanner_channel')) { await queryInterface.addIndex('workstation_scan_channels', [ 'scanner_id', 'channel_id' ], { name: 'idx_wsc_scanner_channel', }); } }, async down(queryInterface) { await queryInterface.removeIndex('workstation_scan_channels', 'idx_wsc_scanner_channel'); await queryInterface.removeIndex('scan_records', 'idx_scan_channel_seq'); await queryInterface.removeIndex('scan_records', 'idx_scan_subject_barcode'); await queryInterface.removeColumn('scan_records', 'workstation_snapshot'); await queryInterface.removeColumn('scan_records', 'workstation_id'); }, };