Skip to content

fix: lazy-import db in test-data cleanup route to prevent build-time crash#1161

Merged
timothyfroehlich merged 3 commits intomainfrom
worktree/transient-enchanting-giraffe
Apr 8, 2026
Merged

fix: lazy-import db in test-data cleanup route to prevent build-time crash#1161
timothyfroehlich merged 3 commits intomainfrom
worktree/transient-enchanting-giraffe

Conversation

@timothyfroehlich
Copy link
Copy Markdown
Owner

@timothyfroehlich timothyfroehlich commented Apr 8, 2026

/api/test-data/cleanup imported db at module scope, causing next build to throw when POSTGRES_URL is unset. The dynamic = "force-dynamic" export added in a prior commit prevented static rendering but didn't stop the module from being loaded — src/server/db/index.ts throws during initialization regardless.

Changes

  • Lazy db import — replaced top-level import { db } with const { db } = await import("~/server/db") inside POST(), placed after the NODE_ENV production guard. Mirrors the pattern in test-setup/route.ts.
  • Import order fix — moved export const dynamic = "force-dynamic" below the import block to match sibling routes (client-logs, cron/cleanup-blobs) and avoid lint violations.
export async function POST(request: Request): Promise<Response> {
  if (process.env.NODE_ENV === "production") {
    return NextResponse.json({ error: "Forbidden" }, { status: 403 });
  }

  // Lazy import to avoid loading db module during build
  const { db } = await import("~/server/db");
  // ...
}

…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>
Copilot AI review requested due to automatic review settings April 8, 2026 02:55
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 8, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
pin-point Ready Ready Preview, Comment Apr 8, 2026 3:25am

@supabase
Copy link
Copy Markdown

supabase bot commented Apr 8, 2026

Updates to Preview Branch (worktree/transient-enchanting-giraffe) ↗︎

Deployments Status Updated
Database Wed, 08 Apr 2026 03:24:40 UTC
Services Wed, 08 Apr 2026 03:24:40 UTC
APIs Wed, 08 Apr 2026 03:24:40 UTC

Tasks are run on every commit but only new migration files are pushed.
Close and reopen this PR if you want to apply changes from existing seed or migration files.

Tasks Status Updated
Configurations Wed, 08 Apr 2026 03:24:41 UTC
Migrations Wed, 08 Apr 2026 03:24:41 UTC
Seeding Wed, 08 Apr 2026 03:24:41 UTC
Edge Functions Wed, 08 Apr 2026 03:24:41 UTC

View logs for this Workflow Run ↗︎.
Learn more about Supabase for Git ↗︎.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 4 to 7
import { db } from "~/server/db";

export const dynamic = "force-dynamic";
import { log } from "~/lib/logger";
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +3 to +6

import { db } from "~/server/db";

export const dynamic = "force-dynamic";
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 uses AI. Check for mistakes.
@timothyfroehlich
Copy link
Copy Markdown
Owner Author

@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
Copilot AI review requested due to automatic review settings April 8, 2026 03:24
@timothyfroehlich timothyfroehlich review requested due to automatic review settings April 8, 2026 03:24
Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 8, 2026

@copilot apply changes based on the comments in this thread

Applied both fixes in b963910:

  1. Moved export const dynamic = "force-dynamic" below all imports so the import block is contiguous.
  2. Removed the top-level import { db } and replaced it with a lazy const { db } = await import("~/server/db") inside POST() after the NODE_ENV guard — mirroring the pattern in test-setup/route.ts so the module can be safely loaded during build without POSTGRES_URL.

Copilot AI changed the title fix: add force-dynamic to test-data cleanup route fix: lazy-import db in test-data cleanup route to prevent build-time crash Apr 8, 2026
@timothyfroehlich timothyfroehlich enabled auto-merge (squash) April 8, 2026 03:25
@timothyfroehlich timothyfroehlich merged commit e8e804c into main Apr 8, 2026
23 checks passed
@timothyfroehlich timothyfroehlich deleted the worktree/transient-enchanting-giraffe branch April 8, 2026 03:35
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.

3 participants