'use strict'; /** * 清理仓库接口配置项残留键:warehouse_apis.extra 中「已不在硬编码清单内」的键 * * 背景:`app/lib/warehouseApiFields.js` 是配置项的唯一来源(分组 + 字段清单 + 类型), * 字段值以 { 字段名: 值 } 落库在 `warehouse_apis.extra`。2026-09-14 按用户要求移除了 * 「基础信息」分组里从未被代码读取的三项(`owner_alias` 货主别名 / `outsource_api_type` 委外接口类型 / * `customer_id` 客户ID),但历史落库的 extra 里仍残留这些键(读取时已被 `readFieldValues` 过滤, * 保存时因整体覆盖也会消失)。本迁移做一次性修剪,使库内数据与清单一致。 * * 口径: * - 以当前清单的 key 集合为准,删除 extra 中**不在清单内**的所有键(幂等:无可删键时不做任何写库) * - 只动 `extra` 字段,不改 `updated_at` * - 不移除清单内字段的值(保留 `owner_code` 等在用配置) */ const { listFieldDefs } = require('../../app/lib/warehouseApiFields'); module.exports = { async up(queryInterface) { const sequelize = queryInterface.sequelize; const validKeys = new Set(listFieldDefs().map(field => field.key)); const rows = await sequelize.query('SELECT id, extra FROM warehouse_apis', { type: sequelize.QueryTypes.SELECT }); let prunedKeys = 0; let touchedRows = 0; for (const row of rows) { let extra = row.extra; if (typeof extra === 'string') { try { extra = JSON.parse(extra); } catch { extra = null; } } if (!extra || typeof extra !== 'object' || Array.isArray(extra)) continue; const stale = Object.keys(extra).filter(key => !validKeys.has(key)); if (!stale.length) continue; const next = { ...extra }; for (const key of stale) delete next[key]; await sequelize.query( 'UPDATE warehouse_apis SET extra = ? WHERE id = ?', { replacements: [ JSON.stringify(next), row.id ] } ); prunedKeys += stale.length; touchedRows += 1; } console.log(`[migration] warehouse_apis.extra 清理清单外配置项:${touchedRows} 行 / ${prunedKeys} 个键`); }, /** * 不可逆:被删的键已无任何代码引用(如确需恢复,在页面重新填写即可) */ async down() {}, };