#46

Fix Railway migration runtime

MergedstylesshOwnermerged intofrom

Summary

  • bundle the Postgres migration script into the slim TanStack Start runner image
  • keep the existing Railway pre-deploy command and service configuration unchanged
  • add esbuild as an explicit build dependency

Root cause

The runner copied scripts/migrate.mjs without node_modules, so staging pre-deploy failed with ERR_MODULE_NOT_FOUND for pg. Demo succeeded because it does not run database migrations.

Validation

  • pnpm install --frozen-lockfile
  • pnpm lint
  • pnpm typecheck
  • pnpm test (169 passed)
  • pnpm build
  • Docker image build
  • bundled migration applied all 11 migrations to temporary Postgres and was idempotent
  • final container /api/health returned 200

Activity

6
mikeyreviewed2mo ago
mikey2mo ago

Review: Fix Railway migration runtime

Verification performed (not just static reading): reproduced the actual failure/fix locally.

  • Installed pg@8.23.0 and esbuild@0.28.2 (exact versions pinned in the lockfile) and ran the same esbuild bundle command from Dockerfile:13 against scripts/migrate.mjs.
  • Confirmed the bundled output has zero runtime dependency on node_modules (no require('pg') left unresolved — this is exactly the ERR_MODULE_NOT_FOUND the PR describes).
  • Confirmed the pg native-binding require('pg-native') path (pg/lib/native/client.js) is wrapped in try/catch both pre- and post-bundle, so the bundle doesn't hard-fail when pg-native isn't installed (it isn't, by design — pg-native is an optional peer dep here).
  • Spun up a real local Postgres 17 instance and ran the bundled migrate.mjs against it using this repo's actual 11 migration files (lib/db/migrations/0000_modern_vance_astro.sql … 0010_purge-ai-provider-keys.sql). All 11 applied successfully in order, matching the PR's own validation notes.
  • Re-ran the bundled script against the same database a second time: it correctly reported "Database migrations are already current." — confirms idempotency, no double-apply or advisory-lock deadlock.
  • Verified the relative-path resolution (fileURLToPath(new URL("../lib/db/migrations/", import.meta.url))) still resolves correctly post-bundle, since the Dockerfile places migrate.mjs at runner:/app/scripts/migrate.mjs with lib/db/migrations copied to runner:/app/lib/db/migrations — same relative layout as the source tree, so no path breakage from the bundle/copy relocation (build:/app/migrate.mjs → runner:/app/scripts/migrate.mjs).

Scope check: diff is minimal and matches the stated intent — one new RUN esbuild ... layer in the build stage, the COPY source flipped to the bundled output, and esbuild added as an explicit (already-transitive) devDependency with a matching lockfile entry. No unrelated changes.

Result: No blocking correctness, security, or maintainability issues found. The fix addresses the described root cause and is empirically confirmed to produce a working, idempotent migration runner independent of node_modules.

Non-blocking observation: the esbuild bundle command is duplicated as an inline RUN string in the Dockerfile with no pinned checksum verification beyond the lockfile — acceptable as-is, but if this pattern is reused for other scripts, moving it into a small package.json script (e.g. build:migrate) would keep the Dockerfile thinner and give the command a single source of truth. Not a blocker for this PR.

stylessh merged commit 12f89e2 into main2mo ago

Branch worktree-keen-indigo-valley has been merged.

Checking branch