Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run Flyway Migrations on Heroku?

I'm using FlywayDB for migrations on a Maven Java project. I am currently working to make it possible to deploy to Heroku.

On my local machine I am using the Maven Flyway plugin to run migrations:

$> mvn clean compile flyway:migrate

To do the same on heroku, I would normally try:

$> heroku run mvn flyway:migrate

However, mvn is not available after the build phase, so this yields an error (mvn: command not found)

How can I run my flyway migrations on Heroku?

like image 403
slifty Avatar asked Jul 03 '15 19:07

slifty


People also ask

Where do Flyway migrations go?

By default Flyway will look for migrations on the classpath under db/migration, which on a Maven project means src/main/resources/db/migration. You can however also use a location starting with the filesystem: prefix that can be anywhere on your disk.


Video Answer


2 Answers

I think your best bet is to create a small class in your application that uses the FlywayDB Java API. It might look like this:

class Migrator {
  public static void main(String[] args) throws Exception {
    ...
    Flyway flyway = new Flyway();
    flyway.setDataSource(url, user, password);
    flyway.migrate();
  }
}

Then create an entry in your Procfile like this:

migrate: java -cp target/classes:target/dependency/* Migrator

And finally run it as needed with heroku run migrate.

The reason Heroku does not include Maven in the slug (i.e. at runtime) is because the .m2 directory is not retained. If Maven were included, and you then ran a mvn command, it would first have to download the internet. The .m2 directory is not retained because it would make the slug size too large.

like image 133
codefinger Avatar answered Oct 07 '22 03:10

codefinger


According to the Heroku documentation using the Maven plugin is not recommended for running Flyway migrations.

Within the documentation there are two alternatives:

like image 43
David Avatar answered Oct 07 '22 01:10

David