167 lines
8.6 KiB
JavaScript
167 lines
8.6 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 报表定义模型重构:code 关联 → 主键 id 关联。
|
||
*
|
||
* 背景:报表定义模型已重构(definition 以主键 id 唯一,删除 code/scope/description
|
||
* 及 output_config/source_config/sheet_config 三个 JSON 配置列,新增 type/file_name/fields;
|
||
* plan/log 改用 report_definition_id 关联定义),本迁移同步数据库结构。
|
||
*
|
||
* 三步改造:
|
||
* 1. wms_report_definition:删 uk_code,加 type/file_name/fields 三列,回填 id=1
|
||
* 存量数据,物理删除已软删的 e2e 演示行(id=2),删除 code/scope/description
|
||
* 及三个 JSON 配置列,最后去掉 type 默认值(新配置必须显式声明类型);
|
||
* 2. wms_report_delivery_plan:加 report_definition_id 并回填,唯一键
|
||
* uk_store_report_code → uk_store_report_definition;
|
||
* 3. wms_report_delivery_log:加 report_definition_id/report_name 并回填,
|
||
* 唯一键 uk_store_report_biz_time 改为基于 report_definition_id。
|
||
*
|
||
* 执行顺序说明(关键约束):plan/log 的回填依赖 definition.code 关联
|
||
* (JOIN ... ON d.code = p.report_code),因此 definition 旧列(含 code)的删除
|
||
* 被延后到 plan/log 回填核验通过之后执行,最终表结构与规格逐项一致。
|
||
*
|
||
* down 不可逆:旧列及其数据已被物理删除,无法从库内还原,故 down 直接抛错,
|
||
* 如需回退请从备份恢复。
|
||
*/
|
||
|
||
const TABLE_DEFINITION = 'wms_report_definition';
|
||
const TABLE_PLAN = 'wms_report_delivery_plan';
|
||
const TABLE_LOG = 'wms_report_delivery_log';
|
||
|
||
// id=1 存量报表勾选字段 key 数组(与代码侧 stockFlow 查询器字段规格一致)
|
||
const STOCK_FLOW_FIELDS = [
|
||
'item_code', 'item_name', 'barcode', 'daily', 'ck_total', 'rk_total', 'available',
|
||
'total', 'pending', 'locked', 'ck_avg', 'rk_avg', 'stock_days', 'suggest', 'warn',
|
||
];
|
||
|
||
module.exports = {
|
||
async up(queryInterface) {
|
||
const { sequelize } = queryInterface;
|
||
const q = sql => sequelize.query(sql);
|
||
const log = msg => console.log(`[report-definition-by-id] ${msg}`);
|
||
|
||
/**
|
||
* 执行计数校验 SQL,返回行数;不满足期望时抛错,阻止后续破坏性 DDL。
|
||
* @param {string} sql 返回 cnt 列的 SELECT COUNT 语句。
|
||
* @param {number} expected 期望的行数。
|
||
* @param {string} message 校验失败时的错误描述。
|
||
*/
|
||
const assertCount = async (sql, expected, message) => {
|
||
const [ rows ] = await q(sql);
|
||
const count = Number(rows[0].cnt);
|
||
console.log(`[report-definition-by-id] 核验:${message}(实际 ${count},期望 ${expected})`);
|
||
if (count !== expected) {
|
||
throw new Error(`[report-definition-by-id] ${message} 核验失败:实际 ${count},期望 ${expected}`);
|
||
}
|
||
};
|
||
|
||
// ============ Step 1(前半):wms_report_definition 结构准备 ============
|
||
// 1.1 删除 uk_code 唯一索引(code 即将废弃)
|
||
await queryInterface.removeIndex(TABLE_DEFINITION, 'uk_code');
|
||
log('definition:已删除唯一索引 uk_code');
|
||
|
||
// 1.2 新增 type / file_name / fields 三列(按规格顺序放在 name 之后)
|
||
await q(`ALTER TABLE \`${TABLE_DEFINITION}\`
|
||
ADD COLUMN \`type\` VARCHAR(50) NOT NULL DEFAULT 'stockFlow' COMMENT '报表类型(内置查询器注册 key)' AFTER \`name\`,
|
||
ADD COLUMN \`file_name\` VARCHAR(200) NOT NULL DEFAULT '' COMMENT '导出文件名模板(支持 {date} 占位)' AFTER \`type\`,
|
||
ADD COLUMN \`fields\` JSON NULL COMMENT '勾选字段 key 数组' AFTER \`file_name\``);
|
||
log('definition:已新增 type/file_name/fields 三列');
|
||
|
||
// 1.3 回填 id=1 存量数据(7日出入库报表)
|
||
await q(`UPDATE \`${TABLE_DEFINITION}\` SET
|
||
\`type\` = 'stockFlow',
|
||
\`file_name\` = '7日出入库_{date}',
|
||
\`fields\` = JSON_ARRAY(${STOCK_FLOW_FIELDS.map(f => `'${f}'`).join(', ')})
|
||
WHERE \`id\` = 1`);
|
||
log('definition:已回填 id=1 的 type/file_name/fields');
|
||
|
||
// 1.4 物理删除已软删的 e2e 演示行(id=2)
|
||
await q(`DELETE FROM \`${TABLE_DEFINITION}\`
|
||
WHERE \`id\` = 2 AND \`deleted_at\` IS NOT NULL AND \`code\` = 'e2e_demo_report'`);
|
||
log('definition:已删除软删状态下的 e2e 演示行(id=2)');
|
||
|
||
// ============ Step 2:wms_report_delivery_plan 改 id 关联 ============
|
||
// 2.1 新增 report_definition_id(按规格顺序放在 store_id 之后)
|
||
await q(`ALTER TABLE \`${TABLE_PLAN}\`
|
||
ADD COLUMN \`report_definition_id\` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0
|
||
COMMENT '报表定义ID(wms_report_definition.id)' AFTER \`store_id\``);
|
||
log('plan:已新增 report_definition_id');
|
||
|
||
// 2.2 按 code 关联回填(必须在 definition.code 删除前完成)
|
||
await q(`UPDATE \`${TABLE_PLAN}\` p
|
||
JOIN \`${TABLE_DEFINITION}\` d ON d.\`code\` = p.\`report_code\`
|
||
SET p.\`report_definition_id\` = d.\`id\``);
|
||
log('plan:已按 code 关联回填 report_definition_id');
|
||
|
||
// 2.3 核验:report_definition_id=0 的行数必须为 0,否则抛出异常
|
||
await assertCount(
|
||
`SELECT COUNT(*) AS cnt FROM \`${TABLE_PLAN}\` WHERE \`report_definition_id\` = 0`,
|
||
0,
|
||
'plan 回填后 report_definition_id=0 的行数必须为 0'
|
||
);
|
||
|
||
// 2.4 删除旧唯一键与 report_code,建立新唯一键
|
||
await queryInterface.removeIndex(TABLE_PLAN, 'uk_store_report_code');
|
||
await q(`ALTER TABLE \`${TABLE_PLAN}\` DROP COLUMN \`report_code\``);
|
||
await q(`ALTER TABLE \`${TABLE_PLAN}\`
|
||
ADD UNIQUE INDEX \`uk_store_report_definition\` (\`store_id\`, \`report_definition_id\`, \`deleted_at\`)`);
|
||
log('plan:已切换唯一键 uk_store_report_definition 并删除 report_code');
|
||
|
||
// ============ Step 3:wms_report_delivery_log 改 id 关联 ============
|
||
// 3.1 新增 report_definition_id / report_name(按规格顺序放在 store_id 之后)
|
||
await q(`ALTER TABLE \`${TABLE_LOG}\`
|
||
ADD COLUMN \`report_definition_id\` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0
|
||
COMMENT '报表定义ID(wms_report_definition.id)' AFTER \`store_id\`,
|
||
ADD COLUMN \`report_name\` VARCHAR(100) NULL
|
||
COMMENT '报表名称快照(投递时定义 name)' AFTER \`report_definition_id\``);
|
||
log('log:已新增 report_definition_id/report_name');
|
||
|
||
// 3.2 按 code 关联回填(必须在 definition.code 删除前完成)
|
||
await q(`UPDATE \`${TABLE_LOG}\` l
|
||
JOIN \`${TABLE_DEFINITION}\` d ON d.\`code\` = l.\`report_code\`
|
||
SET l.\`report_definition_id\` = d.\`id\`, l.\`report_name\` = d.\`name\``);
|
||
log('log:已按 code 关联回填 report_definition_id/report_name');
|
||
|
||
// 3.3 核验:report_definition_id=0 的行数必须为 0,否则抛出异常
|
||
await assertCount(
|
||
`SELECT COUNT(*) AS cnt FROM \`${TABLE_LOG}\` WHERE \`report_definition_id\` = 0`,
|
||
0,
|
||
'log 回填后 report_definition_id=0 的行数必须为 0'
|
||
);
|
||
|
||
// 3.4 删除旧唯一键与 report_code,建立新唯一键
|
||
await queryInterface.removeIndex(TABLE_LOG, 'uk_store_report_biz_time');
|
||
await q(`ALTER TABLE \`${TABLE_LOG}\` DROP COLUMN \`report_code\``);
|
||
await q(`ALTER TABLE \`${TABLE_LOG}\`
|
||
ADD UNIQUE INDEX \`uk_store_report_biz_time\`
|
||
(\`store_id\`, \`report_definition_id\`, \`biz_date\`, \`time_point\`, \`deleted_at\`)`);
|
||
log('log:已切换唯一键 uk_store_report_biz_time 并删除 report_code');
|
||
|
||
// ============ Step 1(后半):删除 definition 旧列并收尾 ============
|
||
// plan/log 回填已核验通过,此时删除 definition 旧列(含 code)是安全的
|
||
await q(`ALTER TABLE \`${TABLE_DEFINITION}\`
|
||
DROP COLUMN \`code\`,
|
||
DROP COLUMN \`scope\`,
|
||
DROP COLUMN \`description\`,
|
||
DROP COLUMN \`output_config\`,
|
||
DROP COLUMN \`source_config\`,
|
||
DROP COLUMN \`sheet_config\``);
|
||
log('definition:已删除 code/scope/description 及三个 JSON 配置列');
|
||
|
||
// 去掉 type 默认值:新配置行必须显式声明报表类型
|
||
await q(`ALTER TABLE \`${TABLE_DEFINITION}\` ALTER COLUMN \`type\` DROP DEFAULT`);
|
||
log('definition:已去掉 type 默认值');
|
||
|
||
log('全部完成');
|
||
},
|
||
|
||
async down() {
|
||
// 本迁移破坏性删除旧列数据(code/scope/description 及三个 JSON 配置列、
|
||
// e2e 演示行、plan/log 的 report_code 列),库内已无原始数据可还原,不可自动回滚。
|
||
throw new Error(
|
||
'[report-definition-by-id] 本迁移不可逆:definition 旧列及其数据、e2e 演示行、plan/log 的 report_code 列已被删除,'
|
||
+ '库内无法还原原始数据。如需回退,请从备份恢复数据库或手工重建相关列与数据。'
|
||
);
|
||
},
|
||
};
|