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?
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.
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.
According to the Heroku documentation using the Maven plugin is not recommended for running Flyway migrations.
Within the documentation there are two alternatives:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With