Typesafe Migration Guard ๐Ÿ›ก๏ธ

September 17, 2026 ยท View on GitHub

CI Pipeline AI Engine ORM Database Latency License

"Built for production workflows, not just toy demos."

Typesafe Migration Guard is an automated, deterministic safety gatekeeper that intercepts database migrations in continuous integration (CI/CD) pipelines and production deployment runners. Powered by the TypeSafe AI SDK and its machine-native Jev System One model, it evaluates DDL statements in real-time, blocking destructive operations (such as DROP TABLE, DROP COLUMN, and unindexed truncation) with an HTTP 403 Forbidden status code before any migration touches your live Supabase PostgreSQL database.


๐Ÿ“บ Video Walkthrough & Demo

Watch the demonstration of Typesafe Migration Guard intercepting database migrations and blocking destructive operations in real time:

Typesafe Migration Guard Walkthrough Video
โ–ถ๏ธ Click the preview above to watch the walkthrough on YouTube

The Problem: Why Traditional Tooling Fails in Production

  1. Static Regex / AST Linters: Prone to false negatives and brittle syntax edge cases. A subtle nested statement, transaction block, or multi-statement migration can easily bypass naive regex pattern matchers.
  2. Generative LLMs (GPT-4, Claude, Gemini): Traditional conversational LLMs suffer from high latency (2,000ms โ€“ 6,000ms), non-deterministic conversational text generation, and token streaming overhead. Gating a fast CI pipeline with a slow conversational LLM introduces unacceptable friction and flaky parses.
  3. TypeSafe Jev (The System One Solution): Jev is a machine-native, structured-decision model trained with RLCD (Reinforcement Learning for Calibrated Decisions). It executes single-pass parallel classifications, outputting strictly typed decisions (SAFE vs DANGER) with calibrated probabilities in sub-second latency (~150โ€“400ms).

Architecture & Interception Flow

sequenceDiagram
    autonumber
    actor Dev as Developer / Git Push
    participant CI as GitHub Actions Pipeline
    participant MG as Migration Guard API (/api/migration-guard)
    participant Jev as TypeSafe AI (Jev System One)
    participant Supa as Supabase PostgreSQL

    Dev->>CI: Push Migration commit
    CI->>CI: Build & Run Pre-Flight Migration Scan
    CI->>MG: POST /api/migration-guard (sqlCommand, env: "production")
    MG->>Jev: client.systemOne(choice: SAFE | DANGER)
    Note over Jev: Machine-native decision pass (~250ms)
    Jev-->>MG: { choice: "DANGER", confidence: 1.0, probabilities: { ... } }
    
    alt Destructive DDL Detected in Production
        MG-->>CI: HTTP 403 Forbidden [BLOCKED]
        Note over CI: Pipeline HALTED. Deployment Aborted. Alert Sent.
    else Safe DDL Verified
        MG-->>CI: HTTP 200 OK [ALLOWED]
        CI->>Supa: prisma migrate deploy
        Note over Supa: Migration executed safely.
    end

Key Features

  • โšก Sub-Second Real-Time Interception: Average evaluation latency of ~250ms, ensuring that CI/CD pipelines remain blazing fast.
  • ๐Ÿšซ Strict HTTP 403 Production Gate: Any destructive DDL targeting a production environment immediately fails the HTTP request with status 403 Forbidden, halting automated deployment scripts.
  • ๐ŸŽฏ Calibrated Probabilistic Confidence: Jev returns mathematical confidence scores (e.g. 100%) and probability distributions for audit trails.
  • ๐Ÿ—„๏ธ Supabase PostgreSQL Ready: Configured with dual connection strings (DATABASE_URL for pooled queries via Supavisor, DIRECT_URL for schema migrations).
  • ๐Ÿ–ฅ๏ธ Interactive Operations Dashboard: Next.js App Router visual console to inspect migrations, preview verdicts, and test deployment scenarios with one click.
  • ๐Ÿ”„ AWS EC2 Zero-Downtime Deployment: Production bash script utilizing PM2 cluster reload, automated pre-flight checks, and rollback hooks.

Installation & Setup

Prerequisites

  • Node.js: v20.x or v22.x
  • TypeSafe AI API Key
  • Supabase PostgreSQL Database (or local Postgres instance)

1. Clone & Install Dependencies

git clone https://github.com/your-org/typesafe-migration-guard.git
cd typesafe-migration-guard
npm install

2. Environment Configuration

Copy .env.example to .env:

cp .env.example .env

Configure your credentials in .env:

# TypeSafe AI Credentials
TYPESAFE_API_KEY="your-typesafe-api-key"
TYPESAFE_MODEL="jev-latest"

# Supabase PostgreSQL Configuration
DIRECT_URL="postgresql://postgres.your-project-ref:your-password@aws-0-us-east-1.pooler.supabase.com:5432/postgres"
DATABASE_URL="postgresql://postgres.your-project-ref:your-password@aws-0-us-east-1.pooler.supabase.com:6543/postgres?pgbouncer=true"

# Server Configuration
PORT=3005
MIGRATION_GUARD_URL="http://localhost:3005/api/migration-guard"

3. Generate Prisma Artifacts

npm run prisma:generate

4. Run Development Server

npm run dev

Open http://localhost:3005 in your browser to access the Interactive Guardian Dashboard.


API Reference: The Guardian Endpoint

POST /api/migration-guard

Evaluates a SQL statement against environment policy.

Request Headers

Content-Type: application/json

Request Payload

{
  "sqlCommand": "DROP TABLE IF EXISTS users CASCADE;",
  "environment": "production"
}

Response: Destructive in Production (HTTP 403 Forbidden)

{
  "status": "BLOCKED",
  "verdict": "DANGER",
  "environment": "production",
  "blocked": true,
  "latencyMs": 261.03,
  "model": "jev-1.13.0",
  "confidence": 1.0,
  "probabilities": {
    "SAFE": 0.0,
    "DANGER": 1.0
  },
  "message": "CRITICAL: Destructive migration blocked by TypeSafe Migration Guard in production environment.",
  "sqlCommand": "DROP TABLE IF EXISTS users CASCADE;",
  "timestamp": "2026-09-17T05:25:28.993Z"
}

Response: Safe in Production (HTTP 200 OK)

{
  "status": "ALLOWED",
  "verdict": "SAFE",
  "environment": "production",
  "blocked": false,
  "latencyMs": 265.57,
  "model": "jev-1.13.0",
  "confidence": 1.0,
  "probabilities": {
    "SAFE": 1.0,
    "DANGER": 0.0
  },
  "message": "SUCCESS: Migration verified safe by TypeSafe Migration Guard for production execution.",
  "sqlCommand": "CREATE TABLE users (id TEXT PRIMARY KEY, email TEXT UNIQUE);",
  "timestamp": "2026-09-17T05:25:25.257Z"
}

CI/CD Pipeline Integration

A complete GitHub Actions workflow is included at .github/workflows/migration-guard-ci.yml.

Workflow Mechanics:

  1. Every Pull Request and commit targeting main runs the migration-safety-gate job.
  2. The gate spins up the Next.js service and runs npm run guard:check.
  3. If any migration returns HTTP 403 (DANGER), the CI step immediately terminates with exit code 1.
  4. The deployment step (deploy-production) is conditioned on needs: migration-safety-gate, making it impossible to deploy destructive DDL.

Run the CI migration verification locally anytime:

# Check all pending migrations in prisma/migrations
npm run guard:check

# Or check a specific migration
npm run guard:check prisma/migrations/20260917000001_create_users_table

AWS EC2 PM2 Deployment Guide

The deployment script scripts/deploy-ec2.sh implements zero-downtime cluster reloads with built-in safety rollback:

# Make script executable
chmod +x ./scripts/deploy-ec2.sh

# Run dry-run simulation mode (safe for testing)
./scripts/deploy-ec2.sh --simulate

# Run live production deployment
ENVIRONMENT=production ./scripts/deploy-ec2.sh

Execution Lifecycle:

  1. Pre-flight Check: Executes npm run guard:check. If destructive DDL is detected, aborts immediately before any database or file system changes occur.
  2. Git Sync & Dependency Update: Pulls latest verified commit from origin/main and runs npm ci --only=production.
  3. Prisma Migrate: Executes npx prisma migrate deploy only after Jev clearance.
  4. PM2 Cluster Reload: Executes pm2 reload typesafe-migration-guard --update-env for seamless zero-downtime handover.
  5. Health Check & Auto-Rollback: Polls /api/migration-guard. If unhealthy, rolls back to previous commit and reloads PM2.

Mock Migration Test Suite

Included in prisma/migrations:

  • 20260917000001_create_users_table/migration.sql: Additive schema definition (CREATE TABLE "users"). Verified: SAFE (HTTP 200).
  • 20260917000002_drop_users_table/migration.sql: Destructive schema deprecation (DROP TABLE "users" CASCADE). Verified: DANGER (HTTP 403).

Low-Latency Benchmark

Assessment EngineLatencyDeterministic SchemaHallucination RiskCI-Gating Suitability
TypeSafe Jev (System One)~240ms โ€“ 300msStrict Typed Enum0%Production-Ready
Regex Linters~10msBooleanHigh (Bypassable)Unreliable
Conversational LLMs (GPT-4 / Claude)~3,500ms โ€“ 6,000msFree-form Text / JSONHighFlaky / Slows CI

Summary of Active Protection Against Human Error

Human Mistake / ScenariosWithout Migration GuardWith Typesafe Migration Guard
Developer runs prisma migrate dev with accidental column dropDeployed to production; customer records lostIntercepted in CI; blocked with HTTP 403
Merge conflict resolution includes duplicate or cascading DROPDatabase lock / table deletion during deploymentIntercepted in pre-flight runner; pipeline halted
Staging test migration mistakenly pushed to production branchApplied directly to production databaseEnvironment check enforces strict 403 blocking

License

MIT ยฉ Typesafe Migration Guard Contributors.