196 lines
4.8 KiB
JavaScript
196 lines
4.8 KiB
JavaScript
'use strict';
|
||
|
||
module.exports = {
|
||
async up(queryInterface, Sequelize) {
|
||
const { BIGINT, STRING, INTEGER, DATE, TINYINT } = Sequelize;
|
||
|
||
// 先检查表是否存在,避免重复创建
|
||
const [ tables ] = await queryInterface.sequelize.query(
|
||
"SHOW TABLES LIKE 'wms_store'"
|
||
);
|
||
|
||
if (tables.length > 0) {
|
||
console.log('Table wms_store already exists, skipping creation');
|
||
return;
|
||
}
|
||
|
||
await queryInterface.createTable('wms_store', {
|
||
id: {
|
||
type: BIGINT(20),
|
||
primaryKey: true,
|
||
autoIncrement: true,
|
||
allowNull: false,
|
||
comment: '商家ID',
|
||
},
|
||
store_name: {
|
||
type: STRING(100),
|
||
allowNull: false,
|
||
comment: '商家名称',
|
||
},
|
||
company_name: {
|
||
type: STRING(200),
|
||
allowNull: false,
|
||
unique: true,
|
||
comment: '公司全称',
|
||
},
|
||
business_license: {
|
||
type: STRING(100),
|
||
allowNull: true,
|
||
unique: true,
|
||
comment: '营业执照号',
|
||
},
|
||
legal_person: {
|
||
type: STRING(50),
|
||
allowNull: true,
|
||
comment: '法人代表',
|
||
},
|
||
contact_phone: {
|
||
type: STRING(20),
|
||
allowNull: false,
|
||
comment: '联系电话',
|
||
},
|
||
contact_email: {
|
||
type: STRING(100),
|
||
allowNull: true,
|
||
comment: '联系邮箱',
|
||
},
|
||
contact_person: {
|
||
type: STRING(50),
|
||
allowNull: false,
|
||
comment: '联系人',
|
||
},
|
||
address: {
|
||
type: STRING(500),
|
||
allowNull: true,
|
||
comment: '公司地址',
|
||
},
|
||
industry: {
|
||
type: STRING(100),
|
||
allowNull: true,
|
||
comment: '所属行业',
|
||
},
|
||
account_id: {
|
||
type: BIGINT(20),
|
||
allowNull: false,
|
||
comment: '关联账号ID(商家管理员)',
|
||
},
|
||
status: {
|
||
type: TINYINT(4),
|
||
allowNull: false,
|
||
defaultValue: 2,
|
||
comment: '状态:0-禁用,1-启用,2-审核中',
|
||
},
|
||
created_by: {
|
||
type: BIGINT(20),
|
||
allowNull: false,
|
||
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: '更新时间',
|
||
},
|
||
is_warehouse: {
|
||
type: INTEGER(11),
|
||
allowNull: true,
|
||
defaultValue: 0,
|
||
comment: '是否为仓库:0-否,1-是',
|
||
},
|
||
logo_url: {
|
||
type: STRING(255),
|
||
allowNull: true,
|
||
comment: 'logo链接',
|
||
},
|
||
domain: {
|
||
type: STRING(100),
|
||
allowNull: true,
|
||
comment: '绑定域名',
|
||
},
|
||
auth_start: {
|
||
type: DATE,
|
||
allowNull: true,
|
||
comment: '授权开始时间',
|
||
},
|
||
auth_end: {
|
||
type: DATE,
|
||
allowNull: true,
|
||
comment: '授权结束时间',
|
||
},
|
||
}, {
|
||
engine: 'InnoDB',
|
||
charset: 'utf8mb4',
|
||
collate: 'utf8mb4_unicode_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', [ 'company_name' ], {
|
||
name: 'uk_company_name',
|
||
unique: true,
|
||
});
|
||
|
||
await safeCreateIndex('wms_store', [ 'business_license' ], {
|
||
name: 'uk_business_license',
|
||
unique: true,
|
||
});
|
||
|
||
// 添加普通索引
|
||
await safeCreateIndex('wms_store', [ 'status' ], {
|
||
name: 'idx_status',
|
||
});
|
||
|
||
await safeCreateIndex('wms_store', [ 'account_id' ], {
|
||
name: 'idx_account_id',
|
||
});
|
||
|
||
await safeCreateIndex('wms_store', [ 'store_name' ], {
|
||
name: 'idx_store_name',
|
||
});
|
||
|
||
await safeCreateIndex('wms_store', [ 'contact_phone' ], {
|
||
name: 'idx_contact_phone',
|
||
});
|
||
|
||
await safeCreateIndex('wms_store', [ 'is_warehouse' ], {
|
||
name: 'idx_is_warehouse',
|
||
});
|
||
|
||
await safeCreateIndex('wms_store', [ 'auth_end' ], {
|
||
name: 'idx_auth_end',
|
||
});
|
||
|
||
// 添加复合索引
|
||
await safeCreateIndex('wms_store', [ 'status', 'is_warehouse' ], {
|
||
name: 'idx_status_warehouse',
|
||
});
|
||
},
|
||
|
||
async down(queryInterface) {
|
||
// 安全删除表
|
||
const [ tables ] = await queryInterface.sequelize.query(
|
||
"SHOW TABLES LIKE 'wms_store'"
|
||
);
|
||
|
||
if (tables.length > 0) {
|
||
await queryInterface.dropTable('wms_store');
|
||
}
|
||
},
|
||
};
|