Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions internal-packages/dashboard-agent-db/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ The conversation datastore for the in-dashboard agent, isolated from the main
Prisma database. Drizzle (postgres-js) over a dedicated `trigger_dashboard_agent`
Postgres schema.

- **Cloud:** a separate PlanetScale Postgres database (`DASHBOARD_AGENT_DATABASE_URL`),
reached over a standard pooled connection.
- **OSS / self-host:** falls back to the main `DATABASE_URL`; the tables live in
the dedicated `trigger_dashboard_agent` schema, isolated from Prisma's `public`.
- **Cloud:** a separate PlanetScale Postgres database. The app connects over a
pooled connection (`DASHBOARD_AGENT_DATABASE_URL`); migrations run over a direct
(non-pooler) connection (`DASHBOARD_AGENT_DIRECT_URL`), since a transaction-mode
pooler can't run the migrator.
- **OSS / self-host:** falls back to the main `DATABASE_URL` (and `DIRECT_URL` for
migrations); the tables live in the dedicated `trigger_dashboard_agent` schema,
isolated from Prisma's `public`.

The schema is **foreign-key-free** — it references main entities (`organizationId`,
`userId`) by id only, because in cloud it lives in a different database.
Expand Down Expand Up @@ -36,7 +39,7 @@ of truth.

```bash
pnpm run db:generate # generate SQL migration from src/schema.ts (offline)
pnpm run db:migrate # apply migrations (needs DASHBOARD_AGENT_DATABASE_URL or DATABASE_URL)
pnpm run db:migrate # apply migrations (direct url: DASHBOARD_AGENT_DIRECT_URL, falling back to DASHBOARD_AGENT_DATABASE_URL / DIRECT_URL / DATABASE_URL)
```

drizzle-kit is scoped to the `trigger_dashboard_agent` schema (`schemaFilter`), so
Expand Down
8 changes: 6 additions & 2 deletions internal-packages/dashboard-agent-db/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { defineConfig } from "drizzle-kit";

// Cloud points at the dedicated PlanetScale database; OSS falls back to the main
// DATABASE_URL (tables still land in the trigger_dashboard_agent schema).
// Migrations need a direct (non-pooler) connection; a transaction-mode pooler
// can't run the migrator. Prefer the agent's direct url, then its pooled url,
// then the main DIRECT_URL/DATABASE_URL (OSS single-database fallback; tables
// still land in the trigger_dashboard_agent schema).
const url =
process.env.DASHBOARD_AGENT_DIRECT_URL ??
process.env.DASHBOARD_AGENT_DATABASE_URL ??
process.env.DIRECT_URL ??
process.env.DATABASE_URL ??
"postgres://placeholder"; // generate is offline; a real url is only needed for migrate/studio

Expand Down
9 changes: 7 additions & 2 deletions internal-packages/dashboard-agent-db/migrate-status.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ import postgres from "postgres";
const MIGRATIONS_SCHEMA = "drizzle";
const MIGRATIONS_TABLE = "__dashboard_agent_migrations";

const connectionString = process.env.DASHBOARD_AGENT_DATABASE_URL ?? process.env.DATABASE_URL;
// Match migrate.mjs: a direct (non-pooler) connection, same precedence.
const connectionString =
process.env.DASHBOARD_AGENT_DIRECT_URL ??
process.env.DASHBOARD_AGENT_DATABASE_URL ??
process.env.DIRECT_URL ??
process.env.DATABASE_URL;

if (!connectionString) {
console.error(
"[dashboard-agent-db] DASHBOARD_AGENT_DATABASE_URL / DATABASE_URL not set; cannot check status."
"[dashboard-agent-db] No database url set (DASHBOARD_AGENT_DIRECT_URL / DASHBOARD_AGENT_DATABASE_URL / DIRECT_URL / DATABASE_URL); cannot check status."
);
process.exit(2);
}
Expand Down
14 changes: 10 additions & 4 deletions internal-packages/dashboard-agent-db/migrate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ import { drizzle } from "drizzle-orm/postgres-js";
import { migrate } from "drizzle-orm/postgres-js/migrator";
import postgres from "postgres";

// Cloud points at the dedicated dashboard-agent database; OSS falls back to the
// main DATABASE_URL (tables still land in the `trigger_dashboard_agent` schema).
const connectionString = process.env.DASHBOARD_AGENT_DATABASE_URL ?? process.env.DATABASE_URL;
// Migrations need a direct (non-pooler) connection; a transaction-mode pooler
// can't run the migrator. Prefer the agent's direct url, then its pooled url,
// then the main DIRECT_URL/DATABASE_URL (OSS single-database fallback; tables
// still land in the `trigger_dashboard_agent` schema).
const connectionString =
process.env.DASHBOARD_AGENT_DIRECT_URL ??
process.env.DASHBOARD_AGENT_DATABASE_URL ??
process.env.DIRECT_URL ??
process.env.DATABASE_URL;

if (!connectionString) {
console.error(
"[dashboard-agent-db] DASHBOARD_AGENT_DATABASE_URL / DATABASE_URL not set; cannot migrate."
"[dashboard-agent-db] No database url set (DASHBOARD_AGENT_DIRECT_URL / DASHBOARD_AGENT_DATABASE_URL / DIRECT_URL / DATABASE_URL); cannot migrate."
);
process.exit(1);
}
Expand Down