90 lines
3.0 KiB
JavaScript
90 lines
3.0 KiB
JavaScript
'use strict';
|
||
|
||
const TABLE_NAME = 'wms_workbench';
|
||
|
||
async function indexExists(queryInterface, tableName, indexName) {
|
||
const indexes = await queryInterface.showIndex(tableName);
|
||
return (indexes || []).some(i => (typeof i === 'string' ? i : i.name) === indexName);
|
||
}
|
||
|
||
async function addIndexIfNotExists(queryInterface, tableName, indexName, options = {}) {
|
||
if (await indexExists(queryInterface, tableName, indexName)) {
|
||
console.log(`[idx] ${tableName}.${indexName} 已存在,跳过`);
|
||
return;
|
||
}
|
||
await queryInterface.addIndex(tableName, options.fields, {
|
||
name: indexName,
|
||
unique: !!options.unique,
|
||
});
|
||
console.log(`[idx] ${tableName}.${indexName} 已创建`);
|
||
}
|
||
|
||
async function dropIndexIfExists(queryInterface, tableName, indexName) {
|
||
if (await indexExists(queryInterface, tableName, indexName)) {
|
||
await queryInterface.removeIndex(tableName, indexName);
|
||
console.log(`[idx] ${tableName}.${indexName} 已删除`);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Migration: add login_code / token to wms_workbench
|
||
* - login_code: VARCHAR(64) NULL COMMENT '工作台登录码(前缀+随机,64位,全局唯一)'
|
||
* - token: VARCHAR(64) NULL COMMENT '工作台令牌(随机64位,绑定后生成)'
|
||
*
|
||
* Idempotent: 通过 describeTable / showIndex 判断后仅补齐缺失部分。
|
||
*/
|
||
|
||
module.exports = {
|
||
/**
|
||
* 执行正向迁移:新增 login_code、token 两列与 uk_login_code、idx_token 索引。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @param {import('sequelize')} Sequelize Sequelize 实例。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
up: async (queryInterface, Sequelize) => {
|
||
const columns = await queryInterface.describeTable(TABLE_NAME);
|
||
|
||
if (!columns.login_code) {
|
||
await queryInterface.addColumn(TABLE_NAME, 'login_code', {
|
||
type: Sequelize.STRING(64),
|
||
allowNull: true,
|
||
comment: '工作台登录码(前缀+随机,64位,全局唯一)',
|
||
});
|
||
}
|
||
|
||
if (!columns.token) {
|
||
await queryInterface.addColumn(TABLE_NAME, 'token', {
|
||
type: Sequelize.STRING(64),
|
||
allowNull: true,
|
||
comment: '工作台令牌(随机64位,绑定后生成)',
|
||
});
|
||
}
|
||
|
||
await addIndexIfNotExists(queryInterface, TABLE_NAME, 'uk_login_code', {
|
||
fields: [ 'login_code' ],
|
||
unique: true,
|
||
});
|
||
await addIndexIfNotExists(queryInterface, TABLE_NAME, 'idx_token', {
|
||
fields: [ 'token' ],
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 执行回滚迁移:删除索引与新增列。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
down: async queryInterface => {
|
||
await dropIndexIfExists(queryInterface, TABLE_NAME, 'uk_login_code');
|
||
await dropIndexIfExists(queryInterface, TABLE_NAME, 'idx_token');
|
||
|
||
const columns = await queryInterface.describeTable(TABLE_NAME);
|
||
if (columns.login_code) {
|
||
await queryInterface.removeColumn(TABLE_NAME, 'login_code');
|
||
}
|
||
if (columns.token) {
|
||
await queryInterface.removeColumn(TABLE_NAME, 'token');
|
||
}
|
||
},
|
||
};
|