WMS_MAIN/database/migrations/archive/20251110091013-create-wms-api-app.js

221 lines
5.7 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, STRING, INTEGER, JSON, TINYINT, DATE } = Sequelize;
// 先检查表是否存在,避免重复创建
const [ tables ] = await queryInterface.sequelize.query(
"SHOW TABLES LIKE 'wms_api_app'"
);
if (tables.length > 0) {
console.log('Table wms_api_app already exists, skipping creation');
return;
}
await queryInterface.createTable('wms_api_app', {
id: {
type: BIGINT(20),
primaryKey: true,
autoIncrement: true,
allowNull: false,
comment: '应用ID',
},
app_name: {
type: STRING(200),
allowNull: false,
comment: '应用名称',
},
interface_type_id: {
type: INTEGER(10),
allowNull: false,
references: {
model: 'wms_interface_type',
key: 'id',
},
onUpdate: 'RESTRICT',
onDelete: 'RESTRICT',
comment: '接口类型ID',
},
contract_id: {
type: BIGINT(20),
allowNull: true,
references: {
model: 'wms_store_warehouse',
key: 'id',
},
onUpdate: 'RESTRICT',
onDelete: 'RESTRICT',
comment: '合约ID,关联wms_store_warehouse.id(货主接口使用)',
},
warehouse_id: {
type: BIGINT(20),
allowNull: true,
comment: '仓库ID(仓库接口使用)',
},
self_app_key: {
type: STRING(100),
allowNull: true,
unique: true,
comment: '自身AppKey(开放接口使用,系统生成)',
},
self_app_secret: {
type: STRING(500),
allowNull: true,
comment: '自身AppSecret(开放接口使用,系统生成)',
},
self_ip_whitelist: {
type: JSON,
allowNull: true,
comment: '自身IP白名单',
},
qimen_app_key: {
type: STRING(100),
allowNull: true,
comment: '奇门对方AppKey',
},
qimen_customer_id: {
type: STRING(100),
allowNull: true,
comment: '奇门客户ID',
},
qimen_warehouse_code: {
type: STRING(100),
allowNull: true,
comment: '奇门仓库编码',
},
api_version: {
type: STRING(50),
allowNull: true,
defaultValue: '1.0',
comment: 'API版本',
},
sign_method: {
type: STRING(50),
allowNull: true,
defaultValue: 'md5',
comment: '签名方法',
},
rate_limit: {
type: INTEGER(11),
allowNull: true,
defaultValue: 1000,
comment: '速率限制(次/分钟)',
},
timeout_seconds: {
type: INTEGER(11),
allowNull: true,
defaultValue: 30,
comment: '超时时间(秒)',
},
contact_person: {
type: STRING(100),
allowNull: true,
comment: '联系人',
},
contact_phone: {
type: STRING(20),
allowNull: true,
comment: '联系电话',
},
email: {
type: STRING(100),
allowNull: true,
comment: '邮箱',
},
is_enabled: {
type: TINYINT(1),
allowNull: false,
defaultValue: 1,
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_unicode_ci',
comment: 'API应用配置表',
});
// 安全创建索引的函数
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_api_app', [ 'app_name', 'interface_type_id' ], {
name: 'uk_app_name_interface',
unique: true,
});
await safeCreateIndex('wms_api_app', [ 'self_app_key' ], {
name: 'uk_self_app_key',
unique: true,
});
await safeCreateIndex('wms_api_app', [ 'qimen_app_key', 'qimen_customer_id' ], {
name: 'uk_interface_warehouse',
unique: true,
});
await safeCreateIndex('wms_api_app', [ 'interface_type_id', 'contract_id' ], {
name: 'uk_interface_contract',
unique: true,
});
// 添加普通索引
await safeCreateIndex('wms_api_app', [ 'contract_id' ], {
name: 'fk_wms_api_app_contract',
});
await safeCreateIndex('wms_api_app', [ 'interface_type_id' ], {
name: 'idx_interface_type_id',
});
await safeCreateIndex('wms_api_app', [ 'warehouse_id' ], {
name: 'idx_warehouse_id',
});
await safeCreateIndex('wms_api_app', [ 'is_enabled' ], {
name: 'idx_is_enabled',
});
await safeCreateIndex('wms_api_app', [ 'created_at' ], {
name: 'idx_created_at',
});
// 添加复合索引
await safeCreateIndex('wms_api_app', [ 'interface_type_id', 'is_enabled' ], {
name: 'idx_interface_enabled',
});
},
async down(queryInterface) {
// 安全删除表
const [ tables ] = await queryInterface.sequelize.query(
"SHOW TABLES LIKE 'wms_api_app'"
);
if (tables.length > 0) {
await queryInterface.dropTable('wms_api_app');
}
},
};