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 devfirst to generate migration files. Runningzyvor migrate --applybefore 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 NULLconstraints for non-nullable fieldsPRIMARY KEYandSERIALfor autoincrement fieldsUNIQUEconstraints and indexes- Composite unique indexes
- Foreign keys with
ON DELETEactions - Supported default values
Type Mapping
| Schema Type | PostgreSQL Type |
|---|---|
Int | INTEGER |
Int @default(autoincrement()) | SERIAL |
String | TEXT |
Boolean | BOOLEAN |
Float | DOUBLE PRECISION |
DateTime | TIMESTAMP |
Decimal | NUMERIC |
Json | JSONB |
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 Change | Generated SQL |
|---|---|
| Field added | ALTER TABLE ... ADD COLUMN |
| Field removed | ALTER TABLE ... DROP COLUMN IF EXISTS |
| Model removed | DROP TABLE IF EXISTS ... CASCADE |
| Model added | Noted 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:
- Calculates a hash of the SQL contents.
- Looks up the migration filename in
_zyvor_migrations. - Same checksum: skips the migration.
- Different checksum: returns a hard error showing both hashes.
- 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 Capability | Current Workaround |
|---|---|
ALTER COLUMN TYPE | Write a forward migration manually |
| Rename detection | A rename is treated as drop + add, which can cause data loss. Hand-write the migration |
| Index and constraint diffs | Handle them manually |
Rollback, migrate reset, and migrate status | Write a forward migration |
Pivot tables from through: | Declare the pivot table as a model |
Composite @@id | Use a surrogate id with @@unique([...]) |
⚠️ Renaming a field is potentially destructive.
For example, renaming
biotobiographyin your schema generates:
DROP COLUMN bioADD COLUMN biographyThe existing data in
biois lost. Until rename detection is supported, review every generated migration diff before applying it.