AutoMigrate

GORM-style automatic schema migration. Creates tables, adds missing columns and indexes. Never deletes or changes existing data.

How It Works

AutoMigrate inspects your model definitions and the database schema, then:

  • Creates table if it doesn't exist
  • Adds missing columns (never deletes or changes types)
  • Creates missing indexes for unique and index columns
  • Creates history table if model has versioned: true

This is safe for development and production. You can also use migrate:auto as a CLI command.

Primary Keys

The column you declare as id becomes the table's primary key, and its declared type is honoured:

  • INTEGER or BIGINT gets the database's auto-increment key (AUTOINCREMENT, AUTO_INCREMENT or SERIAL), and the database assigns it.
  • Anything else — STRING, UUID — is created as a NOT NULL PRIMARY KEY of that type, for you to supply. This is the pattern generate:model scaffolds.

A string id must stay a string

A UUID column quietly created as an integer would reject every create({ id: generateUUID() }) with datatype mismatch, so the declared type wins. Since AutoMigrate only ever adds, a table created before this rule existed keeps its old key — drop it (or write a migration) to pick up the new definition.

Usage

app.ts
import { Stabilize } from "stabilize-orm";
import { User, Post, Comment } from "./models";
// Single model
await orm.autoMigrate(User);
// Multiple models (like GORM)
await orm.autoMigrate([User, Post, Comment]);

CLI Command

terminal
❯stabilize-cli migrate:auto
Auto-migrating models...
✔ Auto-migrate complete: 3 model(s) processed.

Scans models/ directory and auto-migrates all found models.

AutoMigrate vs Migrations

You don't need to disable migrations.

AutoMigrate and manual migrations can coexist. They serve different purposes:

FeatureAutoMigrateMigrations
Use caseDevelopment, prototypingProduction, version control
DirectionForward only (add columns)Up and down (rollback)
TrackedNo tracking tablestabilize_migrations table
RollbackNot supportedFull rollback support
Safe for prodYes (never deletes)Yes (reviewed SQL)

Recommended approach:

  • Use autoMigrate during development for rapid iteration
  • Use generate:migration + migrate for production deployments
  • Both work together — AutoMigrate adds columns, migrations track history
  • AutoMigrate is safe — it never deletes data or drops columns

What AutoMigrate Does NOT Do

  • Does NOT delete columns
  • Does NOT change column types
  • Does NOT rename columns
  • Does NOT drop tables
  • Does NOT create a rollback mechanism

For destructive changes, use manual migrations with explicit up and down SQL.

Example: Development Workflow

workflow
❯# 1. Edit your model
❯# 2. Auto-migrate picks up changes
❯stabilize-cli migrate:auto
✔ Auto-migrate complete: 1 model(s) processed.
❯# 3. Ready for production? Generate migration
❯stabilize-cli generate:migration User
✔ Migration generated: migrations/20260402170000_create_user_table.json