108 lines
3.5 KiB
JavaScript
108 lines
3.5 KiB
JavaScript
'use strict';
|
||
|
||
const TABLE_NAME = 'wms_order_delivery';
|
||
const TABLE_LIKE_PATTERN = 'wms_order_delivery\\_%';
|
||
const COLUMN_NAME = 'source_order_codes';
|
||
const COLUMN_DDL = "TEXT NULL COMMENT '来源订单号集合'";
|
||
const PREFERRED_AFTER = 'source_platform_name';
|
||
|
||
/**
|
||
* 获取发货单主表及所有分表名称。
|
||
* @param {import('sequelize').QueryInterface} queryInterface Sequelize 查询接口。
|
||
* @return {Promise<Array<Object>>} 表名查询结果。
|
||
*/
|
||
async function getOrderDeliveryTables(queryInterface) {
|
||
const [ tables ] = await queryInterface.sequelize.query(`
|
||
SELECT TABLE_NAME
|
||
FROM INFORMATION_SCHEMA.TABLES
|
||
WHERE TABLE_SCHEMA = DATABASE()
|
||
AND (TABLE_NAME = '${TABLE_NAME}' OR TABLE_NAME LIKE '${TABLE_LIKE_PATTERN}')
|
||
`);
|
||
|
||
return tables;
|
||
}
|
||
|
||
/**
|
||
* 判断表中是否存在指定字段。
|
||
* @param {import('sequelize').QueryInterface} queryInterface Sequelize 查询接口。
|
||
* @param {string} tableName 表名。
|
||
* @param {string} columnName 字段名。
|
||
* @return {Promise<boolean>} 是否存在字段。
|
||
*/
|
||
async function hasColumn(queryInterface, tableName, columnName) {
|
||
const [ columns ] = await queryInterface.sequelize.query(
|
||
`
|
||
SELECT COLUMN_NAME
|
||
FROM INFORMATION_SCHEMA.COLUMNS
|
||
WHERE TABLE_SCHEMA = DATABASE()
|
||
AND TABLE_NAME = :tableName
|
||
AND COLUMN_NAME = :columnName
|
||
`,
|
||
{ replacements: { tableName, columnName } }
|
||
);
|
||
|
||
return columns.length > 0;
|
||
}
|
||
|
||
/**
|
||
* 解析 ADD COLUMN 的 AFTER 子句。
|
||
* 当目标表存在 `source_platform_name` 时,优先追加到其后;否则返回空串,直接追加到表尾。
|
||
* @param {import('sequelize').QueryInterface} queryInterface Sequelize 查询接口。
|
||
* @param {string} tableName 表名。
|
||
* @return {Promise<string>} AFTER 子句或空字符串。
|
||
*/
|
||
async function resolveAfterClause(queryInterface, tableName) {
|
||
if (await hasColumn(queryInterface, tableName, PREFERRED_AFTER)) {
|
||
return ` AFTER \`${PREFERRED_AFTER}\``;
|
||
}
|
||
|
||
return '';
|
||
}
|
||
|
||
module.exports = {
|
||
/**
|
||
* 为发货单主表及分表补齐 `source_order_codes` 字段。
|
||
* 仅在字段缺失时执行 ADD COLUMN,并尽量放在 `source_platform_name` 后面。
|
||
*
|
||
* @param {import('sequelize').QueryInterface} queryInterface Sequelize 查询接口。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async up(queryInterface) {
|
||
const tables = await getOrderDeliveryTables(queryInterface);
|
||
|
||
for (const row of tables) {
|
||
const tableName = row.TABLE_NAME;
|
||
|
||
if (await hasColumn(queryInterface, tableName, COLUMN_NAME)) {
|
||
continue;
|
||
}
|
||
|
||
const afterClause = await resolveAfterClause(queryInterface, tableName);
|
||
await queryInterface.sequelize.query(
|
||
`ALTER TABLE \`${tableName}\` ADD COLUMN \`${COLUMN_NAME}\` ${COLUMN_DDL}${afterClause}`
|
||
);
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 回滚删除发货单主表及分表的 `source_order_codes` 字段。
|
||
* 仅在字段存在时执行 DROP COLUMN,保证回滚幂等。
|
||
*
|
||
* @param {import('sequelize').QueryInterface} queryInterface Sequelize 查询接口。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async down(queryInterface) {
|
||
const tables = await getOrderDeliveryTables(queryInterface);
|
||
|
||
for (const row of tables) {
|
||
const tableName = row.TABLE_NAME;
|
||
|
||
if (await hasColumn(queryInterface, tableName, COLUMN_NAME)) {
|
||
await queryInterface.sequelize.query(
|
||
`ALTER TABLE \`${tableName}\` DROP COLUMN \`${COLUMN_NAME}\``
|
||
);
|
||
}
|
||
}
|
||
},
|
||
};
|