fix(order): wms_order_items.ext_code 改为 TEXT 支持超长外部编码
This commit is contained in:
parent
ac4222c49a
commit
864351977b
@ -3,7 +3,7 @@
|
||||
|
||||
module.exports = app => {
|
||||
const { Sequelize, model } = app;
|
||||
const { STRING, BIGINT, DECIMAL, INTEGER, JSON } = Sequelize;
|
||||
const { STRING, BIGINT, DECIMAL, INTEGER, JSON, TEXT } = Sequelize;
|
||||
|
||||
const OrderItems = model.define('wms_order_items', {
|
||||
id: {
|
||||
@ -28,7 +28,7 @@ module.exports = app => {
|
||||
comment: '零售价格',
|
||||
},
|
||||
ext_code: {
|
||||
type: STRING(50),
|
||||
type: TEXT,
|
||||
allowNull: true,
|
||||
comment: '外部编码',
|
||||
},
|
||||
|
||||
@ -0,0 +1,79 @@
|
||||
'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 '外部编码'`
|
||||
);
|
||||
}
|
||||
},
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user