WMS_MAIN/database/migrations/20260528090000-create-wms-package-material-and-supplier.js
liufumingren abf2c4d94a 1
2026-05-29 16:47:38 +08:00

284 lines
8.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const PACKAGE_MATERIAL_TABLE = 'wms_package_material';
const PACKAGE_SUPPLIER_TABLE = 'wms_package_supplier';
/**
* 获取当前数据库已存在的表名集合。
* @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))
);
};
/**
* 获取指定表的索引名集合。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {string} tableName 表名。
* @return {Promise<Set<string>>} 索引名集合。
*/
const getIndexNameSet = async (queryInterface, tableName) => {
const indexes = await queryInterface.showIndex(tableName);
return new Set(indexes.map(index => String(index && index.name ? index.name : '').trim()).filter(Boolean));
};
/**
* 创建包材表。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {import('sequelize')} Sequelize Sequelize 实例。
* @return {Promise<void>} 无返回值。
*/
const createPackageMaterialTable = async (queryInterface, Sequelize) => {
await queryInterface.createTable(PACKAGE_MATERIAL_TABLE, {
id: {
type: Sequelize.BIGINT(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
allowNull: false,
comment: '主键ID',
},
warehouse_id: {
type: Sequelize.BIGINT(20).UNSIGNED,
allowNull: false,
comment: '仓库ID',
},
material_code: {
type: Sequelize.STRING(64),
allowNull: false,
comment: '包材编码',
},
material_name: {
type: Sequelize.STRING(128),
allowNull: false,
comment: '包材名称',
},
material_type: {
type: Sequelize.STRING(32),
allowNull: true,
comment: '包材类型',
},
spec: {
type: Sequelize.STRING(128),
allowNull: true,
comment: '规格描述',
},
length: {
type: Sequelize.DECIMAL(10, 2),
allowNull: true,
comment: '长度(cm)',
},
width: {
type: Sequelize.DECIMAL(10, 2),
allowNull: true,
comment: '宽度(cm)',
},
height: {
type: Sequelize.DECIMAL(10, 2),
allowNull: true,
comment: '高度(cm)',
},
weight: {
type: Sequelize.DECIMAL(10, 3),
allowNull: true,
comment: '重量(kg)',
},
cost_price: {
type: Sequelize.DECIMAL(10, 2),
allowNull: true,
comment: '成本单价',
},
status: {
type: Sequelize.TINYINT(1),
allowNull: false,
defaultValue: 1,
comment: '状态:0=禁用,1=启用',
},
version: {
type: Sequelize.INTEGER.UNSIGNED,
allowNull: false,
defaultValue: 1,
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: '更新时间',
},
}, {
engine: 'InnoDB',
charset: 'utf8mb4',
collate: 'utf8mb4_general_ci',
comment: '包材信息表',
});
};
/**
* 创建包材供应商表。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {import('sequelize')} Sequelize Sequelize 实例。
* @return {Promise<void>} 无返回值。
*/
const createPackageSupplierTable = async (queryInterface, Sequelize) => {
await queryInterface.createTable(PACKAGE_SUPPLIER_TABLE, {
id: {
type: Sequelize.BIGINT(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
allowNull: false,
comment: '主键ID',
},
warehouse_id: {
type: Sequelize.BIGINT(20).UNSIGNED,
allowNull: false,
comment: '仓库ID',
},
supplier_code: {
type: Sequelize.STRING(64),
allowNull: false,
comment: '供应商编码',
},
supplier_name: {
type: Sequelize.STRING(128),
allowNull: false,
comment: '供应商名称',
},
contact_name: {
type: Sequelize.STRING(64),
allowNull: true,
comment: '联系人',
},
contact_phone: {
type: Sequelize.STRING(32),
allowNull: true,
comment: '联系电话',
},
status: {
type: Sequelize.TINYINT(1),
allowNull: false,
defaultValue: 1,
comment: '状态:0=禁用,1=启用',
},
version: {
type: Sequelize.INTEGER.UNSIGNED,
allowNull: false,
defaultValue: 1,
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: '更新时间',
},
}, {
engine: 'InnoDB',
charset: 'utf8mb4',
collate: 'utf8mb4_general_ci',
comment: '包材供应商表',
});
};
/**
* 确保包材表索引存在。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<void>} 无返回值。
*/
const ensurePackageMaterialIndexes = async queryInterface => {
const indexNameSet = await getIndexNameSet(queryInterface, PACKAGE_MATERIAL_TABLE);
if (!indexNameSet.has('idx_wms_package_material_warehouse_id')) {
await queryInterface.addIndex(PACKAGE_MATERIAL_TABLE, [ 'warehouse_id' ], {
name: 'idx_wms_package_material_warehouse_id',
});
}
if (!indexNameSet.has('uk_wms_package_material_warehouse_material_code')) {
await queryInterface.addIndex(PACKAGE_MATERIAL_TABLE, [ 'warehouse_id', 'material_code' ], {
name: 'uk_wms_package_material_warehouse_material_code',
unique: true,
});
}
};
/**
* 确保包材供应商表索引存在。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<void>} 无返回值。
*/
const ensurePackageSupplierIndexes = async queryInterface => {
const indexNameSet = await getIndexNameSet(queryInterface, PACKAGE_SUPPLIER_TABLE);
if (!indexNameSet.has('idx_wms_package_supplier_warehouse_id')) {
await queryInterface.addIndex(PACKAGE_SUPPLIER_TABLE, [ 'warehouse_id' ], {
name: 'idx_wms_package_supplier_warehouse_id',
});
}
if (!indexNameSet.has('uk_wms_package_supplier_warehouse_supplier_code')) {
await queryInterface.addIndex(PACKAGE_SUPPLIER_TABLE, [ 'warehouse_id', 'supplier_code' ], {
name: 'uk_wms_package_supplier_warehouse_supplier_code',
unique: true,
});
}
};
module.exports = {
/**
* 创建包材与包材供应商表及索引。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {import('sequelize')} Sequelize Sequelize 实例。
* @return {Promise<void>} 无返回值。
*/
async up(queryInterface, Sequelize) {
const existingTables = await getExistingTableSet(queryInterface);
if (!existingTables.has(PACKAGE_MATERIAL_TABLE)) {
await createPackageMaterialTable(queryInterface, Sequelize);
}
if (!existingTables.has(PACKAGE_SUPPLIER_TABLE)) {
await createPackageSupplierTable(queryInterface, Sequelize);
}
await ensurePackageMaterialIndexes(queryInterface);
await ensurePackageSupplierIndexes(queryInterface);
},
/**
* 回滚删除包材与包材供应商表。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {import('sequelize')} Sequelize Sequelize 实例。
* @return {Promise<void>} 无返回值。
*/
async down(queryInterface, Sequelize) {
void Sequelize;
const existingTables = await getExistingTableSet(queryInterface);
if (existingTables.has(PACKAGE_SUPPLIER_TABLE)) {
await queryInterface.dropTable(PACKAGE_SUPPLIER_TABLE);
}
if (existingTables.has(PACKAGE_MATERIAL_TABLE)) {
await queryInterface.dropTable(PACKAGE_MATERIAL_TABLE);
}
},
};