Skip to content
Jennifer Programming Language

sqlmigrate API reference

A version-tracked schema-migration runner over the sql library. It applies ordered, reversible DDL migrations and records what ran in a schema_migrations tracking table, so each migration runs exactly once and can be rolled back.

It is deliberately decoupled from the orm mapper: a migration's up / down are plain list of string DDL statements, so you build them with orm's DDL helpers (orm.createTable / orm.dropTable / orm.addColumn / ...) or hand-written SQL - the runner never touches a Schema or a Query. That keeps it usable for any sql program, ORM or not.

The runner owns transaction control: each migration's up (or down) statements plus its tracking-table write run inside one sql transaction. On PostgreSQL (transactional DDL) a failed statement rolls the whole step back. On MySQL / MariaDB every DDL statement commits implicitly, so a step that fails after some of its DDL ran keeps that DDL and stays unrecorded: keep one DDL statement per migration there. It takes a raw sql.Connection (not an orm.Session) for that reason.

migrate and rollbackMigrations hold a database-wide advisory lock for the whole run (pg_try_advisory_xact_lock / GET_LOCK), so two instances starting at once apply each migration once; a run that cannot get the lock within 60 seconds throws kind timeout.

Migrations are ordered by version lexically, so zero-pad numeric versions ("001", "002", ...) or use a sortable timestamp ("20260101120000"). The tracking table (version VARCHAR(255) PRIMARY KEY, description TEXT) is accepted verbatim by both MySQL and PostgreSQL, so the runner needs no dialect.

Needs sql, so the default jennifer binary.

Import with import "sqlmigrate.j" as sqlmigrate;. See the sqlmigrate guide for prose and examples.

Contents

NameWhat it is
migrateApply every pending migration in version order, each inside its own transaction, recording it in the...
migrationStatusThe applied / pending state of every migration, in version order.
rollbackMigrationsRoll back the steps most-recently-applied migrations (by version order), running each one's down statem...
Migrationstruct: One schema migration: an ordered up and reverse down list of DDL statements (build them with orm's DD...
MigrationStatusstruct: The applied / pending state of one migration, from sqlmigrate.migrationStatus.

Functions

migrate

jennifer
sqlmigrate.migrate(conn as sql.Connection, migrations as list of Migration)

Apply every pending migration in version order, each inside its own transaction, recording it in the schema_migrations tracking table (created if absent). Idempotent: already-applied versions are skipped. Holds the migration lock for the whole run, so concurrent runs apply each migration once; throws kind timeout when another run holds it for over 60 seconds.

conn
the open connection
migrations
the full migration set (any order)
Returns int
the number of migrations applied this run

migrationStatus

jennifer
sqlmigrate.migrationStatus(
    conn as sql.Connection,
    migrations as list of Migration)

The applied / pending state of every migration, in version order.

conn
the open connection
migrations
the full migration set
Returns list of MigrationStatus
one entry per migration, in version order

rollbackMigrations

jennifer
sqlmigrate.rollbackMigrations(
    conn as sql.Connection,
    migrations as list of Migration,
    steps as int)

Roll back the steps most-recently-applied migrations (by version order), running each one's down statements newest-first inside its own transaction and removing its tracking-table row. Holds the migration lock like migrate.

conn
the open connection
migrations
the full migration set
steps
how many applied migrations to reverse
Returns int
the number of migrations rolled back

Structs

Migration

One schema migration: an ordered up and reverse down list of DDL statements (build them with orm's DDL helpers or hand-written SQL), identified by version. Runs are ordered by version lexically, so zero-pad numeric versions ("001", "002", ...) or use a sortable timestamp.

jennifer
def struct Migration {
    version as string,
    description as string,
    up as list of string,
    down as list of string
};
version
the ordering key ([A-Za-z0-9._-])
description
a human-readable label (recorded in the tracking table)
up
the forward DDL statements
down
the reverse DDL statements

MigrationStatus

The applied / pending state of one migration, from sqlmigrate.migrationStatus.

jennifer
def struct MigrationStatus {
    version as string,
    description as string,
    applied as bool
};
version
the migration version
description
its description
applied
whether it has been applied