How Migrax works¶
The pieces¶
Schema model. Migrax builds a model of the tables your entities need. When your project has
Hibernate 4 or newer, EclipseLink or OpenJPA, Migrax asks your own JPA provider for its
mapping, without connecting to a database, so names, types, join tables, element collections,
inheritance and sequences are exactly what your application expects. Without one of them it
scans the jakarta.persistence / javax.persistence annotations instead.
Snapshot. .migrax/snapshot.json records the schema your migrations produce. generate
compares the entities with the snapshot and writes only the difference. The snapshot also records
the dialect and naming strategy, so the names stay stable even if
your configuration changes later.
The very first generate has no snapshot, so it compares with the live database instead. When
that database already has tables, it also writes them to 0001_baseline.sql and records that
migration as applied there without running it, so an empty database can still be built from
the migrations alone. The baseline has no rollback script: undoing it would drop every table.
Migration files. Migrations live in src/main/resources/db/migration and are numbered:
0001_initial.sql, 0002_add_customers_phone.sql, ... The name describes the change. Next to
each generated migration, rollback/<same name>.sql undoes it.
History. migrax migrate records every applied migration in the migrax_history table,
with a SHA-256 checksum of the file. Failed attempts are recorded in migrax_failures.
What generate does¶
- Compiles the project if sources changed, and asks Maven or Gradle (preferring
./mvnw/./gradlew) for the runtime classpath. The classpath is cached in.migrax/classpath.txtand refreshed when the build file changes. - Reads the entities into a schema model.
- Compares it with the snapshot and works out the operations: create and drop tables, add, drop, rename and alter columns, keys, indexes, unique constraints, check constraints, comments and sequences.
- Asks about likely renames, and refuses drops unless you pass
--allow-destructive(see Changing entities). - Writes the migration and its rollback script, lints the SQL, and updates the snapshot.
migrax plan shows the same SQL without writing anything.
What migrate does¶
- Takes a database lock, so two instances starting at the same time don't both migrate.
- Checks the history: an applied file that changed or disappeared stops the run.
- Applies pending migrations in order, each in its own transaction with its history record.
- Runs repeatable
R__*.sqlmigrations that are new or changed, and the callbacks.
If a migration fails, its transaction is rolled back (where the database supports transactional
DDL) and the failure is recorded. migrate then refuses to continue until you have looked at
it; see Rollbacks and recovery.
Locking¶
| Database | Lock |
|---|---|
| PostgreSQL | pg_advisory_lock |
| YugabyteDB | pg_advisory_lock (2025.1 and newer), else a row in migrax_lock |
| CockroachDB | A row in migrax_lock (CockroachDB has no session locks) |
| MySQL, MariaDB, TiDB | GET_LOCK |
| SQL Server | sp_getapplock |
| Oracle | DBMS_LOCK (needs EXECUTE on DBMS_LOCK) |
| HSQLDB, Derby | A row in migrax_lock |
| H2 | Single-process database, no lock needed |
| SQLite | An operating-system lock on <database>.migrax-lock next to the database file |