WMS_MAIN/database/migrations/20260703000001-migrate-binding-data-to-inbound-direct-log.js
liufumingren 478ad2ab0d 1
2026-07-03 16:10:46 +08:00

185 lines
6.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 数据迁移:将 wms_entry_receiving_binding 存量数据迁移到 wms_inbound_direct_log。
*
* 背景:
* - wms_entry_receiving_binding 的"入库明细 ↔ 收货明细"绑定能力已迁移到 wms_inbound_direct_log。
* - 本 migration 负责将 wms_entry_receiving_binding 中的历史数据同步到 wms_inbound_direct_log。
*
* 字段映射(source → target):
* - entry_item_id / receiving_detail_id / allocated_quantity→quantity / status / creator_id→operator_id
* - created_at→created_at / updated_at→updated_at / deleted_at→deleted_at
*
* 关联补全:
* - entry_order_id / entry_order_code / warehouse_id / store_id:通过 wms_entry_items + wms_entry_order
* - stock_id:从 wms_entry_items
* - inbound_type:固定 'pda'
* - space_id:固定 0
*
* @description 迁移脚本:20260703000001
*/
/** 目标表名 */
const TARGET_TABLE = 'wms_inbound_direct_log';
/** 源表名 */
const SOURCE_TABLE = 'wms_entry_receiving_binding';
/**
* 规范化 showAllTables 返回的表名。
* @param {string|Object} table 表名或表对象。
* @return {string} 规范化后的表名。
*/
const normalizeTableName = table => {
if (typeof table === 'string') return table;
return table && typeof table === 'object' ? (table.tableName || table.name || '') : '';
};
/**
* 检查源表是否存在。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<boolean>} 源表是否存在。
*/
const hasSourceTable = async queryInterface => {
const allTables = await queryInterface.showAllTables();
const tableSet = new Set((allTables || []).map(normalizeTableName));
return tableSet.has(SOURCE_TABLE);
};
/**
* 检查目标表是否存在。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<boolean>} 目标表是否存在。
*/
const hasTargetTable = async queryInterface => {
const allTables = await queryInterface.showAllTables();
const tableSet = new Set((allTables || []).map(normalizeTableName));
return tableSet.has(TARGET_TABLE);
};
/**
* 统计源表总行数。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<number>} 源表总行数。
*/
const countSourceRows = async queryInterface => {
const [ rows ] = await queryInterface.sequelize.query(
`SELECT COUNT(*) AS cnt FROM ${SOURCE_TABLE}`,
{ type: queryInterface.sequelize.QueryTypes.SELECT }
);
return rows ? rows.cnt : 0;
};
/**
* 正向迁移:从 wms_entry_receiving_binding 迁移数据到 wms_inbound_direct_log。
*
* 去重策略:按 (entry_item_id, receiving_detail_id, status) GROUP BY 去重,
* 重复组保留 id 最小的一条,其余跳过并打印数量。
*
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {typeof import('sequelize')} Sequelize Sequelize 类型集合。
* @return {Promise<void>} 无返回值。
*/
module.exports.up = async (queryInterface, Sequelize) => {
void Sequelize;
// 幂等:源表不存在则跳过
if (!(await hasSourceTable(queryInterface))) {
console.log('wms_entry_receiving_binding 表不存在,跳过迁移');
return;
}
// 幂等:目标表不存在则跳过
if (!(await hasTargetTable(queryInterface))) {
console.log('wms_inbound_direct_log 表不存在,跳过迁移');
return;
}
const totalRows = await countSourceRows(queryInterface);
console.log(`wms_entry_receiving_binding 总行数:${totalRows}`);
if (totalRows === 0) {
console.log('无数据需要迁移,跳过');
return;
}
// 统计去重后跳过的行数
const [ dupResult ] = await queryInterface.sequelize.query(
`SELECT COUNT(*) AS skip_cnt
FROM ${SOURCE_TABLE}
WHERE id NOT IN (
SELECT MIN(id)
FROM ${SOURCE_TABLE}
GROUP BY entry_item_id, receiving_detail_id, status
)`,
{ type: queryInterface.sequelize.QueryTypes.SELECT }
);
const skipCount = dupResult ? dupResult.skip_cnt : 0;
if (skipCount > 0) {
console.log(`检测到 ${skipCount} 条重复记录(按 entry_item_id + receiving_detail_id + status 分组去重),将被跳过`);
}
// 执行 INSERT INTO ... SELECT
const [ insertResult ] = await queryInterface.sequelize.query(
`INSERT INTO ${TARGET_TABLE}
(entry_item_id, receiving_detail_id, quantity, status,
operator_id, created_at, updated_at, deleted_at,
entry_order_id, entry_order_code, warehouse_id, store_id,
stock_id, inbound_type, space_id)
SELECT
b.entry_item_id,
b.receiving_detail_id,
b.allocated_quantity,
b.status,
b.creator_id,
b.created_at,
b.updated_at,
b.deleted_at,
ei.order_id,
eo.entry_order_code,
eo.warehouse_id,
eo.store_id,
ei.stock_id,
'pda',
0
FROM ${SOURCE_TABLE} b
LEFT JOIN wms_entry_items ei ON b.entry_item_id = ei.id
LEFT JOIN wms_entry_order eo ON ei.order_id = eo.id
WHERE b.id IN (
SELECT MIN(id)
FROM ${SOURCE_TABLE}
GROUP BY entry_item_id, receiving_detail_id, status
)`,
{ type: queryInterface.sequelize.QueryTypes.INSERT }
);
const insertedCount = insertResult ? (Array.isArray(insertResult) ? insertResult.length : insertResult[1]) : 0;
console.log(`迁移完成:成功插入 ${insertedCount} 行`);
};
/**
* 回滚迁移:删除从 wms_entry_receiving_binding 迁移过来的数据。
*
* 删除条件:receiving_detail_id IS NOT NULL AND entry_item_id IS NOT NULL
* (仅删除有绑定关系的记录,不影响原有 direct-log 数据)
*
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<void>} 无返回值。
*/
module.exports.down = async queryInterface => {
if (!(await hasTargetTable(queryInterface))) {
console.log('wms_inbound_direct_log 表不存在,跳过回滚');
return;
}
const [ deleteResult ] = await queryInterface.sequelize.query(
`DELETE FROM ${TARGET_TABLE}
WHERE receiving_detail_id IS NOT NULL
AND entry_item_id IS NOT NULL`,
{ type: queryInterface.sequelize.QueryTypes.DELETE }
);
const deletedCount = deleteResult || 0;
console.log(`回滚完成:删除 ${deletedCount} 行`);
};