2026-04-07 21:02:37 +00:00
|
|
|
/// <reference types="node" />
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* dcrouter migration runner.
|
|
|
|
|
*
|
|
|
|
|
* Uses @push.rocks/smartmigration via dynamic import so smartmigration's type
|
|
|
|
|
* chain (which pulls in mongodb 7.x and related types) doesn't leak into
|
|
|
|
|
* compile-time type checking for this folder.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/** Matches the subset of IMigrationRunResult we actually log. */
|
|
|
|
|
export interface IMigrationRunResult {
|
|
|
|
|
stepsApplied: Array<unknown>;
|
|
|
|
|
wasFreshInstall: boolean;
|
|
|
|
|
currentVersionBefore: string | null;
|
|
|
|
|
currentVersionAfter: string;
|
|
|
|
|
totalDurationMs: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface IMigrationRunner {
|
|
|
|
|
run(): Promise<IMigrationRunResult>;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 23:02:42 +00:00
|
|
|
async function migrateTargetProfileTargetHosts(ctx: {
|
|
|
|
|
mongo?: { collection: (name: string) => any };
|
|
|
|
|
log: { log: (level: 'info', message: string) => void };
|
|
|
|
|
}): Promise<void> {
|
|
|
|
|
const collection = ctx.mongo!.collection('TargetProfileDoc');
|
|
|
|
|
const cursor = collection.find({ 'targets.host': { $exists: true } });
|
|
|
|
|
let migrated = 0;
|
|
|
|
|
|
|
|
|
|
for await (const doc of cursor) {
|
|
|
|
|
const targets = ((doc as any).targets || []).map((target: any) => {
|
|
|
|
|
if (target && typeof target === 'object' && 'host' in target && !('ip' in target)) {
|
|
|
|
|
const { host, ...rest } = target;
|
|
|
|
|
return { ...rest, ip: host };
|
|
|
|
|
}
|
|
|
|
|
return target;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await collection.updateOne({ _id: (doc as any)._id }, { $set: { targets } });
|
|
|
|
|
migrated++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.log.log('info', `rename-target-profile-host-to-ip: migrated ${migrated} profile(s)`);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 21:02:37 +00:00
|
|
|
/**
|
|
|
|
|
* Create a configured SmartMigration runner with all dcrouter migration steps registered.
|
|
|
|
|
*
|
|
|
|
|
* Call `.run()` on the returned instance at startup (after DcRouterDb is ready,
|
|
|
|
|
* before any service that reads migrated collections).
|
|
|
|
|
*
|
|
|
|
|
* @param db - The initialized SmartdataDb instance from DcRouterDb.getDb()
|
|
|
|
|
* @param targetVersion - The current app version (from commitinfo.version)
|
|
|
|
|
*/
|
|
|
|
|
export async function createMigrationRunner(
|
|
|
|
|
db: unknown,
|
|
|
|
|
targetVersion: string,
|
|
|
|
|
): Promise<IMigrationRunner> {
|
|
|
|
|
const sm = await import('@push.rocks/smartmigration');
|
|
|
|
|
const migration = new sm.SmartMigration({
|
|
|
|
|
targetVersion,
|
|
|
|
|
db: db as any,
|
|
|
|
|
// Brand-new installs skip all migrations and stamp directly to the current version.
|
|
|
|
|
freshInstallVersion: targetVersion,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Register steps in execution order. Each step's .from() must match the
|
|
|
|
|
// previous step's .to() to form a contiguous chain.
|
|
|
|
|
migration
|
|
|
|
|
.step('rename-target-profile-host-to-ip')
|
|
|
|
|
.from('13.0.11').to('13.1.0')
|
|
|
|
|
.description('Rename ITargetProfileTarget.host → ip on all target profiles')
|
2026-04-13 23:02:42 +00:00
|
|
|
.up(async (ctx) => migrateTargetProfileTargetHosts(ctx))
|
2026-04-08 14:54:49 +00:00
|
|
|
.step('rename-domain-source-manual-to-dcrouter')
|
|
|
|
|
.from('13.1.0').to('13.8.1')
|
|
|
|
|
.description('Rename DomainDoc.source value from "manual" to "dcrouter"')
|
|
|
|
|
.up(async (ctx) => {
|
|
|
|
|
const collection = ctx.mongo!.collection('domaindoc');
|
|
|
|
|
const result = await collection.updateMany(
|
|
|
|
|
{ source: 'manual' },
|
|
|
|
|
{ $set: { source: 'dcrouter' } },
|
|
|
|
|
);
|
|
|
|
|
ctx.log.log(
|
|
|
|
|
'info',
|
|
|
|
|
`rename-domain-source-manual-to-dcrouter: migrated ${result.modifiedCount} domain(s)`,
|
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
.step('rename-record-source-manual-to-local')
|
|
|
|
|
.from('13.8.1').to('13.8.2')
|
|
|
|
|
.description('Rename DnsRecordDoc.source value from "manual" to "local"')
|
|
|
|
|
.up(async (ctx) => {
|
|
|
|
|
const collection = ctx.mongo!.collection('dnsrecorddoc');
|
|
|
|
|
const result = await collection.updateMany(
|
|
|
|
|
{ source: 'manual' },
|
|
|
|
|
{ $set: { source: 'local' } },
|
|
|
|
|
);
|
|
|
|
|
ctx.log.log(
|
|
|
|
|
'info',
|
|
|
|
|
`rename-record-source-manual-to-local: migrated ${result.modifiedCount} record(s)`,
|
|
|
|
|
);
|
2026-04-13 17:38:23 +00:00
|
|
|
})
|
|
|
|
|
.step('unify-routes-rename-collection')
|
|
|
|
|
.from('13.8.2').to('13.16.0')
|
2026-04-13 18:08:36 +00:00
|
|
|
.description('Rename StoredRouteDoc → RouteDoc, add origin field, drop RouteOverrideDoc')
|
2026-04-13 17:38:23 +00:00
|
|
|
.up(async (ctx) => {
|
|
|
|
|
const db = ctx.mongo!;
|
|
|
|
|
|
2026-04-13 18:08:36 +00:00
|
|
|
// 1. Rename StoredRouteDoc → RouteDoc (smartdata uses exact class names)
|
|
|
|
|
const collections = await db.listCollections({ name: 'StoredRouteDoc' }).toArray();
|
2026-04-13 17:38:23 +00:00
|
|
|
if (collections.length > 0) {
|
2026-04-13 18:08:36 +00:00
|
|
|
await db.renameCollection('StoredRouteDoc', 'RouteDoc');
|
|
|
|
|
ctx.log.log('info', 'Renamed StoredRouteDoc → RouteDoc');
|
2026-04-13 17:38:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. Set origin='api' on all migrated docs (they were API-created)
|
2026-04-13 18:08:36 +00:00
|
|
|
const routeCol = db.collection('RouteDoc');
|
2026-04-13 17:38:23 +00:00
|
|
|
const result = await routeCol.updateMany(
|
|
|
|
|
{ origin: { $exists: false } },
|
|
|
|
|
{ $set: { origin: 'api' } },
|
|
|
|
|
);
|
|
|
|
|
ctx.log.log('info', `Set origin='api' on ${result.modifiedCount} migrated route(s)`);
|
|
|
|
|
|
2026-04-13 18:08:36 +00:00
|
|
|
// 3. Drop RouteOverrideDoc collection
|
|
|
|
|
const overrideCollections = await db.listCollections({ name: 'RouteOverrideDoc' }).toArray();
|
2026-04-13 17:38:23 +00:00
|
|
|
if (overrideCollections.length > 0) {
|
2026-04-13 18:08:36 +00:00
|
|
|
await db.collection('RouteOverrideDoc').drop();
|
|
|
|
|
ctx.log.log('info', 'Dropped RouteOverrideDoc collection');
|
2026-04-13 17:38:23 +00:00
|
|
|
}
|
2026-04-13 23:02:42 +00:00
|
|
|
})
|
|
|
|
|
.step('repair-target-profile-ip-migration')
|
|
|
|
|
.from('13.16.0').to('13.17.4')
|
|
|
|
|
.description('Repair TargetProfileDoc.targets host→ip migration for already-upgraded installs')
|
|
|
|
|
.up(async (ctx) => {
|
|
|
|
|
await migrateTargetProfileTargetHosts(ctx);
|
2026-04-07 21:02:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return migration;
|
|
|
|
|
}
|