Skip to Content
Migrations

Migrations

KalamDB projects store schema history under kalam/migrations/ (configured in [migrations].dir). The CLI diffs schema.sql against a local baseline, maintains a working _draft.sql, and tracks applied migrations on the server per namespace.

Migration files

File patternMeaning
YYYYMMDDHHMMSS_<name>.sqlCommitted, ordered migration with -- UP and -- DOWN sections
_draft.sqlWorking draft generated from schema changes not yet sealed

Draft files are not applied to production deployments until sealed into a numbered migration.

Typical development flow

BASH
# 1. Edit schema.sqlvim schema.sql # 2. Let kalam dev detect changes (or run pipeline manually)kalam dev # 3. When prompted, apply the draft — or seal and migrate explicitly:kalam migration sealkalam db migrate # 4. Check statekalam migration status

During kalam dev, edits to schema.sql update _draft.sql automatically. Numbered migrations apply on startup and after each watched change; the draft waits for your confirmation unless you use kalam dev --force (which skips the prompt but still leaves sealing/applying the draft to the Apply path or explicit kalam migration seal).

Commands

kalam migration create <name>

Create a numbered migration immediately from the current diff (without going through the draft file):

BASH
kalam migration create add_messages_table# creates kalam/migrations/20260611120000_add_messages_table.sql

kalam migration status

Report migration state for the resolved environment:

BASH
kalam migration statuskalam migration status --env staging

Groups: Applied, Pending, Failed, Applying.

kalam migration seal

Promote _draft.sql into the next numbered migration file (rename only — does not apply to the database):

BASH
kalam migration seal

Run kalam db migrate afterward to apply the sealed migration.

kalam db migrate

Apply all pending numbered migrations to the linked database:

BASH
kalam db migratekalam db migrate --env prod
  • Does not include _draft.sql unless you sealed it first
  • Ensures the target namespace exists before applying
  • Validates checksums for migrations already marked applied

kalam migration retry <id>

Re-queue a failed migration for another apply attempt:

BASH
kalam migration retry 20260610120000_add_users

kalam migration repair <id> --mark-applied

Manually mark a migration as applied on the server (use when you’ve fixed state out of band):

BASH
kalam migration repair 20260610120000_add_users --mark-applied

How drafting works

  1. Baselinekalam/.schema-baseline.sql mirrors the last successfully applied schema
  2. Diff — the CLI compares schema.sql to the baseline and generates UP/DOWN SQL
  3. Draft — changes land in kalam/migrations/_draft.sql
  4. Sealkalam migration seal renames the draft to timestamp_name.sql
  5. Applykalam db migrate or the dev pipeline executes UP statements and records checksums on the server
  6. Baseline update — after apply, baseline is refreshed from schema.sql

If the diff is empty, a stale _draft.sql is removed automatically.

Dev vs deploy behavior

ContextDraft auto-applyAuto-create drafts
kalam devPrompted (or --force)Yes, when migrations.auto_create = true
kalam db migrateNo — numbered files onlyNo
kalam deployRefused — blocks if uncommitted schema would require new migrationsNo

Production deploy applies committed migration history only. Generate and seal migrations locally before kalam deploy --env prod.

Stuck or failed migrations

During apply, the CLI may prompt when a migration was interrupted or failed previously:

  • Retry — attempt apply again
  • Skip / mark applied — for failed migrations when you’ve verified state manually
  • Abort — stop the apply run

Pass --force on kalam dev to retry automatically in some stuck states.

For manual recovery:

BASH
kalam migration statuskalam migration retry <migration_id># orkalam migration repair <migration_id> --mark-applied

Global flags

All migration commands accept:

FlagDescription
--project-dir <PATH>Project root with kalam.toml
--env <NAME>Target environment

Migration file format

SQL
-- Migration: add_messages_table-- Created: 2026-06-11T12:00:00Z -- UPCREATE TABLE app.messages (  id BIGINT PRIMARY KEY,  body TEXT); -- DOWNDROP TABLE app.messages;
Last updated on