Skip to content

ci: add migration drift check#219

Merged
FSM1 merged 5 commits into
mainfrom
chore/ci-migration-drift-check
Feb 28, 2026
Merged

ci: add migration drift check#219
FSM1 merged 5 commits into
mainfrom
chore/ci-migration-drift-check

Conversation

@FSM1

@FSM1 FSM1 commented Feb 28, 2026

Copy link
Copy Markdown
Owner

Summary

  • Adds a migration-check CI job that detects entity/migration drift by running typeorm migration:generate against a freshly-migrated Postgres database
  • If any entity changes lack a corresponding migration file, the job fails with the generated migration contents and a fix command
  • Added as a dependency of the build job (parallel with api-spec and test)

Prevents the class of bug from Phase 14 (PR #186) where a missing CREATE TABLE migration was invisible locally due to synchronize:true.

Test plan

  • migration-check job appears in CI and passes on current codebase (no drift)
  • To verify failure mode: locally add a dummy column to an entity, confirm migration:generate detects the drift

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Chores
    • Added an automated migration drift check to CI that validates database migrations on push and gates the build if structural drift is detected.
    • Updated project scripts for migration tooling to ensure consistent TypeScript configuration when running migration commands.

Adds a `migration-check` CI job that runs existing migrations against a
fresh Postgres, then uses `typeorm migration:generate` to detect if any
entity changes lack a corresponding migration. Prevents the exact class
of bug that hit in Phase 14 (PR #186) where a missing CREATE TABLE
migration was invisible locally due to synchronize:true.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Entire-Checkpoint: d092a7bc6505
@coderabbitai

coderabbitai Bot commented Feb 28, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@FSM1 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 12 minutes and 39 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 5a8f70d and a79de59.

📒 Files selected for processing (1)
  • .github/workflows/ci.yml

Walkthrough

Adds a Migration Drift Check job to CI that runs on pushes affecting src, boots PostgreSQL 16-alpine, runs migrations, and generates a drift-check migration; the Build job now depends on this check. Also adjusts the API package TypeORM CLI invocation to include --project tsconfig.json.

Changes

Cohort / File(s) Summary
CI Pipeline Configuration
.github/workflows/ci.yml
Added a new "Migration Drift Check" job that spins up PostgreSQL 16-alpine, installs deps, runs existing migrations, generates a drift-check migration, fails on structural drift (CREATE/DROP TABLE, ADD/DROP COLUMN, ALTER TYPE, RENAME COLUMN), and gates Build to depend on this job.
API package scripts
apps/api/package.json
Updated the typeorm npm script to invoke ts-node with an explicit project: changed to ts-node --project tsconfig.json -r tsconfig-paths/register ./node_modules/typeorm/cli.js.

Sequence Diagram(s)

sequenceDiagram
    participant GH as GitHub Actions Runner
    participant DB as Postgres 16-alpine (service)
    participant NPM as npm / scripts
    participant ORM as TypeORM CLI
    participant Repo as Repository (migrations/entities)

    GH->>DB: spin up Postgres service
    GH->>NPM: install dependencies
    GH->>NPM: run migration scripts (apply existing migrations)
    NPM->>ORM: invoke TypeORM CLI (uses updated ts-node project)
    ORM->>Repo: generate drift-check migration
    ORM->>GH: return generated migration (diff)
    GH->>GH: evaluate migration contents for structural changes
    alt structural drift detected
        GH->>GH: fail job and print migration contents
    else only cosmetic/no drift
        GH->>GH: report success
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'ci: add migration drift check' accurately describes the main change—adding a new CI job for migration drift detection, which is the primary focus of the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch chore/ci-migration-drift-check

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 and usage tips.

@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 the current code and only fix it if needed.

Inline comments:
In @.github/workflows/ci.yml:
- Around line 213-223: The drift-check currently silences all failures with "||
true" after running "pnpm typeorm migration:generate ...", which masks
datasource/config errors; change the step to capture the command's exit code and
its stdout/stderr (run "pnpm typeorm migration:generate
src/migrations/DriftCheck -d src/data-source.ts" without "|| true"), then
inspect the output and exit code: if the output contains the exact success
string "No changes in database schema were found" treat it as success, but if
the exit code is non-zero and the output does not include that success message,
print the full command output (and the generated files under
src/migrations/*DriftCheck*.ts if any) and exit non‑zero to fail the job so
datasource/config errors are surfaced.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 453f0f7 and 0a8dd66.

📒 Files selected for processing (2)
  • .github/workflows/ci.yml
  • .planning/todos/done/2026-02-27-ci-migration-drift-check.md

Comment thread .github/workflows/ci.yml Outdated
FSM1 and others added 2 commits February 28, 2026 16:41
… drift

Two issues found during CI validation:

1. TypeORM CLI fails with TS1240 (decorator metadata) when ts-node
   starts with a .js entry file and lazily initializes the TypeScript
   service without the correct tsconfig. Fix: add --project tsconfig.json
   to the typeorm script in package.json.

2. migration:generate always reports "drift" because our hand-written
   migrations use explicit constraint/index names (e.g., FK_refresh_tokens_user)
   while TypeORM expects auto-generated hash names. Fix: only flag structural
   drift (CREATE/DROP TABLE, ADD/DROP COLUMN, type changes) and ignore
   cosmetic constraint/index renames.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Entire-Checkpoint: 140578ed345c
The `|| true` suppressed all failures from `migration:generate`, masking
real datasource/config errors (TypeORM exits 1 for both "no changes" and
actual errors). Capture exit code and output separately, only treating
the "No changes" message as success.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Entire-Checkpoint: f0b63e72d3e4
@FSM1 FSM1 enabled auto-merge (squash) February 28, 2026 15:57

@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 the current code and only fix it if needed.

Inline comments:
In @.github/workflows/ci.yml:
- Around line 233-247: The early "exit 0" in the else branch after detecting "No
entity/migration drift detected." causes the subsequent STATUS/OUTPUT checks for
the `migration:generate` command to never run; remove or relocate that early
exit so the script always evaluates STATUS and OUTPUT after running
`migration:generate` (use the existing STATUS variable and the grep check on
OUTPUT to short-circuit only when the known "No changes in database schema were
found" message appears, otherwise let the code reach the block that logs
"::error::migration:generate failed unexpectedly." and exits 1). Ensure you only
short-circuit to success when the grep condition matches, and otherwise allow
the STATUS-based failure handling to execute.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0a8dd66 and 5a8f70d.

📒 Files selected for processing (2)
  • .github/workflows/ci.yml
  • apps/api/package.json

Comment thread .github/workflows/ci.yml Outdated
@FSM1 FSM1 disabled auto-merge February 28, 2026 16:02
FSM1 and others added 2 commits February 28, 2026 17:06
Move STATUS check before the drift file check. Previously, if
migration:generate failed without producing a file, the else branch
exited 0 and the STATUS check was unreachable — masking real errors
like DB connection failures or TypeORM bugs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Entire-Checkpoint: a3a82c38a9d8
@FSM1 FSM1 merged commit 1714dc8 into main Feb 28, 2026
17 checks passed
@FSM1 FSM1 deleted the chore/ci-migration-drift-check branch March 4, 2026 01:16
@coderabbitai coderabbitai Bot mentioned this pull request Mar 25, 2026
3 tasks
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