MasterRecord

Migrations

Evolve your schema from code — diffed, versioned, reversible.

MasterRecord migrations compare your entity definitions against a saved snapshot, generate the SQL to reconcile them, and apply it in order. You get version-controlled schema changes with up/down support across SQLite, MySQL, and PostgreSQL.

Name the context file after the class
The CLI resolves a context by file name — enable-migrations AppContext looks for *AppContext.js. Keep your context in app/models/AppContext.js.

The workflow#

terminal
$ # 1. one-time: create the snapshot
$ masterrecord enable-migrations AppContext
$
$ # 2. generate a migration from current models
$ masterrecord add-migration InitialCreate AppContext
$
$ # 3. apply pending migrations
$ masterrecord update-database AppContext
$
$ # roll back the most recent migration
$ masterrecord update-database-down AppContext

Inside a Master app, use the friendlier wrapper:

terminal
$ master db enable
$ master db new InitialCreate
$ master db migrate
$ master db rollback

What a migration looks like#

Generated migrations are plain ESM with up() and down() methods built on the schema API. Review and edit them before applying:

1700000000000_InitialCreate_migration.js
import masterrecord from 'masterrecord';

class InitialCreate extends masterrecord.schema {
  constructor(context) { super(context); }

  async up(table) {
    await this.init(table);
    await this.createTable(table.User);
    await this.createTable(table.Post);
  }

  async down(table) {
    await this.init(table);
    await this.dropTable(table.Post);
    await this.dropTable(table.User);
  }
}

export default InitialCreate;

Evolving the schema#

Change a model — add a field, an index, or a relationship — then generate another migration. MasterRecord diffs against the snapshot and emits just the delta (add column, create index, etc.).

terminal
$ master db new AddViewsToPost
$ master db migrate

Migration tracking#

Applied migrations are recorded in a _masterrecord_migrations table, so update-databaseruns only what’s pending and is safe to re-run (idempotent).

CLI command reference#

master dbmasterrecordDoes
enableenable-migrationsCreate the initial snapshot.
new <Name>add-migration <Name>Diff models → migration file.
migrateupdate-databaseApply pending migrations.
rollbackupdate-database-downRevert the latest migration.
listget-migrationsList migration files.
ensureensure-databaseCreate the database if missing (MySQL).