Seed satatus data for resources, sites, and hc

This commit is contained in:
Owen
2026-04-28 15:14:43 -07:00
parent 208289f498
commit b81ae3d998
2 changed files with 179 additions and 11 deletions

View File

@@ -1,5 +1,6 @@
import { db } from "@server/db/pg/driver";
import { sql } from "drizzle-orm";
import { SiJfrog } from "react-icons/si";
const version = "1.18.0";
@@ -67,11 +68,12 @@ export default async function migration() {
FROM "siteResources" sr
WHERE sr."siteId" IS NOT NULL`
);
const existingSiteResourcesForNetwork = siteResourcesForNetworkQuery.rows as {
siteResourceId: number;
orgId: string;
siteId: number;
}[];
const existingSiteResourcesForNetwork =
siteResourcesForNetworkQuery.rows as {
siteResourceId: number;
orgId: string;
siteId: number;
}[];
console.log(
`Found ${existingSiteResourcesForNetwork.length} existing siteResource(s) to migrate to networks`
@@ -446,10 +448,7 @@ export default async function migration() {
`Migrated ${existingHealthChecks.length} targetHealthCheck row(s) with corrected IDs`
);
} catch (e) {
console.error(
"Error while migrating targetHealthCheck rows:",
e
);
console.error("Error while migrating targetHealthCheck rows:", e);
throw e;
}
}
@@ -493,5 +492,90 @@ export default async function migration() {
}
}
// Seed statusHistory for all existing sites
try {
const sitesQuery = await db.execute(
sql`SELECT "siteId", "orgId", "online" FROM "sites"`
);
const allSites = sitesQuery.rows as {
siteId: number;
orgId: string;
online: boolean;
}[];
const now = Math.floor(Date.now() / 1000);
for (const site of allSites) {
await db.execute(sql`
INSERT INTO "statusHistory" ("entityType", "entityId", "orgId", "status", "timestamp")
VALUES ('site', ${site.siteId}, ${site.orgId}, ${site.online ? "online" : "offline"}, ${now})
`);
}
console.log(`Seeded statusHistory for ${allSites.length} site(s)`);
} catch (e) {
console.error("Error while seeding statusHistory for sites:", e);
throw e;
}
// Seed statusHistory for all existing resources
try {
const resourcesQuery = await db.execute(
sql`SELECT "resourceId", "orgId", "health" FROM "resources"`
);
const allResources = resourcesQuery.rows as {
resourceId: number;
orgId: string;
health: string | null;
}[];
const now = Math.floor(Date.now() / 1000);
for (const resource of allResources) {
await db.execute(sql`
INSERT INTO "statusHistory" ("entityType", "entityId", "orgId", "status", "timestamp")
VALUES ('resource', ${resource.resourceId}, ${resource.orgId}, ${resource.health ?? "unknown"}, ${now})
`);
}
console.log(
`Seeded statusHistory for ${allResources.length} resource(s)`
);
} catch (e) {
console.error("Error while seeding statusHistory for resources:", e);
throw e;
}
// Seed statusHistory for all existing health checks
try {
const healthChecksQuery = await db.execute(
sql`SELECT "targetHealthCheckId", "orgId", "hcHealth" FROM "targetHealthCheck"`
);
const allHealthChecks = healthChecksQuery.rows as {
targetHealthCheckId: number;
orgId: string;
hcHealth: string | null;
}[];
const now = Math.floor(Date.now() / 1000);
for (const hc of allHealthChecks) {
await db.execute(sql`
INSERT INTO "statusHistory" ("entityType", "entityId", "orgId", "status", "timestamp")
VALUES ('health_check', ${hc.targetHealthCheckId}, ${hc.orgId}, ${hc.hcHealth ?? "unknown"}, ${now})
`);
}
console.log(
`Seeded statusHistory for ${allHealthChecks.length} health check(s)`
);
} catch (e) {
console.error(
"Error while seeding statusHistory for health checks:",
e
);
throw e;
}
console.log(`${version} migration complete`);
}

View File

@@ -340,7 +340,6 @@ export default async function migration() {
ALTER TABLE 'resources' ADD 'wildcard' integer DEFAULT false NOT NULL;
`
).run();
})();
db.pragma("foreign_keys = ON");
@@ -364,7 +363,11 @@ export default async function migration() {
const result = insertNetwork.run("resource", sr.orgId);
const networkId = result.lastInsertRowid as number;
insertSiteNetwork.run(sr.siteId, networkId);
updateSiteResource.run(networkId, networkId, sr.siteResourceId);
updateSiteResource.run(
networkId,
networkId,
sr.siteResourceId
);
}
});
@@ -454,6 +457,87 @@ export default async function migration() {
}
console.log(`Migrated database`);
// Seed statusHistory for all existing sites
const allSites = db
.prepare(`SELECT "siteId", "orgId", "online" FROM 'sites'`)
.all() as { siteId: number; orgId: string; online: number }[];
const insertSiteHistory = db.prepare(
`INSERT INTO 'statusHistory' ("entityType", "entityId", "orgId", "status", "timestamp") VALUES (?, ?, ?, ?, ?)`
);
const now = Math.floor(Date.now() / 1000);
const seedSites = db.transaction(() => {
for (const site of allSites) {
insertSiteHistory.run(
"site",
site.siteId,
site.orgId,
site.online ? "online" : "offline",
now
);
}
});
seedSites();
console.log(`Seeded statusHistory for ${allSites.length} site(s)`);
// Seed statusHistory for all existing resources
const allResources = db
.prepare(`SELECT "resourceId", "orgId", "health" FROM 'resources'`)
.all() as {
resourceId: number;
orgId: string;
health: string | null;
}[];
const insertResourceHistory = db.prepare(
`INSERT INTO 'statusHistory' ("entityType", "entityId", "orgId", "status", "timestamp") VALUES (?, ?, ?, ?, ?)`
);
const seedResources = db.transaction(() => {
for (const resource of allResources) {
insertResourceHistory.run(
"resource",
resource.resourceId,
resource.orgId,
resource.health ?? "unknown",
now
);
}
});
seedResources();
console.log(
`Seeded statusHistory for ${allResources.length} resource(s)`
);
// Seed statusHistory for all existing health checks
const allHealthChecks = db
.prepare(
`SELECT "targetHealthCheckId", "orgId", "hcHealth" FROM 'targetHealthCheck'`
)
.all() as {
targetHealthCheckId: number;
orgId: string;
hcHealth: string | null;
}[];
const insertHealthCheckHistory = db.prepare(
`INSERT INTO 'statusHistory' ("entityType", "entityId", "orgId", "status", "timestamp") VALUES (?, ?, ?, ?, ?)`
);
const seedHealthChecks = db.transaction(() => {
for (const hc of allHealthChecks) {
insertHealthCheckHistory.run(
"health_check",
hc.targetHealthCheckId,
hc.orgId,
hc.hcHealth ?? "unknown",
now
);
}
});
seedHealthChecks();
console.log(
`Seeded statusHistory for ${allHealthChecks.length} health check(s)`
);
} catch (e) {
console.log("Failed to migrate db:", e);
throw e;