Skip to content

fix(db): reorder 0012 timestamp after 0011 so drizzle-kit applies it - #687

Merged
junhoyeo merged 2 commits into
mainfrom
fix/migration-journal-0012-ordering
Jun 8, 2026
Merged

junhoyeo merged 2 commits into
mainfrom
fix/migration-journal-0012-ordering

Conversation

@junhoyeo

@junhoyeo junhoyeo commented Jun 8, 2026

Copy link
Copy Markdown
Owner

Why prod was 500'ing

After merging #675, every /api/auth/session and /leaderboard SSR request started failing with column sessions.token_hash does not exist. Migration 0012_hash_browser_session_tokens.sql (from #625) renames sessions.token → token_hash, but it never ran on production even though drizzle-kit migrate claimed success.

Root cause

The 2026-05-25 schema audit hand-rolled when=1780000000000 (0010) and 1780086400000 (0011). When 0012 was generated by drizzle-kit generate later, it got the real-clock value when=1779948193133 (Feb 24, 2026), which is earlier than the hand-rolled 0010/0011 timestamps (Feb 25).

drizzle-kit migrate sorts the journal by when before applying. On any DB where 0010/0011 had already been applied, drizzle considered the prefix-through-0011 already-applied and silently skipped 0012. Then 0013 (mcp_servers) applied cleanly because its when is the latest. Result: prod's sessions table never got renamed.

Fix

Bump 0012.when to 1780150000000 so it sits between 0011.when=1780086400000 and 0013.when=1780172800000, restoring monotonic ordering.

Already done on production

  • Migration 0012 SQL applied by hand against prod (sessions table now has token_hash)
  • drizzle.__drizzle_migrations row inserted for 0012 with the matching SHA256 (782891d5...) and the new when=1780150000000, so drizzle-kit migrate will not try to re-apply

Test plan

  • /api/auth/session → 200 on prod (was 500)
  • /leaderboard SSR → renders 50 ranked users, no console errors (was Server Components render error)
  • On next preview deploy: confirm drizzle-kit migrate reports zero migrations to apply (the matching journal row should already be in __drizzle_migrations)

Summary by cubic

Reordered migration 0012 by bumping its when so drizzle-kit applies it after 0011 and before 0013, fixing the sessions.token → token_hash rename and the 500s. Added a docs rule: never edit applied migration SQL; use a sidecar note to avoid hash mismatches that make drizzle-kit try to re-apply.

Written for commit 38de12e. Summary will update on new commits.

Review in cubic

@vercel

vercel Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
tokscale Ignored Ignored Preview Jun 8, 2026 9:59am

Request Review

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 1 file

Re-trigger cubic

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8dd4391ea5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

"idx": 12,
"version": "7",
"when": 1779948193133,
"when": 1780150000000,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Regenerate migration metadata instead of hand-editing

This directly changes a drizzle/meta/_journal.json timestamp, but the repo-level AGENTS.md Migration journal hygiene rule says to never hand-edit journal timestamps/sequence numbers and to use drizzle-kit generate to claim or repair migration slots; it also calls the 0010/0011 hand-edited timestamps a one-time historical exception. For this migration-ordering fix, please regenerate/repair the migration metadata through the Drizzle workflow rather than committing another manual when edit, otherwise future branches can inherit metadata that bypasses the repo's conflict-avoidance process.

Useful? React with 👍 / 👎.

junhoyeo added 2 commits June 8, 2026 18:59
The 2026-05-25 schema audit hand-rolled when=1780000000000 (0010) and
1780086400000 (0011). 0012 was generated normally by drizzle-kit
generate and got when=1779948193133 (Feb 24), which is EARLIER than
the hand-rolled 0010/0011 timestamps (Feb 25).

drizzle-kit migrate sorts by when, so 0012 was placed before 0010 in
the apply sequence. On any DB where 0010/0011 were already present,
drizzle considered the prefix-through-0011 already-applied and silently
skipped 0012. Result: production sessions table never got the
token → token_hash rename and every /api/auth/session request 500'd
with 'column sessions.token_hash does not exist'.

Fix: bump 0012's when to 1780150000000 (between 0011's 1780086400000
and 0013's 1780172800000), restoring the intended monotonic ordering.
The matching __drizzle_migrations row on prod has been inserted with
the same when value and the file's SHA256 hash so drizzle-kit migrate
will not try to re-apply.

Confidence: high
Scope-risk: narrow
Directive: Never hand-roll _journal.json timestamps (the rule is
  already in AGENTS.md 'Migration journal hygiene'). The hand-rolled
  0010/0011 timestamps are now boxed in by 0012/0013; do not introduce
  new hand-rolled values, and check ordering whenever editing the
  journal.
drizzle stores SHA256(file content) in drizzle.__drizzle_migrations on
first apply and compares against the local file hash on every subsequent
drizzle-kit migrate run. A comment-only edit changes the SHA256 and makes
drizzle attempt to re-apply, which fails on non-idempotent DDL.

This bit prod on 2026-06-08: the audit batch added a -- IMPORTANT lock-
window comment to 0011_drop_dead_columns.sql AFTER it had been deployed.
The local hash diverged from the prod __drizzle_migrations row, leaving
drizzle in a state where the next migrate run would have tried to drop
columns that no longer exist.

Recovered with a one-off UPDATE on the prod journal row's hash. The rule
in this commit prevents the next instance.

Confidence: high
Scope-risk: narrow
Directive: For after-the-fact migration documentation (lock windows,
  rollback notes), use a sidecar 0NNN_<tag>.md or comments in schema.ts
  / AGENTS.md — never inside the applied .sql.
@junhoyeo
junhoyeo force-pushed the fix/migration-journal-0012-ordering branch from 20f5ac8 to 38de12e Compare June 8, 2026 09:59
@junhoyeo
junhoyeo merged commit 4590ecb into main Jun 8, 2026
5 checks passed
@junhoyeo
junhoyeo deleted the fix/migration-journal-0012-ordering branch June 8, 2026 10:00
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