DB Migrations

Ebean has built in support for generating DB migrations and running them

Generating DB Migrations

Note that we can use ebeaninit to add GenerateDbMigration (the code below) to your project.

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

In src/test/kotlin add the code below to generate migrations.

To generate a DB migration we run this main method. The migration DDL for the given platform will be generated based on the changes to the model (e.g. alter table add column etc).

package main;

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

public class GenerateDbMigration {

   * Generate the DDL for the next DB migration.
  public static void main(String[] args) throws IOException {

    DbMigration dbMigration = DbMigration.create();

package main

import io.ebean.annotation.Platform
import io.ebean.dbmigration.DbMigration

 * Generate the DDL for the next DB migration.
fun main(args : Array<!String>) {

  // requires jvmTarget 1.8
  val dbMigration = DbMigration.create()


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 Migration

To run the migration using the built in migration runner, set ebean.migration.run to true.

In application.yaml
## run migrations when the Ebean starts
    run: true

With ebean.migration.run=true then when the 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.