80 lines
2.6 KiB
JavaScript
80 lines
2.6 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 将订单商品表 ext_code 列从 VARCHAR(50) 改为 TEXT。
|
||
*
|
||
* 背景:
|
||
* wms_order_items.ext_code 原为 VARCHAR(50),上游平台(奇门/仓库接口)下发的
|
||
* 商品外部编码可能超过 50 字符,INSERT 时报
|
||
* "Data too long for column 'ext_code' at row 1",导致整个订单创建失败。
|
||
*
|
||
* 变更:
|
||
* 1. 主表 wms_order_items 及全部已有分表 wms_order_items_{warehouseId}
|
||
* (含 _dispatch/_history 后缀)的 ext_code 列统一改为 TEXT。
|
||
* 2. 模型定义同步改为 TEXT(app/model/order_items.js),后续新建分表直接
|
||
* 使用 TEXT,OrderItemsFactory 的结构对齐逻辑不会再回退该列。
|
||
*
|
||
* @param {import('sequelize').QueryInterface} queryInterface Sequelize 查询接口
|
||
* @return {Promise<void>} 无返回值
|
||
*/
|
||
async function listOrderItemsTables(queryInterface) {
|
||
const [ rows ] = await queryInterface.sequelize.query(`
|
||
SELECT TABLE_NAME
|
||
FROM INFORMATION_SCHEMA.TABLES
|
||
WHERE TABLE_SCHEMA = DATABASE()
|
||
AND (TABLE_NAME = 'wms_order_items' OR TABLE_NAME LIKE 'wms_order_items\\_%')
|
||
`);
|
||
|
||
return (Array.isArray(rows) ? rows : []).map(row => row.TABLE_NAME);
|
||
}
|
||
|
||
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 (Array.isArray(columns) ? columns : []).length > 0;
|
||
}
|
||
|
||
module.exports = {
|
||
up: async queryInterface => {
|
||
const tableNames = await listOrderItemsTables(queryInterface);
|
||
let alteredCount = 0;
|
||
|
||
for (const tableName of tableNames) {
|
||
if (!(await hasColumn(queryInterface, tableName, 'ext_code'))) {
|
||
continue;
|
||
}
|
||
|
||
await queryInterface.sequelize.query(
|
||
`ALTER TABLE \`${tableName}\` MODIFY \`ext_code\` TEXT NULL COMMENT '外部编码'`
|
||
);
|
||
alteredCount += 1;
|
||
}
|
||
|
||
console.log(`[migrate] wms_order_items ext_code → TEXT,共处理 ${alteredCount} 张表`);
|
||
},
|
||
|
||
down: async queryInterface => {
|
||
const tableNames = await listOrderItemsTables(queryInterface);
|
||
|
||
for (const tableName of tableNames) {
|
||
if (!(await hasColumn(queryInterface, tableName, 'ext_code'))) {
|
||
continue;
|
||
}
|
||
|
||
// 注意:若已有数据超过 50 字符,回滚会失败,需先人工截断数据
|
||
await queryInterface.sequelize.query(
|
||
`ALTER TABLE \`${tableName}\` MODIFY \`ext_code\` VARCHAR(50) NULL COMMENT '外部编码'`
|
||
);
|
||
}
|
||
},
|
||
};
|