161 lines
4.4 KiB
JavaScript
161 lines
4.4 KiB
JavaScript
'use strict';
|
||
|
||
const TABLE_NAME = 'wms_express';
|
||
const OLD_CODE = 'DYXD';
|
||
const NEW_CODE = 'DY';
|
||
|
||
/**
|
||
* 规范化 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 hasTargetTable = async queryInterface => {
|
||
const allTables = await queryInterface.showAllTables();
|
||
const tableSet = new Set((allTables || []).map(normalizeTableName));
|
||
return tableSet.has(TABLE_NAME);
|
||
};
|
||
|
||
/**
|
||
* 读取指定编码的快递记录。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @param {string} code 快递编码。
|
||
* @return {Promise<Object|null>} 快递记录;不存在时返回 null。
|
||
*/
|
||
const findExpressByCode = async (queryInterface, code) => {
|
||
const rows = await queryInterface.sequelize.query(
|
||
`SELECT * FROM ${TABLE_NAME} WHERE code = :code LIMIT 1`,
|
||
{
|
||
type: queryInterface.sequelize.QueryTypes.SELECT,
|
||
replacements: { code },
|
||
}
|
||
);
|
||
|
||
return rows && rows[0] ? rows[0] : null;
|
||
};
|
||
|
||
/**
|
||
* 升级迁移:将 wms_express.code 从 DYXD 迁移为 DY。
|
||
*
|
||
* 冲突处理策略:
|
||
* 1. 仅存在 DYXD:直接改为 DY;
|
||
* 2. 同时存在 DY 与 DYXD:将 DYXD 的空值补齐到 DY,再删除 DYXD;
|
||
* 3. 不存在 DYXD:视为已完成,保持幂等。
|
||
*
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @param {typeof import('sequelize')} Sequelize Sequelize 类型集合。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
module.exports.up = async (queryInterface, Sequelize) => {
|
||
void Sequelize;
|
||
|
||
if (!(await hasTargetTable(queryInterface))) {
|
||
return;
|
||
}
|
||
|
||
const oldRow = await findExpressByCode(queryInterface, OLD_CODE);
|
||
if (!oldRow) {
|
||
return;
|
||
}
|
||
|
||
const newRow = await findExpressByCode(queryInterface, NEW_CODE);
|
||
|
||
if (!newRow) {
|
||
await queryInterface.sequelize.query(
|
||
`UPDATE ${TABLE_NAME} SET code = :newCode WHERE code = :oldCode`,
|
||
{
|
||
replacements: {
|
||
newCode: NEW_CODE,
|
||
oldCode: OLD_CODE,
|
||
},
|
||
}
|
||
);
|
||
return;
|
||
}
|
||
|
||
await queryInterface.sequelize.transaction(async transaction => {
|
||
await queryInterface.sequelize.query(
|
||
`UPDATE ${TABLE_NAME}
|
||
SET
|
||
name = COALESCE(NULLIF(name, ''), :oldName),
|
||
KS = COALESCE(NULLIF(KS, ''), :oldKS),
|
||
PDD = COALESCE(NULLIF(PDD, ''), :oldPDD),
|
||
DYXD = COALESCE(NULLIF(DYXD, ''), :oldDYXD),
|
||
CAINIAO = COALESCE(NULLIF(CAINIAO, ''), :oldCAINIAO),
|
||
polyapi_extra_info = COALESCE(polyapi_extra_info, :oldPolyapiExtraInfo)
|
||
WHERE code = :newCode`,
|
||
{
|
||
replacements: {
|
||
newCode: NEW_CODE,
|
||
oldName: oldRow.name,
|
||
oldKS: oldRow.KS,
|
||
oldPDD: oldRow.PDD,
|
||
oldDYXD: oldRow.DYXD,
|
||
oldCAINIAO: oldRow.CAINIAO,
|
||
oldPolyapiExtraInfo: oldRow.polyapi_extra_info,
|
||
},
|
||
transaction,
|
||
}
|
||
);
|
||
|
||
await queryInterface.sequelize.query(
|
||
`DELETE FROM ${TABLE_NAME} WHERE code = :oldCode`,
|
||
{
|
||
replacements: { oldCode: OLD_CODE },
|
||
transaction,
|
||
}
|
||
);
|
||
});
|
||
};
|
||
|
||
/**
|
||
* 回滚迁移:将 wms_express.code 从 DY 恢复为 DYXD。
|
||
*
|
||
* 仅在 DYXD 不存在时执行恢复;若已存在 DYXD 则跳过,避免主键冲突。
|
||
*
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @param {typeof import('sequelize')} Sequelize Sequelize 类型集合。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
module.exports.down = async (queryInterface, Sequelize) => {
|
||
void Sequelize;
|
||
|
||
if (!(await hasTargetTable(queryInterface))) {
|
||
return;
|
||
}
|
||
|
||
const oldRow = await findExpressByCode(queryInterface, OLD_CODE);
|
||
if (oldRow) {
|
||
return;
|
||
}
|
||
|
||
const newRow = await findExpressByCode(queryInterface, NEW_CODE);
|
||
if (!newRow) {
|
||
return;
|
||
}
|
||
|
||
await queryInterface.sequelize.query(
|
||
`UPDATE ${TABLE_NAME} SET code = :oldCode WHERE code = :newCode`,
|
||
{
|
||
replacements: {
|
||
oldCode: OLD_CODE,
|
||
newCode: NEW_CODE,
|
||
},
|
||
}
|
||
);
|
||
};
|