Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Migrations

Migrations are generated from your schema, tracked in the database, and applied in order. You never hand-write SQL for a schema change.

Where They Come From

zyvor dev             # Generates 0001_initial.sql, then detects changes on every save
zyvor migrate --apply # Applies migrations that haven't run yet

Order matters. Run zyvor dev first to generate migration files. Running zyvor migrate --apply before that results in:

No migration files found in .zyvor/migrations/

Initial Migration

0001_initial.sql contains the following for every model:

  • CREATE TABLE IF NOT EXISTS — idempotent table creation
  • Columns with mapped names and quoted identifiers
  • NOT NULL constraints for non-nullable fields
  • PRIMARY KEY and SERIAL for autoincrement fields
  • UNIQUE constraints and indexes
  • Composite unique indexes
  • Foreign keys with ON DELETE actions
  • Supported default values

Type Mapping

Schema TypePostgreSQL Type
IntINTEGER
Int @default(autoincrement())SERIAL
StringTEXT
BooleanBOOLEAN
FloatDOUBLE PRECISION
DateTimeTIMESTAMP
DecimalNUMERIC
JsonJSONB

Incremental Migrations

zyvor dev maintains a serialized schema snapshot at:

.zyvor/migrations/snapshot.json

Whenever you save your schema, Zyvor compares it with the previous snapshot and generates a migration file if it detects changes.

Example output:

🔄 Schema change detected! Generated: 0002_schema_update.sql

Migration Numbering

Migration numbering is automatic. Zyvor scans the existing .sql files, finds the highest migration number, and increments it.

Supported Schema Changes

Schema ChangeGenerated SQL
Field addedALTER TABLE ... ADD COLUMN
Field removedALTER TABLE ... DROP COLUMN IF EXISTS
Model removedDROP TABLE IF EXISTS ... CASCADE
Model addedNoted in the diff; full CREATE TABLE is included in the regenerated initial migration

An unchanged schema produces no diff file, avoiding unnecessary migration files.

Migration Tracking

Running zyvor migrate --apply creates and maintains the _zyvor_migrations table:

CREATE TABLE IF NOT EXISTS _zyvor_migrations (
    id SERIAL PRIMARY KEY,
    name TEXT NOT NULL UNIQUE,
    checksum TEXT NOT NULL,
    applied_at TIMESTAMP NOT NULL DEFAULT NOW()
);

For each .sql file, Zyvor processes migrations in filename order:

  1. Calculates a hash of the SQL contents.
  2. Looks up the migration filename in _zyvor_migrations.
  3. Same checksum: skips the migration.
  4. Different checksum: returns a hard error showing both hashes.
  5. Not recorded: executes the migration and then records it.

Example output:

⏭️  0001_initial.sql already applied. Skipping.
📝 Applying: 0002_schema_update.sql
✅ 0002_schema_update.sql applied successfully!

What This Means

  • Re-running is safe: If the schema and migration files haven’t changed, Zyvor skips already-applied migrations.
  • Applied migration edits are detected: Changing an already-applied migration produces a drift error instead of silently allowing the database history to disagree with the migration file.

Automatic Database Creation

If the target database doesn’t exist, Zyvor connects to the PostgreSQL postgres maintenance database, checks pg_database, and creates the target database.

Example output:

📦 Creating database 'my_app'...
✅ Database 'my_app' created!

You don’t need to run CREATE DATABASE manually.

Not Supported Yet

The following migration capabilities are not currently supported. Use the listed workarounds when needed.

Missing CapabilityCurrent Workaround
ALTER COLUMN TYPEWrite a forward migration manually
Rename detectionA rename is treated as drop + add, which can cause data loss. Hand-write the migration
Index and constraint diffsHandle them manually
Rollback, migrate reset, and migrate statusWrite a forward migration
Pivot tables from through:Declare the pivot table as a model
Composite @@idUse a surrogate id with @@unique([...])

⚠️ Renaming a field is potentially destructive.

For example, renaming bio to biography in your schema generates:

  • DROP COLUMN bio
  • ADD COLUMN biography

The existing data in bio is lost. Until rename detection is supported, review every generated migration diff before applying it.