WMS_MAIN/database/migrations/20260522103000-create-shops.js
liufumingren dc4894cb6d 1
2026-05-25 17:08:56 +08:00

129 lines
3.6 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 TABLE_NAME = 'wms_shops';
/**
* 获取当前数据库已存在的表名集合。
* @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 {import('sequelize')} Sequelize Sequelize 实例。
* @return {Promise<void>} 无返回值。
*/
const createShopsTable = async (queryInterface, Sequelize) => {
await queryInterface.createTable(TABLE_NAME, {
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',
},
platform_code: {
type: Sequelize.STRING(50),
allowNull: false,
comment: '平台编码',
},
shop_name: {
type: Sequelize.STRING(100),
allowNull: false,
comment: '店铺名称',
},
status: {
type: Sequelize.TINYINT(1),
allowNull: false,
defaultValue: 0,
comment: '授权状态:0=未授权,1=已授权',
},
auth_expire_at: {
type: Sequelize.DATE,
allowNull: true,
comment: '授权到期时间',
},
auth_random_code: {
type: Sequelize.STRING(128),
allowNull: true,
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: '更新时间',
},
}, {
charset: 'utf8mb4',
collate: 'utf8mb4_general_ci',
comment: '店铺表',
});
await queryInterface.addIndex(TABLE_NAME, [ 'warehouse_id' ], {
name: 'idx_warehouse_id',
});
await queryInterface.addIndex(TABLE_NAME, [ 'platform_code' ], {
name: 'idx_platform_code',
});
await queryInterface.addIndex(TABLE_NAME, [ 'status' ], {
name: 'idx_status',
});
await queryInterface.addIndex(TABLE_NAME, [ 'warehouse_id', 'platform_code', 'shop_name' ], {
name: 'uk_warehouse_platform_shop_name',
unique: true,
});
await queryInterface.addIndex(TABLE_NAME, [ 'auth_random_code' ], {
name: 'uk_auth_random_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(TABLE_NAME)) {
await createShopsTable(queryInterface, Sequelize);
}
},
/**
* 回滚删除店铺表。
* @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(TABLE_NAME)) {
await queryInterface.dropTable(TABLE_NAME);
}
},
};