Skip to content

Commit 6307a8f

Browse files
OriginDevITclaude
andauthored
fix(db): migrate script loads repo-root .env (#16)
`pnpm migrate:dev` from a clean shell failed with "DATABASE_URL_ADMIN is not set" because the runner only read process.env and nothing loaded .env. Load the repo-root .env via process.loadEnvFile() before reading the vars - anchored to the script's own path, since `pnpm --filter` runs with packages/db as the cwd. A missing .env is swallowed, so CI and production (which set the vars directly) are unaffected. Verified from a clean shell with nothing exported: - .env present, DB already migrated -> exit 0, no error - DATABASE_URL_ADMIN sourced only from .env -> applies all 13 - no .env + var exported -> works - no .env + nothing set -> the intended "not set" error, no crash Claude-Session: https://claude.ai/code/session_01Puf9jhj137U3iswf4GWZkG Signed-off-by: Matthew Wren <info@origindev.com> Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 19bceb0 commit 6307a8f

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

packages/db/src/migrate.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
* (DATABASE_URL_ADMIN, falling back to DATABASE_URL for single-URL setups).
55
*
66
* pnpm --filter @osds/db migrate # or, from the repo root: pnpm migrate:dev
7+
*
8+
* Loads the repo-root .env first, so it works from a clean shell with nothing
9+
* exported. A real environment (CI, production) sets the vars directly and
10+
* needs no .env.
711
*/
812
import { promises as fs } from "node:fs";
913
import * as path from "node:path";
14+
import { loadEnvFile } from "node:process";
1015
import { pathToFileURL } from "node:url";
1116
import {
1217
Kysely,
@@ -17,6 +22,15 @@ import {
1722
} from "kysely";
1823
import { Pool } from "pg";
1924

25+
// packages/db/src (or dist) -> repo root. Anchored to this file, not the cwd,
26+
// because `pnpm --filter` runs with the package as the working directory.
27+
const repoRootEnv = path.resolve(import.meta.dirname, "../../../.env");
28+
try {
29+
loadEnvFile(repoRootEnv);
30+
} catch {
31+
// No .env at the repo root - rely on the ambient environment.
32+
}
33+
2034
const migrationFolder = path.join(import.meta.dirname, "migrations");
2135

2236
/** Loads ./migrations/NNNN_*.{ts,js} via file:// URLs so it also works on Windows. */

0 commit comments

Comments
 (0)