WMS_MAIN/database/migrations/archive/20251110084033-create-wms-store-warehouse.js

199 lines
5.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';
module.exports = {
async up(queryInterface, Sequelize) {
const { BIGINT, TEXT, STRING, DATE, TINYINT } = Sequelize;
// 先检查表是否存在,避免重复创建
const [ tables ] = await queryInterface.sequelize.query(
"SHOW TABLES LIKE 'wms_store_warehouse'"
);
if (tables.length > 0) {
console.log('Table wms_store_warehouse already exists, skipping creation');
return;
}
await queryInterface.createTable('wms_store_warehouse', {
id: {
type: BIGINT(20),
primaryKey: true,
autoIncrement: true,
allowNull: false,
comment: '授权ID',
},
store_id: {
type: BIGINT(20),
allowNull: false,
references: {
model: 'wms_store',
key: 'id',
},
onUpdate: 'CASCADE',
onDelete: 'NO ACTION',
comment: '商家ID',
},
warehouse_id: {
type: BIGINT(20),
allowNull: false,
references: {
model: 'wms_store',
key: 'id',
},
onUpdate: 'CASCADE',
onDelete: 'NO ACTION',
comment: '仓库ID',
},
auth_status: {
type: TINYINT(4),
allowNull: false,
defaultValue: 1,
comment: '授权状态:1-申请中,2-已通过,3-已拒绝,4-已解除,5-已过期',
},
apply_reason: {
type: TEXT,
allowNull: true,
comment: '申请理由',
},
review_comment: {
type: TEXT,
allowNull: true,
comment: '审核意见',
},
apply_time: {
type: DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
comment: '申请时间',
},
review_time: {
type: DATE,
allowNull: true,
comment: '审核时间',
},
auth_start: {
type: DATE,
allowNull: true,
comment: '授权开始日期',
},
auth_end: {
type: DATE,
allowNull: true,
comment: '授权结束日期',
},
actual_end: {
type: DATE,
allowNull: true,
comment: '实际结束时间',
},
applied_by: {
type: BIGINT(20),
allowNull: false,
comment: '申请人',
},
reviewed_by: {
type: BIGINT(20),
allowNull: true,
comment: '审核人',
},
ended_by: {
type: BIGINT(20),
allowNull: true,
comment: '解除人',
},
created_at: {
type: DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
comment: '创建时间',
},
updated_at: {
type: DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
comment: '更新时间',
},
store_remask: {
type: STRING(50),
allowNull: true,
comment: '商家备注',
},
warehouse_remask: {
type: STRING(50),
allowNull: true,
comment: '仓库备注',
},
}, {
engine: 'InnoDB',
charset: 'utf8mb4',
collate: 'utf8mb4_general_ci',
comment: '商家仓库授权关系表',
});
// 安全创建索引的函数
const safeCreateIndex = async (tableName, fields, options) => {
const [ existingIndexes ] = await queryInterface.sequelize.query(
`SHOW INDEX FROM ${tableName} WHERE Key_name = '${options.name}'`
);
if (existingIndexes.length === 0) {
await queryInterface.addIndex(tableName, fields, options);
}
};
// 添加唯一索引 - 商家和仓库的组合唯一
await safeCreateIndex('wms_store_warehouse', [ 'store_id', 'warehouse_id' ], {
name: 'uk_store_warehouse',
unique: true,
});
// 添加普通索引
await safeCreateIndex('wms_store_warehouse', [ 'store_id' ], {
name: 'idx_store_id',
});
await safeCreateIndex('wms_store_warehouse', [ 'warehouse_id' ], {
name: 'idx_warehouse_id',
});
await safeCreateIndex('wms_store_warehouse', [ 'auth_status' ], {
name: 'idx_auth_status',
});
await safeCreateIndex('wms_store_warehouse', [ 'apply_time' ], {
name: 'idx_apply_time',
});
await safeCreateIndex('wms_store_warehouse', [ 'auth_start', 'auth_end' ], {
name: 'idx_auth_period',
});
await safeCreateIndex('wms_store_warehouse', [ 'applied_by' ], {
name: 'idx_applied_by',
});
await safeCreateIndex('wms_store_warehouse', [ 'reviewed_by' ], {
name: 'idx_reviewed_by',
});
await safeCreateIndex('wms_store_warehouse', [ 'auth_end' ], {
name: 'idx_auth_end',
});
// 添加复合索引用于常用查询
await safeCreateIndex('wms_store_warehouse', [ 'auth_status', 'apply_time' ], {
name: 'idx_status_apply_time',
});
},
async down(queryInterface) {
// 安全删除表
const [ tables ] = await queryInterface.sequelize.query(
"SHOW TABLES LIKE 'wms_store_warehouse'"
);
if (tables.length > 0) {
await queryInterface.dropTable('wms_store_warehouse');
}
},
};