Skip to content

Refuse to build against a database that is behind the schema - #256

Merged
imshashank merged 3 commits into
mainfrom
fix/block-a-deploy-the-database-cannot-serve
Aug 9, 2026
Merged

imshashank merged 3 commits into
mainfrom
fix/block-a-deploy-the-database-cannot-serve

Conversation

@imshashank

@imshashank imshashank commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

Why

Three outages tonight, one shape. A pull request adds a column, ships, and the database it runs against has never heard of it.

db:check-drift reported both. Nothing ran it against the database that mattered. The CI job added in #250 proves the committed migrations are complete; it never looks at the database a deploy will actually serve.

So the loop still depended on someone remembering, and it failed twice in one evening.

What changed

The Vercel build runs the check first:

"build": "bun ../../packages/db/src/check-drift.ts --guard-deploy && bun --env-file=../../.env next build"

A build whose database cannot answer what the code asks for now fails, loudly, instead of deploying and returning 500 on every page. DATABASE_URL is configured for both Production and Preview, so both are covered.

It fails only when it can prove drift

This matters, or the guard becomes something people route around:

Situation Result
Database is behind fails, naming the missing tables and columns
Database matches passes
Database unreachable passes, says the schema went unchecked
No DATABASE_URL passes, says there was nothing to check

A transient network fault should not block a deploy, and being unable to check is not the same as knowing something is wrong. Without the flag the script keeps its old behaviour and exits 2 on a missing DATABASE_URL, so db:check-drift run by hand still tells you when you have misconfigured it.

Verified all four, then covered the three that are testable without a database in tests/check-drift-guard.test.ts. Those spawn the script in a temporary directory, because bun auto-loads .env from the working directory and a test run from the repo root silently picks up a real DATABASE_URL — which is exactly how my first version of these tests fooled me.

Also here

packages/db/catchup/workspace-deletion-catchup.sql for the two columns #246 shipped without: organization.deletion_requested_at and attachment.upload_expires_at. Both nullable timestamps, no indexes.

Verified by rebuilding a database to match production's exact drift, applying it twice, and running the query from the production stack trace.

Already applied to production, which is back up and reports no drift.

Verification

  • packages/db: 163 pass
  • lint, comment policy, typecheck: clean
  • production and the local dev database both report every table and column the schema declares

Greptile Summary

The PR prevents web builds from proceeding when the configured database is provably behind the declared schema.

  • Runs the drift guard and the Next.js build with the same repository-root environment.
  • Makes guarded checks permissive when no database is configured or the database is unreachable.
  • Adds an idempotent catchup script for workspace deletion and upload-expiry columns.
  • Adds tests for guarded missing and unreachable database behavior.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
apps/web/package.json The guard and Next.js build now load the same repository-root environment, completing the prior fix.
packages/db/src/check-drift.ts Adds guarded deployment behavior that fails only for confirmed missing tables or columns.
packages/db/catchup/workspace-deletion-catchup.sql Adds an idempotent transactional catchup for the two missing nullable timestamp columns.
packages/db/tests/check-drift-guard.test.ts Covers guarded operation without a configured database, with an unreachable database, and unguarded missing configuration.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Web build starts] --> B[Load repository .env]
  B --> C[Run schema drift guard]
  C -->|Database is behind| D[Fail build and report missing schema]
  C -->|Schema matches| E[Run Next.js build]
  C -->|Missing or unreachable database| F[Report schema unchecked]
  F --> E
Loading

Reviews (3): Last reviewed commit: "chore(db): keep the guard flag to itself" | Re-trigger Greptile

Three outages tonight had one shape: a pull request adds a column, ships,
and the database it runs against has never heard of it. The docs tree,
then permanent workspace deletion, which took out every page, because
resolving a user's workspaces selects organization.deletion_requested_at.

db:check-drift already reported this, but nothing ran it against the
database that mattered. The CI job added earlier proves the committed
migrations are complete; it never looks at production.

The Vercel build now runs it first. A build whose database cannot answer
what the code asks for fails, loudly, instead of deploying and answering
500 on every page.

It fails only when it can prove drift. No DATABASE_URL, or a database it
cannot reach, is reported and allowed through, because a transient
network fault should not block a deploy, and being unable to check is not
the same as knowing something is wrong.

Adds packages/db/catchup/workspace-deletion-catchup.sql for the two
columns that are missing right now, verified against a database rebuilt
to match production.
@imshashank
imshashank requested a review from pulkitxm as a code owner August 9, 2026 05:41
@vercel

vercel Bot commented Aug 9, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
orbit Ready Ready Preview Aug 9, 2026 5:58am

Request Review

@github-actions github-actions Bot added tests Test coverage and test infrastructure area: web The Next.js app and its UI area: database Schema, migrations, queries, seed dependencies Dependency updates labels Aug 9, 2026
@coderabbitai

coderabbitai Bot commented Aug 9, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The PR adds a rerunnable database catch-up migration, introduces guarded drift-check behavior, tests the guard paths, and runs the guard before the web production build.

Changes

Database deployment guard

Layer / File(s) Summary
Schema catch-up migration
packages/db/catchup/workspace-deletion-catchup.sql
Adds transactional, idempotent columns for organization deletion requests and attachment upload expiration.
Guarded drift-check build flow
packages/db/src/check-drift.ts, packages/db/tests/check-drift-guard.test.ts, apps/web/package.json
Adds --guard-deploy handling, validates exit behavior for unavailable databases and schema drift, and runs the guard before the Next.js build.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant WebBuild
  participant CheckDrift
  participant Database
  WebBuild->>CheckDrift: run with --guard-deploy
  CheckDrift->>Database: inspect live schema
  Database-->>CheckDrift: return schema state or connection error
  CheckDrift-->>WebBuild: allow build or refuse on drift
Loading

Possibly related PRs

  • Noveum/orbit#241: Introduced the shared database drift-check implementation extended by this PR.

Suggested reviewers: pulkitxm

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly summarizes the main change: preventing builds against databases that are behind the schema.
Description check ✅ Passed The description directly explains the drift guard, deployment behavior, catch-up migration, tests, and verification results.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/block-a-deploy-the-database-cannot-serve

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@apps/web/package.json`:
- Line 8: Update the build script so the root .env file is loaded before running
packages/db/src/check-drift.ts, ensuring the drift guard receives DATABASE_URL
and other configuration. Preserve the existing --guard-deploy behavior and Next
build command while applying the same environment source to both steps.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 7b2cb212-f823-4635-86e2-df92addd5da9

📥 Commits

Reviewing files that changed from the base of the PR and between 9dbf7f9 and 1884d6b.

📒 Files selected for processing (4)
  • apps/web/package.json
  • packages/db/catchup/workspace-deletion-catchup.sql
  • packages/db/src/check-drift.ts
  • packages/db/tests/check-drift-guard.test.ts

Comment thread apps/web/package.json Resolved
Comment thread apps/web/package.json Resolved
The guard ran without the env file the build itself loads, so a local
build whose DATABASE_URL lives only in the repository root env passed the
check by finding no database, then built against that very database
unchecked. On Vercel the platform supplies the variable and the guard saw
it, which is exactly the kind of gap that only bites the person running
it by hand.

Both halves read ../../.env now. Verified against a database missing
almost everything: the guard exits 1 and names what is missing, where
before it exited 0 and said there was nothing to check.
Nothing outside the script reads it, and the build script names the flag
as a literal, so exporting it added surface without adding a contract.
@imshashank
imshashank merged commit 95c43eb into main Aug 9, 2026
13 checks passed
@imshashank
imshashank deleted the fix/block-a-deploy-the-database-cannot-serve branch August 9, 2026 06:05
@coderabbitai coderabbitai Bot mentioned this pull request Aug 14, 2026
8 tasks done

This branch was successfully deployed

1 active deployment
Preview — e7e6c07c Deployed Aug 9, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: database Schema, migrations, queries, seed area: web The Next.js app and its UI dependencies Dependency updates tests Test coverage and test infrastructure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant