233 lines
6.9 KiB
JavaScript
233 lines
6.9 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 获取当前数据库已存在的表名集合。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @return {Promise<Set<string>>} 表名集合。
|
||
*/
|
||
const getExistingTableSet = async queryInterface => {
|
||
const tables = await queryInterface.showAllTables();
|
||
return new Set(
|
||
tables.map(table => (typeof table === 'string' ? table : table.tableName))
|
||
);
|
||
};
|
||
|
||
/**
|
||
* 幂等添加 custom_code 字段和索引(适用于表已存在但缺少新字段的场景)。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @param {import('sequelize')} Sequelize Sequelize 实例。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
const ensureCustomCodeColumnAndIndex = async (queryInterface, Sequelize) => {
|
||
const tableInfo = await queryInterface.describeTable('wms_combo_goods');
|
||
if (!tableInfo.custom_code) {
|
||
await queryInterface.addColumn('wms_combo_goods', 'custom_code', {
|
||
type: Sequelize.STRING(64),
|
||
allowNull: true,
|
||
comment: '自定义编码(前端可输入,按货主唯一)',
|
||
});
|
||
}
|
||
|
||
const indexes = await queryInterface.showIndex('wms_combo_goods');
|
||
const hasIndex = indexes.some(idx => idx.name === 'uk_store_custom_code');
|
||
if (!hasIndex) {
|
||
await queryInterface.addIndex('wms_combo_goods', [ 'warehouse_id', 'custom_code' ], {
|
||
name: 'uk_store_custom_code',
|
||
});
|
||
}
|
||
};
|
||
|
||
/**
|
||
* 创建组合商品主表 `wms_combo_goods`。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @param {import('sequelize')} Sequelize Sequelize 实例。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
const createComboGoodsTable = async (queryInterface, Sequelize) => {
|
||
await queryInterface.createTable('wms_combo_goods', {
|
||
id: {
|
||
type: Sequelize.INTEGER(11),
|
||
primaryKey: true,
|
||
autoIncrement: true,
|
||
allowNull: false,
|
||
comment: '主键ID',
|
||
},
|
||
combo_code: {
|
||
type: Sequelize.STRING(64),
|
||
allowNull: false,
|
||
comment: '组合商品编码,业务唯一标识',
|
||
},
|
||
combo_name: {
|
||
type: Sequelize.STRING(128),
|
||
allowNull: false,
|
||
comment: '组合商品名称',
|
||
},
|
||
custom_code: {
|
||
type: Sequelize.STRING(64),
|
||
allowNull: true,
|
||
comment: '自定义编码(前端可输入,按货主唯一)',
|
||
},
|
||
combo_sku_id: {
|
||
type: Sequelize.INTEGER(11),
|
||
allowNull: true,
|
||
comment: '组合商品关联的 wms_goods_sku.id',
|
||
},
|
||
status: {
|
||
type: Sequelize.TINYINT(1),
|
||
allowNull: false,
|
||
defaultValue: 1,
|
||
comment: '状态:1-启用 0-停用',
|
||
},
|
||
warehouse_id: {
|
||
type: Sequelize.INTEGER(11),
|
||
allowNull: true,
|
||
comment: '绑定仓库ID,NULL表示全局',
|
||
},
|
||
remark: {
|
||
type: Sequelize.STRING(256),
|
||
allowNull: true,
|
||
comment: '备注',
|
||
},
|
||
creator_id: {
|
||
type: Sequelize.INTEGER(11),
|
||
allowNull: true,
|
||
comment: '创建人ID',
|
||
},
|
||
editor_id: {
|
||
type: Sequelize.INTEGER(11),
|
||
allowNull: true,
|
||
comment: '最后编辑人ID',
|
||
},
|
||
created_at: {
|
||
type: Sequelize.DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
|
||
comment: '创建时间',
|
||
},
|
||
updated_at: {
|
||
type: Sequelize.DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
|
||
comment: '更新时间',
|
||
},
|
||
});
|
||
|
||
await queryInterface.addIndex('wms_combo_goods', [ 'combo_code' ], {
|
||
unique: true,
|
||
name: 'uk_combo_code',
|
||
});
|
||
|
||
await queryInterface.addIndex('wms_combo_goods', [ 'combo_sku_id' ], {
|
||
name: 'idx_combo_sku_id',
|
||
});
|
||
|
||
await queryInterface.addIndex('wms_combo_goods', [ 'status' ], {
|
||
name: 'idx_status',
|
||
});
|
||
|
||
await queryInterface.addIndex('wms_combo_goods', [ 'warehouse_id' ], {
|
||
name: 'idx_warehouse_id',
|
||
});
|
||
|
||
await queryInterface.addIndex('wms_combo_goods', [ 'warehouse_id', 'custom_code' ], {
|
||
name: 'uk_store_custom_code',
|
||
});
|
||
};
|
||
|
||
/**
|
||
* 创建组合商品明细表 `wms_combo_goods_item`。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @param {import('sequelize')} Sequelize Sequelize 实例。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
const createComboGoodsItemTable = async (queryInterface, Sequelize) => {
|
||
await queryInterface.createTable('wms_combo_goods_item', {
|
||
id: {
|
||
type: Sequelize.INTEGER(11),
|
||
primaryKey: true,
|
||
autoIncrement: true,
|
||
allowNull: false,
|
||
comment: '主键ID',
|
||
},
|
||
combo_id: {
|
||
type: Sequelize.INTEGER(11),
|
||
allowNull: false,
|
||
comment: '关联 wms_combo_goods.id',
|
||
},
|
||
item_goods_sku_id: {
|
||
type: Sequelize.INTEGER(11),
|
||
allowNull: false,
|
||
comment: '子商品SKU ID,关联 wms_goods_sku.id',
|
||
},
|
||
item_quantity: {
|
||
type: Sequelize.INTEGER(11),
|
||
allowNull: false,
|
||
defaultValue: 1,
|
||
comment: '子商品数量(整数件数)',
|
||
},
|
||
sort_order: {
|
||
type: Sequelize.INTEGER(11),
|
||
allowNull: false,
|
||
defaultValue: 0,
|
||
comment: '排序序号,越小越靠前',
|
||
},
|
||
created_at: {
|
||
type: Sequelize.DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
|
||
comment: '创建时间',
|
||
},
|
||
updated_at: {
|
||
type: Sequelize.DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
|
||
comment: '更新时间',
|
||
},
|
||
});
|
||
|
||
await queryInterface.addIndex('wms_combo_goods_item', [ 'combo_id' ], {
|
||
name: 'idx_combo_id',
|
||
});
|
||
|
||
await queryInterface.addIndex('wms_combo_goods_item', [ 'item_goods_sku_id' ], {
|
||
name: 'idx_item_goods_sku_id',
|
||
});
|
||
|
||
await queryInterface.addIndex('wms_combo_goods_item', [ 'combo_id', 'item_goods_sku_id' ], {
|
||
unique: true,
|
||
name: 'uk_combo_sku',
|
||
});
|
||
};
|
||
|
||
module.exports = {
|
||
/**
|
||
* 执行 migration:创建组合商品主表与明细表。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @param {import('sequelize')} Sequelize Sequelize 实例。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
up: async (queryInterface, Sequelize) => {
|
||
const existingTables = await getExistingTableSet(queryInterface);
|
||
|
||
if (!existingTables.has('wms_combo_goods')) {
|
||
await createComboGoodsTable(queryInterface, Sequelize);
|
||
} else {
|
||
await ensureCustomCodeColumnAndIndex(queryInterface, Sequelize);
|
||
}
|
||
|
||
if (!existingTables.has('wms_combo_goods_item')) {
|
||
await createComboGoodsItemTable(queryInterface, Sequelize);
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 回滚 migration:删除组合商品明细表与主表。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
down: async queryInterface => {
|
||
await queryInterface.dropTable('wms_combo_goods_item');
|
||
await queryInterface.dropTable('wms_combo_goods');
|
||
},
|
||
};
|