Skip to content

Production safety

Migrax is built around one idea: a migration should be proven before it runs in production.

Lint every migration

Every generated migration is linted as it is written, and migrax lint checks the pending ones (or --all, or the files you name). Findings explain the risk and the safer alternative:

$ migrax lint
src/main/resources/db/migration/0007_add_orders_notes_and_more.sql, statement 3: warning MX007
  ALTER TABLE test_table DROP COLUMN numberOfItems
  This deletes data, and running application instances that still use it will fail.
  Fix: Deploy code that no longer uses it first, then drop it in a later release (expand/contract: 'migrax generate --zero-downtime' does this).
1 file(s) checked, 1 finding(s).

See all lint rules. To accept a finding deliberately, put -- migrax:lint-ignore MX007 on the line before the statement.

Non-blocking changes on PostgreSQL

migrax generate --safe moves index and constraint creation on existing tables into a second, non-transactional migration that uses CREATE INDEX CONCURRENTLY, NOT VALID and VALIDATE CONSTRAINT, so writes keep flowing while they build.

Changes while the old version still runs

During a rolling deployment, instances of the old and the new version use the database at the same time. migrax generate --zero-downtime splits a change so that both work: see Zero-downtime changes.

Verify before you ship

$ migrax verify
Applied 7 migration(s) to a fresh postgresql database.
Schema matches the entities (Hibernate 6.6.13.Final schema validation).
Rollback scripts: ok (7 rolled back and re-applied).
Verified.

verify:

  1. starts a throwaway database: in-memory H2, HSQLDB or Derby, a Docker container of your engine, or the empty database you pass with --url;
  2. applies every migration from scratch;
  3. starts your Hibernate version with schema validation against the result (without Hibernate, it compares the schema with the entities);
  4. tests every rollback script on its own, newest first: it rolls the migration back, applies it again and checks that the schema is the same as before. A script that fails or leaves something behind is named, for example Rollback scripts: failed: 0005_tag_color.sql fails when applied again after its rollback script ran: .... When older migrations have no rollback script, it checks the newest ones that do.

Run it in CI on every pull request.

Detect manual changes

$ migrax drift
2 difference(s) between the database and the snapshot:
  - products.name: expected varchar(255) but the database column is shorter (varchar(100))
  - products.hotfix: column exists in the database but not in the expected schema
Someone changed the database outside migrations. Write a migration for intended changes
('migrax new <name>'), or revert them.

drift reads the live database and compares it with what the migrations produce (the snapshot), or with the entities (--entities). It reports missing and extra tables and columns, keys, unique constraints, nullability, type changes and narrowed columns. It exits with code 2 when it finds drift, so it can run on a schedule.

Drift at startup

New in Migrax 0.16.0.

The application can run the same comparison every time it starts, so a change someone made by hand is noticed at the next deployment instead of weeks later. Turn it on in the application's configuration:

application.properties
migrax.drift.check=fail
Value When the database differs from the schema its migrations produce
off (default) Nothing is checked
warn The application starts and logs every difference
fail The application doesn't start; the log lists every difference

The check runs right after the startup migrations, in the Spring Boot, Quarkus, Micronaut, Helidon and Jakarta EE integrations. With fail, a hand-made column stops the start like this:

Migrax: 1 difference(s) between the database and the schema its migrations produce:
  - note.manual_change: column exists in the database but not in the expected schema
Someone changed the database outside migrations. Write a migration for intended changes, or revert them.
(migrax.drift.check=fail stops the start; warn only logs.)

When it finds nothing, it logs Migrax: no drift: the database matches the schema its migrations produce. While migrations are pending (for example with migrax.migrate-at-startup=false, before the pipeline has run them) or have failed, the check is skipped: their changes would show as differences. With several databases, every database is checked, and migrax.databases.<name>.drift.check sets it for one of them.

Put the snapshot into the application

The expected schema is .migrax/snapshot.json, the file generate keeps up to date and you commit. The application reads it as META-INF/migrax/snapshot.json, so the build has to copy it in:

Nothing to do: the extension copies it while the application is built, also into native executables.

Nothing to do: the Migrax Gradle plugin (id("org.migrax")) adds it to the application's resources.

Add the package-snapshot goal to the Migrax plugin; it runs while the resources are copied:

pom.xml
<plugin>
  <groupId>org.migrax</groupId>
  <artifactId>migrax</artifactId>
  <version>0.19.0</version>
  <executions>
    <execution>
      <goals>
        <goal>package-snapshot</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Copy .migrax/snapshot.json to META-INF/migrax/snapshot.json in the application's classpath, and .migrax/<name>/snapshot.json to META-INF/migrax/<name>/snapshot.json for each further database.

Without the file, warn logs that it can't check and fail stops the start with the same message, so a missing build step is noticed at once.

Hibernate must not change the schema

When Hibernate is allowed to create or update tables at startup, the database drifts away from your migrations, and migrations later fail with "already exists". create and drop-and-create even delete all data on every start. migrax doctor finds these settings and shows the exact line to change:

  [warn]  Hibernate will change your database schema by itself
          Found:  quarkus.hibernate-orm.schema-management.strategy = update
                  in src/main/resources/application.properties, line 15

          Why:    Hibernate adds tables and columns itself when the application starts. ...

          Fix:    change   quarkus.hibernate-orm.schema-management.strategy=update
                  to       quarkus.hibernate-orm.schema-management.strategy=none
                  (or validate: Hibernate then checks the schema at startup without changing it)

When a DBA runs the migrations

Where developers may not change production themselves, hand the DBA one script:

$ migrax migrate --sql > release-2026-10.sql

It is exactly what migrate would do now against that database: the pending migrations in order, each with the row Migrax records in migrax_history (so migrax status shows them as applied afterwards), transactions where the database supports them, and the end markers its tool expects (GO for sqlcmd, / after PL/SQL for sqlplus, DELIMITER for mysql). The first lines say how to run it so it stops at the first error, for example psql -v ON_ERROR_STOP=1 -f release-2026-10.sql. Writing the script reads the database but changes nothing.

With --schemas tenant_a,tenant_b, the script has a section per schema that starts by switching to it (SET search_path, USE or ALTER SESSION SET CURRENT_SCHEMA), so one run of the file migrates every tenant.

A Java migration can't be written as SQL. The script stops before the first one and its last lines say what to do: run the script, then migrax migrate --to <that migration>, then write the next script with migrax migrate --sql.

Applying a release in steps

To roll out several migrations one at a time (for example, run the slow index first and check the database before going on), stop after a given migration:

$ migrax migrate --to 0007
Applying migration '0007_add_order_index.sql'...
Successfully applied migration '0007_add_order_index.sql'.
Applied 1 migration(s) up to 0007_add_order_index.sql.
2 migration(s) after it stay pending.

--to takes the file name, its start, or the version number. Repeatable migrations (R__*.sql) wait until a migrate without --to, because they may use what later migrations add.

A release checklist

  • Migration generated in development and reviewed in the pull request
  • CI: check, lint --strict, verify
  • Backup taken before the production run
  • migrax migrate (or startup migration) as part of the release
  • migrax status and migrax drift afterwards