'use strict'; /** * 新增 storage.inventory.directPutaway(收货单直入库存)API 权限记录。 * * 幂等设计:先查询 path + type='api' 是否已存在,已存在则跳过。 */ const TABLE = 'wms_permission'; module.exports.up = async queryInterface => { const permissions = [ { path: 'storage.inventory.directPutaway', meta_title: '收货单直入库存' }, ]; const paths = permissions.map(p => p.path); const placeholders = paths.map(() => '?').join(', '); const [ rows ] = await queryInterface.sequelize.query( `SELECT path FROM \`${TABLE}\` WHERE type = 'api' AND path IN (${placeholders})`, { replacements: paths } ); const existingPaths = new Set(rows.map(r => r.path)); const toInsert = permissions.filter(p => !existingPaths.has(p.path)); if (toInsert.length > 0) { const values = toInsert .map( p => `('${p.path.replace(/'/g, "\\'")}', '${p.path.replace(/'/g, "\\'")}', '${p.meta_title.replace(/'/g, "\\'")}', 'api', 1, NOW(), NOW())` ) .join(',\n'); await queryInterface.sequelize.query(` INSERT INTO \`${TABLE}\` (path, name, meta_title, type, status, created_at, update_at) VALUES ${values} `); } console.log( `[Migration] wms_permission API 权限新增:共 ${permissions.length} 条,` + `已存在 ${permissions.length - toInsert.length} 条,实际插入 ${toInsert.length} 条` ); }; module.exports.down = async queryInterface => { const paths = [ 'storage.inventory.directPutaway' ]; const placeholders = paths.map(() => '?').join(', '); await queryInterface.sequelize.query( `DELETE FROM \`${TABLE}\` WHERE type = 'api' AND path IN (${placeholders})`, { replacements: paths } ); };