- 迁移 20260924180000-add-pending-domain-to-wms-warehouse-front-config 新增 pending_* 6 列、retired_domain/retired_at 与 idx_pending_domain/idx_retired_domain 两个索引(生产库已执行) - setDomain 只登记待验证域名并下发 TXT 校验码,不再「设置即生效」; TXT 校验通过后由 promotePendingDomain 提升(旧域名写入 retired_domain 下线队列、 清空证书 5 列并重置同步状态),消除「改域名瞬间旧域名公共接口 404」中间态 - DoH 解析改为多端点顺序尝试(dohUrls:alidns → 223.5.5.5 → doh.pub), 单端点 NODATA/NXDOMAIN 不再直接判定,必要时降级系统 DNS 并显式限时; config.frontConfig.domainVerify 同步 batchSize 50→20、timeoutMs 5000→3000 - getDomain/setDomain/checkDomain 响应追加待验证域名组 7 字段(旧 9 字段口径不变) - 同步 methods 文档(frontConfig 6 方法)与 changes 4 条变更记录、 AI_METHOD_ROUTING.md 函数清单(42→50) 注:本提交补齐此前已在生产库执行但从未入库的迁移与代码 (生产 SequelizeMeta 已含 20260924180000,属 orphan meta 补齐入库)。
221 lines
9.1 KiB
JavaScript
221 lines
9.1 KiB
JavaScript
'use strict';
|
||
|
||
const TABLE_NAME = 'wms_warehouse_front_config';
|
||
const PENDING_DOMAIN_INDEX_NAME = 'idx_pending_domain';
|
||
const RETIRED_DOMAIN_INDEX_NAME = 'idx_retired_domain';
|
||
|
||
/**
|
||
* Migration: 仓库前端配置表新增「两阶段域名切换」相关列与索引。
|
||
*
|
||
* 背景(为什么需要这次改造):
|
||
* 原实现 frontConfig.setDomain 是「设置即生效」——一次调用就把 domain 改成新域名、
|
||
* domain_status 归零并清空证书。后果是旧域名立刻被 frontConfig.getByDomain 判为
|
||
* 「域名未配置」(404),而 nginx 侧旧域名 server 块还在,出现「页面能打开但拉不到配置」
|
||
* 的中间态;用户改错域名也无法回滚。
|
||
*
|
||
* 方案(方案 B·两阶段切换,待验证域名与生效域名分离):
|
||
* 1. 第一阶段(frontConfig.setDomain):只把新域名登记到 pending_domain 并下发 TXT 校验码,
|
||
* 生效域名 domain、domain_status、domain_verified_at、证书与同步状态一律不动;
|
||
* 2. 第二阶段(frontConfig.checkDomain 手动触发 / app/schedule/domain_verify_scan.js 定时扫描):
|
||
* TXT 校验通过后由 frontConfig.promotePendingDomain 一次性把 pending_domain 提升为
|
||
* domain(domain_status=1),同时把旧域名写入 retired_domain 下线队列、清空证书并重置同步状态。
|
||
* 旧域名的 nginx 配置与证书文件由外部证书脚本按 retired_domain 删除后置 NULL(本次不改脚本)。
|
||
*
|
||
* 新增列(全部可空或带默认值,无需数据回填):
|
||
* - pending_domain : 待验证域名(STRING(100),验证通过后才提升为生效域名 domain);
|
||
* - pending_verify_host : 待验证域名的 TXT 记录主机名(STRING(255),wmsv-<随机码>.<待验证域名>);
|
||
* - pending_verify_token : 待验证域名的 TXT 记录值(STRING(64),32 位 hex);
|
||
* - pending_verify_attempts: 待验证域名校验尝试次数(INTEGER,NOT NULL,默认 0);
|
||
* - pending_last_error : 待验证域名最近一次失败原因(STRING(500),脱敏截断);
|
||
* - pending_created_at : 发起域名切换的时间(DATE);
|
||
* - retired_domain : 待下线旧域名(STRING(100),域名提升时写入,外部脚本删除其
|
||
* nginx 配置与证书文件后置 NULL);
|
||
* - retired_at : 旧域名进入下线队列的时间(DATE)。
|
||
* 新增索引:
|
||
* - idx_pending_domain:域名验证定时任务按 pending_domain 扫描待验证行的查询索引(普通索引);
|
||
* - idx_retired_domain:外部证书脚本按 retired_domain 扫描待下线旧域名的查询索引(普通索引)。
|
||
*
|
||
* 为什么无需回填:
|
||
* 8 个新列要么可空、要么带默认值 0,存量行的 pending_* 一律为 NULL / 0 → 语义就是
|
||
* 「没有待验证域名」,生效域名 domain / domain_status / 证书 / 同步状态全部保持原值,
|
||
* 升级瞬间行为与升级前一致(唯一变化是下次 setDomain 会走两阶段,属预期变更)。
|
||
* 注意:pending_domain 只有普通索引(非唯一),并发提交同一域名理论上仍可能同时写入两行,
|
||
* 最终由 domain 上的唯一索引在「提升」阶段兜底(提升冲突会保留待验证状态并记入错误)。
|
||
*
|
||
* 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);
|
||
|
||
// 列定义表:after 显式指定列序,pending 组紧随域名验证组,retired 组接在 pending 组之后
|
||
const columnDefs = [
|
||
{
|
||
name: 'pending_domain',
|
||
after: 'domain_last_error',
|
||
definition: {
|
||
type: Sequelize.STRING(100),
|
||
allowNull: true,
|
||
comment: '待验证域名:setDomain 只登记到本列,TXT 校验通过后才提升为生效域名 domain',
|
||
},
|
||
},
|
||
{
|
||
name: 'pending_verify_host',
|
||
after: 'pending_domain',
|
||
definition: {
|
||
type: Sequelize.STRING(255),
|
||
allowNull: true,
|
||
comment: '待验证域名的 TXT 记录主机名(wmsv-<随机码>.<待验证域名>)',
|
||
},
|
||
},
|
||
{
|
||
name: 'pending_verify_token',
|
||
after: 'pending_verify_host',
|
||
definition: {
|
||
type: Sequelize.STRING(64),
|
||
allowNull: true,
|
||
comment: '待验证域名的 TXT 记录值(32 位 hex)',
|
||
},
|
||
},
|
||
{
|
||
name: 'pending_verify_attempts',
|
||
after: 'pending_verify_token',
|
||
definition: {
|
||
type: Sequelize.INTEGER(11),
|
||
allowNull: false,
|
||
defaultValue: 0,
|
||
comment: '待验证域名校验尝试次数',
|
||
},
|
||
},
|
||
{
|
||
name: 'pending_last_error',
|
||
after: 'pending_verify_attempts',
|
||
definition: {
|
||
type: Sequelize.STRING(500),
|
||
allowNull: true,
|
||
comment: '待验证域名最近一次失败原因',
|
||
},
|
||
},
|
||
{
|
||
name: 'pending_created_at',
|
||
after: 'pending_last_error',
|
||
definition: {
|
||
type: Sequelize.DATE,
|
||
allowNull: true,
|
||
comment: '发起域名切换的时间',
|
||
},
|
||
},
|
||
{
|
||
name: 'retired_domain',
|
||
after: 'pending_created_at',
|
||
definition: {
|
||
type: Sequelize.STRING(100),
|
||
allowNull: true,
|
||
comment: '待下线旧域名:域名提升时写入,外部证书脚本删除其 nginx 配置与证书文件后置 NULL',
|
||
},
|
||
},
|
||
{
|
||
name: 'retired_at',
|
||
after: 'retired_domain',
|
||
definition: {
|
||
type: Sequelize.DATE,
|
||
allowNull: true,
|
||
comment: '旧域名进入下线队列的时间',
|
||
},
|
||
},
|
||
];
|
||
|
||
for (const item of columnDefs) {
|
||
if (!columns[item.name]) {
|
||
await queryInterface.addColumn(TABLE_NAME, item.name, {
|
||
...item.definition,
|
||
after: item.after,
|
||
});
|
||
}
|
||
}
|
||
|
||
if (!(await indexExists(queryInterface, TABLE_NAME, PENDING_DOMAIN_INDEX_NAME))) {
|
||
await queryInterface.addIndex(TABLE_NAME, [ 'pending_domain' ], {
|
||
name: PENDING_DOMAIN_INDEX_NAME,
|
||
});
|
||
}
|
||
|
||
if (!(await indexExists(queryInterface, TABLE_NAME, RETIRED_DOMAIN_INDEX_NAME))) {
|
||
await queryInterface.addIndex(TABLE_NAME, [ 'retired_domain' ], {
|
||
name: RETIRED_DOMAIN_INDEX_NAME,
|
||
});
|
||
}
|
||
},
|
||
/**
|
||
* 回滚迁移:删除两阶段域名切换相关索引与列(幂等)。
|
||
*
|
||
* 注意:回滚会一并丢弃待验证域名与下线队列(pending_* / retired_*),
|
||
* 但不会改动生效域名 domain、domain_status、证书与同步状态,因此不影响线上已生效域名。
|
||
* 回滚后代码若仍为两阶段版本,setDomain / checkDomain 会因缺少 pending_* 列而报错,
|
||
* 需与代码版本一并回退。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async down(queryInterface) {
|
||
if (!(await tableExists(queryInterface, TABLE_NAME))) {
|
||
return;
|
||
}
|
||
|
||
for (const indexName of [ RETIRED_DOMAIN_INDEX_NAME, PENDING_DOMAIN_INDEX_NAME ]) {
|
||
if (await indexExists(queryInterface, TABLE_NAME, indexName)) {
|
||
await queryInterface.removeIndex(TABLE_NAME, indexName);
|
||
}
|
||
}
|
||
|
||
const columns = await queryInterface.describeTable(TABLE_NAME);
|
||
// 倒序删除(与 up 的列序相反):先删 retired 组,再删 pending 组
|
||
for (const columnName of [
|
||
'retired_at',
|
||
'retired_domain',
|
||
'pending_created_at',
|
||
'pending_last_error',
|
||
'pending_verify_attempts',
|
||
'pending_verify_token',
|
||
'pending_verify_host',
|
||
'pending_domain',
|
||
]) {
|
||
if (columns[columnName]) {
|
||
await queryInterface.removeColumn(TABLE_NAME, columnName);
|
||
}
|
||
}
|
||
},
|
||
};
|