Back to Rules

🧠 Cursor Rule — Automate Schema Updates

Official
CursorFull-Stack Project Automation
cursordatabaseormmigrationsschema

Summary

Schema changes should be automated, validated, and consistently applied across API, database, UI, and type layers. Cursor can generate migrations, update models, regenerate validators, and detect drift—preventing runtime failures caused by inconsistent schemas.

Objectives

  • Keep database schemas, TypeScript types, and validation schemas in sync
  • Prevent drift between API contracts and client code
  • Automate generation of migrations and update helpers
  • Reduce breakage during refactors and iterative development

Principles

1. Schema is the source of truth—everything else generates from it.

2. Drift between DB, backend, and frontend is the root cause of many runtime bugs.

3. Migrations must be idempotent, reversible, and safe by default.

4. Automatic regeneration of types and validators reduces mistakes and ensures consistency.

Implementation Pattern

Step 1 — Define or modify the schema

Start with your model definition or schema file (Prisma, Drizzle, Zod, JSON schema, SQL DDL). Use Cursor to:

  • Add new fields
  • Remove deprecated fields
  • Modify constraints (nullable, enum, unique, default)

Step 2 — Generate migrations automatically

Cursor should generate:

  • SQL migrations
  • Rollback scripts
  • Safeguards for destructive operations
  • Comments explaining intent and impact

Migration example:

'ALTER TABLE users ADD COLUMN timezone TEXT DEFAULT "UTC";'

Step 3 — Regenerate TypeScript types + validators

Cursor updates:

  • Domain model types
  • API request/response types
  • Validation schemas (Zod/Yup/Joi)
  • Form validators

This ensures correctness from DB → API → UI.

Step 4 — Update CRUD operations, services, and API routes

Cursor applies schema changes across your backend:

  • Update queries and insert logic
  • Modify select fields
  • Update filtering and pagination rules

Step 5 — Update UI bindings and forms

Fields added/removed in the schema should be reflected in:

  • Forms
  • Components
  • Table views / list views
  • Client validation rules

Step 6 — Validate changes through execution

Run:

  • CRUD operations
  • Integration tests
  • Data migration scripts
  • Seed scripts

Cursor ensures behavior hasn’t regressed.

Bug Prevention Examples

  • **DB schema mismatch causing runtime crash**

Backend expects a field that the DB schema no longer has → crash.

Automated typing + migrations ensure both stay consistent.

  • **Frontend sends fields the API no longer accepts**

Client-side forms become outdated → API rejects request.

Shared validators prevent this.

  • **Silent data corruption from missing constraints**

A field becomes nullable accidentally → inconsistent state.

Automated constraint updates preserve integrity.

  • **Production outage from manual migration error**

Human-written SQL misses an index or inserts invalid defaults.

Generated migrations prevent unsafe operations.

Anti-Pattern (Before)

Schema updated in DB but not in code:

'ALTER TABLE orders ADD COLUMN discount_code TEXT;

-- developers forget to update API types and UI'

Problems: client breaks, API throws undefined errors, inconsistent state.

Recommended Pattern (After)

Use Cursor to update schema AND regenerate everything:

'model Order {

id String @id

total Float

discountCode String? // new optional field

}'

Cursor then regenerates:

  • Migration
  • TypeScript types
  • Zod schema
  • Form validators
  • CRUD logic

Benefits

  • Zero drift between layers
  • Faster development with fewer integration failures
  • Stronger correctness guarantees
  • Predictable migrations across environments

Best Practices

  • Always treat schema changes as a multi-layer update (DB → API → UI).
  • Have Cursor regenerate types and validators after each schema edit.
  • Review generated migrations before applying to production.
  • Keep schema files small and well-documented.
  • Use feature-flagged migrations for risky changes.

Cursor Prompts

"Update the User schema and regenerate migrations, types, and validators."

"Show all code changes required after adding a new field to the model."

"Detect schema drift between DB, server types, and UI."

"Generate forward + rollback migrations for this schema change."

Notes

  • Schema automation reduces 70–80% of integration bugs.
  • Always validate migration output in a staging database.
  • Let Cursor regenerate dependent code to prevent drift.
View Tool Page