fix: lazy-import db in test-data cleanup route to prevent build-time crash#1161
Conversation
…ilure The cleanup route imported the db module at the top level, which reads POSTGRES_URL. During next build, static analysis evaluates this import and throws when the env var is missing. Adding force-dynamic skips static generation — matching the pattern used by sibling test routes. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Updates to Preview Branch (worktree/transient-enchanting-giraffe) ↗︎
Tasks are run on every commit but only new migration files are pushed.
View logs for this Workflow Run ↗︎. |
There was a problem hiding this comment.
Pull request overview
Ensures the /api/test-data/cleanup route is treated as dynamic so next build won’t eagerly evaluate server-only DB setup when environment variables aren’t present.
Changes:
- Adds
export const dynamic = "force-dynamic"to the test-data cleanup route.
| import { db } from "~/server/db"; | ||
|
|
||
| export const dynamic = "force-dynamic"; | ||
| import { log } from "~/lib/logger"; |
There was a problem hiding this comment.
export const dynamic = "force-dynamic" is currently placed between import statements, leaving import { log ... } (and the schema import) after a non-import statement. This diverges from the pattern used in other route handlers (e.g., src/app/api/client-logs/route.ts:1-6, src/app/api/cron/cleanup-blobs/route.ts:1-9) and can also trip common lint rules that require imports to come first. Move the dynamic export below the last import so the import block stays contiguous.
|
|
||
| import { db } from "~/server/db"; | ||
|
|
||
| export const dynamic = "force-dynamic"; |
There was a problem hiding this comment.
If the goal is to prevent next build from failing when POSTGRES_URL is unset, note that this route still imports db at module scope (import { db } from "~/server/db";). src/server/db/index.ts:5-11 throws during module initialization when POSTGRES_URL is missing, so any build-time code path that ends up loading this route module will still crash regardless of the dynamic export. Consider mirroring src/app/api/test-setup/route.ts:12-15 by lazily importing db inside POST() (after the NODE_ENV guard) so the module can be safely parsed/loaded during build without requiring database configuration.
|
@copilot apply changes based on the comments in this thread |
…route
- Remove top-level `import { db }` that caused module-scope throw at
build time when POSTGRES_URL is unset
- Move `export const dynamic = \"force-dynamic\"` below all imports
to match pattern in sibling routes
- Add lazy `const { db } = await import(\"~/server/db\")` inside POST()
after the NODE_ENV guard, mirroring test-setup/route.ts pattern
Agent-Logs-Url: https://github.com/timothyfroehlich/PinPoint/sessions/96b60e85-a254-4358-9b26-07935640f4c4
Applied both fixes in b963910:
|
/api/test-data/cleanupimporteddbat module scope, causingnext buildto throw whenPOSTGRES_URLis unset. Thedynamic = "force-dynamic"export added in a prior commit prevented static rendering but didn't stop the module from being loaded —src/server/db/index.tsthrows during initialization regardless.Changes
dbimport — replaced top-levelimport { db }withconst { db } = await import("~/server/db")insidePOST(), placed after theNODE_ENVproduction guard. Mirrors the pattern intest-setup/route.ts.export const dynamic = "force-dynamic"below the import block to match sibling routes (client-logs,cron/cleanup-blobs) and avoid lint violations.