187 lines
7.7 KiB
JavaScript
187 lines
7.7 KiB
JavaScript
'use strict';
|
||
|
||
const TABLE_NAME = 'wms_warehouse_front_config';
|
||
const DOMAIN_STATUS_COLUMN = 'domain_status';
|
||
const DOMAIN_STATUS_INDEX_NAME = 'idx_domain_status';
|
||
|
||
/**
|
||
* Migration: 仓库前端配置表新增域名验证(DNS TXT 校验)相关列与索引。
|
||
*
|
||
* 背景:
|
||
* 前端独立部署需要用户把自持域名解析到平台,必须校验域名归属,避免非自持域名
|
||
* 被指向平台(域名误配/抢注、证书误签)。方案为 DNS TXT 校验:后端为每个仓库
|
||
* 生成一次性校验主机名(wmsv-<10 位 hex>.<域名>)与校验值(32 位 hex),用户在
|
||
* 该子域配置 TXT 记录,后端解析比对通过后置为已验证。未通过验证的域名不得上传
|
||
* 证书(frontConfig.saveCert 前置闸门),公共按域名拉取同以 domain_status=1 为闸门。
|
||
*
|
||
* 新增列:
|
||
* - domain_status : 域名验证状态:0-未验证,1-已验证,2-验证失败(NOT NULL,默认 0);
|
||
* - domain_verify_host : 待校验 TXT 记录主机名(wmsv-<随机码>.<域名>);
|
||
* - domain_verify_token : 待校验 TXT 记录值(32 位 hex);
|
||
* - domain_verified_at : 域名验证通过时间;
|
||
* - domain_verify_attempts : 验证尝试次数(定时任务自增,超过 maxAttempts 后停止自动重试);
|
||
* - domain_last_error : 最近一次验证失败原因(脱敏截断,≤500 字符)。
|
||
* 新增索引:
|
||
* - idx_domain_status:域名验证定时任务按 domain_status 扫描待验证行的查询索引。
|
||
*
|
||
* ⚠ 存量数据回填(本次迁移的关键动作):
|
||
* 升级前已写入 domain 的行,其域名在线上已生效(nginx 已在按域名提供服务),必须
|
||
* 视为「已验证」,否则新加入的 domain_status=1 闸门会在升级瞬间把生产现有域名
|
||
* 全部断掉(证书上传被拒、公共拉取被拒)。
|
||
* 回填 SQL:UPDATE ... SET domain_status = 1
|
||
* WHERE domain IS NOT NULL AND domain <> '' AND domain_status = 0。
|
||
* 回填只在本次真正新增 domain_status 列的那一次执行:迁移允许重复执行,若列已存在
|
||
* 仍继续回填,会把「升级后新设置但尚未验证」的域名误标为已验证。
|
||
* domain_verified_at 保持 NULL:历史生效时间不可考,不写入虚假时间。
|
||
*
|
||
* Idempotent: 列/索引已存在时跳过;down 逐索引、逐列判存在后删除。
|
||
*/
|
||
|
||
/**
|
||
* 判断表是否存在。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @param {string} tableName 表名。
|
||
* @return {Promise<boolean>} 存在返回 true。
|
||
*/
|
||
async function tableExists(queryInterface, tableName) {
|
||
const tables = await queryInterface.showAllTables();
|
||
return tables.some(t => (typeof t === 'string' ? t : (t.tableName || t.name)) === tableName);
|
||
}
|
||
|
||
/**
|
||
* 判断索引是否存在。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @param {string} tableName 表名。
|
||
* @param {string} indexName 索引名。
|
||
* @return {Promise<boolean>} 存在返回 true。
|
||
*/
|
||
async function indexExists(queryInterface, tableName, indexName) {
|
||
const indexes = await queryInterface.showIndex(tableName);
|
||
return (indexes || []).some(item => (typeof item === 'string' ? item : item.name) === indexName);
|
||
}
|
||
|
||
module.exports = {
|
||
/**
|
||
* 执行正向迁移:新增域名验证相关列与索引,并回填存量已生效域名(幂等)。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @param {import('sequelize')} Sequelize Sequelize 实例。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async up(queryInterface, Sequelize) {
|
||
if (!(await tableExists(queryInterface, TABLE_NAME))) {
|
||
return;
|
||
}
|
||
|
||
const columns = await queryInterface.describeTable(TABLE_NAME);
|
||
const domainStatusAdded = !columns[DOMAIN_STATUS_COLUMN];
|
||
|
||
if (domainStatusAdded) {
|
||
await queryInterface.addColumn(TABLE_NAME, DOMAIN_STATUS_COLUMN, {
|
||
type: Sequelize.TINYINT,
|
||
allowNull: false,
|
||
defaultValue: 0,
|
||
comment: '域名验证状态:0-未验证,1-已验证,2-验证失败',
|
||
after: 'domain',
|
||
});
|
||
}
|
||
|
||
if (!columns.domain_verify_host) {
|
||
await queryInterface.addColumn(TABLE_NAME, 'domain_verify_host', {
|
||
type: Sequelize.STRING(255),
|
||
allowNull: true,
|
||
comment: '待校验 TXT 记录主机名(wmsv-<随机码>.<域名>)',
|
||
after: 'domain_status',
|
||
});
|
||
}
|
||
|
||
if (!columns.domain_verify_token) {
|
||
await queryInterface.addColumn(TABLE_NAME, 'domain_verify_token', {
|
||
type: Sequelize.STRING(64),
|
||
allowNull: true,
|
||
comment: '待校验 TXT 记录值(32 位 hex)',
|
||
after: 'domain_verify_host',
|
||
});
|
||
}
|
||
|
||
if (!columns.domain_verified_at) {
|
||
await queryInterface.addColumn(TABLE_NAME, 'domain_verified_at', {
|
||
type: Sequelize.DATE,
|
||
allowNull: true,
|
||
comment: '域名验证通过时间',
|
||
after: 'domain_verify_token',
|
||
});
|
||
}
|
||
|
||
if (!columns.domain_verify_attempts) {
|
||
await queryInterface.addColumn(TABLE_NAME, 'domain_verify_attempts', {
|
||
type: Sequelize.INTEGER(11),
|
||
allowNull: false,
|
||
defaultValue: 0,
|
||
comment: '域名验证尝试次数',
|
||
after: 'domain_verified_at',
|
||
});
|
||
}
|
||
|
||
if (!columns.domain_last_error) {
|
||
await queryInterface.addColumn(TABLE_NAME, 'domain_last_error', {
|
||
type: Sequelize.STRING(500),
|
||
allowNull: true,
|
||
comment: '最近一次域名验证失败原因',
|
||
after: 'domain_verify_attempts',
|
||
});
|
||
}
|
||
|
||
if (!(await indexExists(queryInterface, TABLE_NAME, DOMAIN_STATUS_INDEX_NAME))) {
|
||
await queryInterface.addIndex(TABLE_NAME, [ DOMAIN_STATUS_COLUMN ], {
|
||
name: DOMAIN_STATUS_INDEX_NAME,
|
||
});
|
||
}
|
||
|
||
// 存量回填:升级前已写入 domain 的行在线上已生效,必须视为「已验证」,
|
||
// 否则 domain_status=1 闸门会在升级瞬间断掉生产现有域名。
|
||
// 仅在本次真正新增该列时执行(重复执行迁移不得把新设置但未验证的域名误标为已验证),
|
||
// WHERE 仍带 domain_status = 0 守卫,重复执行同样安全。
|
||
if (domainStatusAdded) {
|
||
const [ result ] = await queryInterface.sequelize.query(
|
||
`UPDATE \`${TABLE_NAME}\`
|
||
SET \`domain_status\` = 1
|
||
WHERE \`domain\` IS NOT NULL
|
||
AND \`domain\` <> ''
|
||
AND \`domain_status\` = 0`
|
||
);
|
||
const affectedRows = Number(result && result.affectedRows !== undefined ? result.affectedRows : result) || 0;
|
||
console.log(`[migration] ${TABLE_NAME} 存量域名回填 domain_status=1,影响行数=${affectedRows}`);
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 回滚迁移:删除域名验证相关索引与列(幂等)。
|
||
* 注意:回滚会一并丢弃域名验证状态与校验记录,域名将退回「无验证闸门」状态。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async down(queryInterface) {
|
||
if (!(await tableExists(queryInterface, TABLE_NAME))) {
|
||
return;
|
||
}
|
||
|
||
if (await indexExists(queryInterface, TABLE_NAME, DOMAIN_STATUS_INDEX_NAME)) {
|
||
await queryInterface.removeIndex(TABLE_NAME, DOMAIN_STATUS_INDEX_NAME);
|
||
}
|
||
|
||
const columns = await queryInterface.describeTable(TABLE_NAME);
|
||
for (const columnName of [
|
||
'domain_last_error',
|
||
'domain_verify_attempts',
|
||
'domain_verified_at',
|
||
'domain_verify_token',
|
||
'domain_verify_host',
|
||
DOMAIN_STATUS_COLUMN,
|
||
]) {
|
||
if (columns[columnName]) {
|
||
await queryInterface.removeColumn(TABLE_NAME, columnName);
|
||
}
|
||
}
|
||
},
|
||
};
|