Data
Migrations
Generating migrations by comparing your entities with the database, running and reverting them, on SQL and MongoDB.
orm-migrations compares the entities in code with the tables in the database and writes the migration that closes the gap, like typeorm migration:generate, then applies it once per database with a checksum and a lock.
Generate
val orm = container.get(Orm::class) // after the repositories exist, so their entities are registered
val made = MigrationGenerator(orm, "main").generate("add user phone", File("src/main/resources/db/migration"))With the project template: ./gradlew migrationGenerate -Pname=add_phone.
It reads the live schema through JDBC and writes two files and an index:
V20261112101500__add_user_phone.sql -- up
U20261112101500__add_user_phone.sql -- down: the changes undone, in reverse order
migrations.txt -- lists them, for classpath loading-- add column users.phone
-- create index idx_users_phone on users
ALTER TABLE "users" ADD COLUMN "phone" VARCHAR(20) NULL;
CREATE INDEX "idx_users_phone" ON "users" ("phone");generate returns null when the database already matches. preview() lists the differences without writing. The comparison covers tables (create), columns (add, drop, change of type, nullability or default), unique constraints and indexes, for the tables of the entities the application registered. A table the code does not know is left alone, and a renamed column shows up as a drop plus an add, so read the file before you commit it.
The types are compared in one vocabulary, so PostgreSQL's int4 and INTEGER, or MySQL's 0.00 and 0 as a default, are not reported as changes.
Run and revert
val migrator = Migrator(orm, "main")
migrator.migrate(Migration.fromClasspath("db/migration")) // applies what is pending, in version order
migrator.revert(Migration.fromClasspath("db/migration")) // undoes the newest one with its U file; revert(list, steps = 3) for more
migrator.pending(all) // what has not run- A lock (a PostgreSQL advisory lock, a MySQL named lock) lets several instances start together; the others find nothing left to do.
- Editing a migration after it ran is an error (the checksum differs); so is a migration older than the newest applied one, or one that is applied but missing from the code.
Migration.sql(version, name, up, down)builds one by hand, andMigration.code(version, name) { store -> ... }writes one in Kotlin for what SQL cannot say.MigrationGenerator(orm, "main").synchronize()applies the differences straight away with no file, like TypeORMschema:sync. Use it in development only.
MongoDB
MongoDB has no schema, so a migration is for what does exist: collections and indexes, and rewriting documents.
val generator = MongoMigrationGenerator(orm, "events")
generator.preview() // create collection ..., create unique index ... on ...
generator.generate("add indexes", File("src/main/resources/db/migration")) // V<ts>__add_indexes.json: the up and down operations
val migrator = MongoMigrator(orm, "events")
migrator.migrate(MongoMigration.fromClasspath("db/migration"))
migrator.revert(...)
MongoMigration.code(2026111201, "backfill slugs") { store -> // a data migration in code
store.db.getCollection("posts").find().forEach { /* rewrite documents */ }
}The history lives in the collection kest_migrations, and a lock document that expires after five minutes keeps instances apart. The generated operations are createCollection, createIndex, dropIndex and dropCollection.
What is tested
On PostgreSQL and MySQL: generating a create, applying it, hand-made drift (an extra column, a dropped column, a changed type, a dropped index) detected and repaired by the next generated migration, reverting with the down file, and re-applying. On MongoDB: the same flow for indexes and collections.