'use strict'; const TABLE_NAME = 'wms_warehouse_front_config'; const CERT_PEM_COLUMN = 'cert_pem'; const CERT_KEY_COLUMN = 'cert_key'; /** * Migration: 仓库前端配置表新增证书内容列(证书 PEM 文本直接入库)。 * * 背景: * 原方案证书内容只存路径(cert_path/key_path),由保存接口写暂存文件后部署; * 现改为证书 PEM 文本直接入库,由定时任务拉取后写文件部署。 * 证书链 fullchain PEM 一般 2~10KB,TEXT(上限 64KB)足够,且项目内长文本 * 列惯例使用 TEXT(如 20260818190000 wms_order_items.ext_code 改 TEXT); * 不选 VARCHAR(16384):utf8mb4 下 16K 字符已触发 MySQL 5.7 行大小限制风险, * 且 PEM 长度无硬上限,TEXT 更稳。 * * 列说明: * - cert_pem:证书 PEM 内容(私钥不在内); * - cert_key:私钥 PEM 内容; * - 均允许为 NULL(未上传证书时为空),无需索引; * - 敏感内容:仅由定时任务读取,禁止写入日志/接口响应。 * * Idempotent: up 列已存在时跳过、down 列不存在时跳过。 */ /** * 判断表是否存在。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {string} tableName 表名。 * @return {Promise} 存在返回 true。 */ async function tableExists(queryInterface, tableName) { const tables = await queryInterface.showAllTables(); return tables.some(t => (typeof t === 'string' ? t : (t.tableName || t.name)) === tableName); } module.exports = { /** * 执行正向迁移:新增 cert_pem / cert_key 两列(幂等)。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {import('sequelize')} Sequelize Sequelize 实例。 * @return {Promise} 无返回值。 */ async up(queryInterface, Sequelize) { if (!(await tableExists(queryInterface, TABLE_NAME))) { return; } const columns = await queryInterface.describeTable(TABLE_NAME); if (!columns[CERT_PEM_COLUMN]) { await queryInterface.addColumn(TABLE_NAME, CERT_PEM_COLUMN, { type: Sequelize.TEXT, allowNull: true, comment: '证书 PEM 内容(私钥不在内);内容仅由定时任务读取,禁止写入日志/接口响应', after: 'cert_path', }); } if (!columns[CERT_KEY_COLUMN]) { await queryInterface.addColumn(TABLE_NAME, CERT_KEY_COLUMN, { type: Sequelize.TEXT, allowNull: true, comment: '私钥 PEM 内容;内容仅由定时任务读取,禁止写入日志/接口响应', after: 'key_path', }); } }, /** * 回滚迁移:删除 cert_pem / cert_key 两列(幂等)。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise} 无返回值。 */ async down(queryInterface) { if (!(await tableExists(queryInterface, TABLE_NAME))) { return; } const columns = await queryInterface.describeTable(TABLE_NAME); if (columns[CERT_PEM_COLUMN]) { await queryInterface.removeColumn(TABLE_NAME, CERT_PEM_COLUMN); } if (columns[CERT_KEY_COLUMN]) { await queryInterface.removeColumn(TABLE_NAME, CERT_KEY_COLUMN); } }, };