add tests and migration

This commit is contained in:
Alex Holliday committed 2026-08-28 14:42:46 -07:00
1 parent e60bfd18ff
commit e964234f54
5 files changed
+230 -2

No files matched your search

@@ -0,0 +1,36 @@
import { MonitorModel } from "../../domain/monitors/monitor.model.js";
import type { ILogger } from "@/utils/logger.js";
/**
* Rewrite docker monitor urls from the old container-name semantics to host urls.
*
* The old docker provider interpreted monitor.url as a container name/ID and only
* ever talked to the local socket, so every pre-existing docker monitor's host is
* definitionally unix:///var/run/docker.sock. The container name is preserved in
* the description. Urls already in host form (unix://, ssh://, or an absolute
* socket path) are left untouched, which makes the migration idempotent.
*/
export async function migrateDockerMonitorUrls(logger: ILogger): Promise<void> {
const SERVICE_NAME = "Migration:MigrateDockerMonitorUrls";
try {
logger.info({ service: SERVICE_NAME, message: "Starting docker monitor url migration" });
const result = await MonitorModel.updateMany({ type: "docker", url: { $not: /^((unix|ssh):\/\/|\/)/ } }, [
{
$set: {
description: {
$concat: [{ $ifNull: ["$description", ""] }, " (was container: ", "$url", ")"],
},
url: "unix:///var/run/docker.sock",
},
},
]);
logger.info({ service: SERVICE_NAME, message: `Migrated ${result.modifiedCount} docker monitors to host urls` });
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
logger.error({ service: SERVICE_NAME, message: `Error migrating docker monitor urls: ${errorMessage}` });
throw error;
}
}
+2
View File
@@ -10,6 +10,7 @@ import { backfillMonitorLastEvaluatedAt } from "./0008_backfillMonitorLastEvalua
import { recomputeResponseTimeFromTimings } from "./0009_recomputeResponseTimeFromTimings.js";
import { slimRecentChecks } from "./0010_slimRecentChecks.js";
import { backfillMonitorProxyMode } from "./0011_backfillMonitorProxyMode.js";
import { migrateDockerMonitorUrls } from "./0012_migrateDockerMonitorUrls.js";
import type { ILogger } from "@/utils/logger.js";
type MigrationEntry = {
@@ -29,6 +30,7 @@ const migrations: MigrationEntry[] = [
{ name: "0009_recomputeResponseTimeFromTimings", execute: recomputeResponseTimeFromTimings },
{ name: "0010_slimRecentChecks", execute: slimRecentChecks },
{ name: "0011_backfillMonitorProxyMode", execute: backfillMonitorProxyMode },
{ name: "0012_migrateDockerMonitorUrls", execute: migrateDockerMonitorUrls },
];
const runMigrations = async (logger: ILogger) => {