Skip to content

fix(db): migrate script loads repo-root .env - #16

Merged
OriginDevIT merged 1 commit into
mainfrom
fix/migrate-loads-dotenv
Aug 30, 2026
Merged

fix(db): migrate script loads repo-root .env#16
OriginDevIT merged 1 commit into
mainfrom
fix/migrate-loads-dotenv

Conversation

@OriginDevIT

Copy link
Copy Markdown
Owner

Problem

pnpm migrate:dev from a clean shell fails with DATABASE_URL_ADMIN is not set even though .env defines it. The runner only reads process.env, and nothing loads .env.

Fix

packages/db/src/migrate.ts calls process.loadEnvFile() on the repo-root .env before reading the connection vars. The path is resolved from the script's own location (import.meta.dirname../../../.env), not the cwd, because pnpm --filter runs with packages/db as the working directory. A missing file is caught and ignored, so CI and production — which set the vars directly — are unaffected. No new dependency (Node built-in).

Verified from a clean shell, nothing exported

Scenario Result
.env present, DB already migrated exit 0, no error (was: crash)
DATABASE_URL_ADMIN sourced only from .env, fresh DB applies all 13 migrations
no .env, var exported (CI/prod path) works — loadEnvFile throw is swallowed
no .env, nothing set the intended Error: DATABASE_URL_ADMIN (or DATABASE_URL) is not set, not a stack trace from loadEnvFile

pnpm typecheck / lint / test pass.

🤖 Generated with Claude Code

`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

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Puf9jhj137U3iswf4GWZkG
Signed-off-by: Matthew Wren <info@origindev.com>
@OriginDevIT
OriginDevIT merged commit 6307a8f into main Aug 30, 2026
1 check passed
@OriginDevIT
OriginDevIT deleted the fix/migrate-loads-dotenv branch August 30, 2026 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant