WMS_MAIN/database/migrations/20251110081641-create-wms-qimen-apps.js

88 lines
2.3 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 { INTEGER, STRING, DATE } = Sequelize;
// 先检查表是否存在,避免重复创建
const [ tables ] = await queryInterface.sequelize.query(
"SHOW TABLES LIKE 'wms_qimen_apps'"
);
if (tables.length > 0) {
console.log('Table wms_qimen_apps already exists, skipping creation');
return;
}
await queryInterface.createTable('wms_qimen_apps', {
id: {
type: INTEGER(11),
primaryKey: true,
autoIncrement: true,
allowNull: false,
comment: '应用ID',
},
name: {
type: STRING(100),
allowNull: true,
comment: '应用名称',
},
app_key: {
type: STRING(50),
allowNull: true,
unique: 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: '更新时间',
},
}, {
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);
}
};
// 添加唯一索引(如果创建表时unique约束没生效,这里确保创建)
await safeCreateIndex('wms_qimen_apps', [ 'app_key' ], {
name: 'uk_app_key',
unique: true,
});
// 添加普通索引
await safeCreateIndex('wms_qimen_apps', [ 'name' ], {
name: 'idx_name',
});
},
async down(queryInterface, Sequelize) {
// 安全删除表
const [ tables ] = await queryInterface.sequelize.query(
"SHOW TABLES LIKE 'wms_qimen_apps'"
);
if (tables.length > 0) {
await queryInterface.dropTable('wms_qimen_apps');
}
},
};