Skip to content

Commit 8211f09

Browse files
committed
feat(dashboard-agent-db): run schema migrations on container start
Self-hosted containers now apply the trigger_dashboard_agent schema migrations on startup, alongside the Prisma and ClickHouse migrations, via a small runtime migrator (drizzle-orm over the committed SQL, no drizzle-kit in the image). A new SKIP_DASHBOARD_AGENT_MIGRATIONS flag lets deployments that apply migrations out of band opt out.
1 parent 0890055 commit 8211f09

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

docker/scripts/entrypoint.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ else
1313
echo "SKIP_POSTGRES_MIGRATIONS=1, skipping Postgres migrations."
1414
fi
1515

16+
if [ "$SKIP_DASHBOARD_AGENT_MIGRATIONS" != "1" ]; then
17+
echo "Running dashboard agent migrations"
18+
pnpm --filter @internal/dashboard-agent-db db:migrate:deploy
19+
echo "Dashboard agent migrations done"
20+
else
21+
echo "SKIP_DASHBOARD_AGENT_MIGRATIONS=1, skipping dashboard agent migrations."
22+
fi
23+
1624
if [ -n "$CLICKHOUSE_URL" ] && [ "$SKIP_CLICKHOUSE_MIGRATIONS" != "1" ]; then
1725
# Run ClickHouse migrations
1826
echo "Running ClickHouse migrations..."
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Production migration runner for the `trigger_dashboard_agent` schema.
2+
//
3+
// Runs under plain `node migrate.mjs` in the built image: `drizzle-orm` and
4+
// `postgres` are runtime dependencies, so this needs no `drizzle-kit`, `tsx`,
5+
// or build step (keeps the image lean). The OSS container runs this from its
6+
// entrypoint; cloud runs it out-of-band against its own database.
7+
import { dirname, join } from "node:path";
8+
import { fileURLToPath } from "node:url";
9+
import { drizzle } from "drizzle-orm/postgres-js";
10+
import { migrate } from "drizzle-orm/postgres-js/migrator";
11+
import postgres from "postgres";
12+
13+
// Cloud points at the dedicated dashboard-agent database; OSS falls back to the
14+
// main DATABASE_URL (tables still land in the `trigger_dashboard_agent` schema).
15+
const connectionString = process.env.DASHBOARD_AGENT_DATABASE_URL ?? process.env.DATABASE_URL;
16+
17+
if (!connectionString) {
18+
console.error(
19+
"[dashboard-agent-db] DASHBOARD_AGENT_DATABASE_URL / DATABASE_URL not set; cannot migrate."
20+
);
21+
process.exit(1);
22+
}
23+
24+
// Prisma-style URLs carry `?schema=...`; postgres.js forwards unknown query
25+
// params as server startup config and Postgres rejects `schema`. Our tables are
26+
// schema-qualified, so the param is unnecessary — drop it.
27+
function normalizeConnectionString(value) {
28+
try {
29+
const url = new URL(value);
30+
url.searchParams.delete("schema");
31+
return url.toString();
32+
} catch {
33+
return value;
34+
}
35+
}
36+
37+
const migrationsFolder = join(dirname(fileURLToPath(import.meta.url)), "drizzle");
38+
const sql = postgres(normalizeConnectionString(connectionString), {
39+
max: 1,
40+
prepare: false,
41+
// Silence the "schema/relation already exists, skipping" notices the journal's
42+
// idempotent CREATE IF NOT EXISTS emits on every re-run, so restart logs stay clean.
43+
onnotice: () => {},
44+
});
45+
46+
try {
47+
// Journal lives in Drizzle's default `drizzle` schema (matching `drizzle-kit
48+
// migrate`, so dev and deploy track migrations the same way). It must not be
49+
// our data schema: the first migration runs `CREATE SCHEMA
50+
// "trigger_dashboard_agent"`, which would collide with the journal schema the
51+
// migrator pre-creates. The dashboard agent is the only Drizzle user of its
52+
// database, so the `drizzle` schema stays exclusively ours.
53+
await migrate(drizzle(sql), { migrationsFolder });
54+
console.log("[dashboard-agent-db] migrations complete");
55+
} finally {
56+
await sql.end();
57+
}

internal-packages/dashboard-agent-db/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"typecheck": "tsc --noEmit",
1717
"db:generate": "drizzle-kit generate",
1818
"db:migrate": "drizzle-kit migrate",
19+
"db:migrate:deploy": "node migrate.mjs",
1920
"db:studio": "drizzle-kit studio"
2021
}
2122
}

0 commit comments

Comments
 (0)