DB Migrations

DB Migrations are DDL changes that are applied typically when the application is started.

Ebean can generate the migrations for us by performing a diff on the model and then generating database platform specific DDL for the change.

Ebean can also run the migrations (similar to FlywayDb). It is recommended to use Ebean's built in migration runner rather than FlywayDb or LiquiBase.

Generate a Migration

In src/test/java add code to generate migrations.

package main;

import io.ebean.annotation.Platform;
import io.ebean.dbmigration.DbMigration;
import java.io.IOException;

public class GenerateDbMigration {

  /**
   * Generate the next "DB schema DIFF" migration.
   */
  public static void main(String[] args) throws IOException {


    DbMigration dbMigration = DbMigration.create();
    dbMigration.setPlatform(Platform.POSTGRES);

    dbMigration.generateMigration();
  }
}

To generate a migration we run this main method.

We run this manually when we have completed some work and consider it ready to be released. Running the generation starts the EbeanServer in offline mode, performs a DIFF of the model and generates the migration DDL script.

Running migrations

To get Ebean to run the DB migrations on startup set ebean.migration.run to true.

In application.yml
## run migrations when the EbeanServer starts
ebean:
  migration:
    run: true

With ebean.migration.run=true then when Ebean starts it will look at the migrations and run any that need to be run. The migration runner will by default create a table called db_migration that holds the meta data about the migrations that have been run and inserts into this table when migrations are successfully executed.