Manage multiple database with the flyway migrations gradle plugin

flyway, gradle

Solution

First, you should never call `execute()` on a task (bad things will happen). Also, a task will be executed at most once per Gradle invocation.

To answer your question, apparently the flyway plugin doesn't support having multiple tasks of the same type. Looking at its implementation, I think you'll have to roll your own task. Something like:

import com.googlecode.flyway.core.Flyway
import org.katta.gradle.plugin.flyway.task.AbstractFlywayTask

class MigrateOtherDb extends AbstractFlywayTask {
    @Override
    void executeTask(Flyway flyway) {
        // set any flyway properties here that differ from
        // those common with other flyway tasks
        println "Executing flyway migrate"
        flyway.migrate()
}

task migrateOtherDb(type: MigrateOtherDb)

I recommend to file a feature request to support multiple tasks of the same type, with a convenient way to configure them.

Problem

We have two databases for which we'd like to manage their migrations using flyway's gradle plugin. I'd like to have a single task that can migrate both databases. However, I can't seem to get the flywayMigrate task to be called twice from a single task. Here's what I have... ``` task migrateFoo() { doFirst { flyway { url = 'jdbc:mysql://localhost/foo' user = 'root' password = 'password' locations = ['filesystem:dev/src/db/foo'] sqlMigrationPrefix = "" initOnMigrate = true outOfOrder = true } } doLast { tasks.flywayMigrate.execute() } } task migrateBar() { doFirst { flyway { url = 'jdbc:mysql://localhost/bar' user = 'root' password = 'password' locations = ['filesystem:dev/src/db/bar'] sqlMigrationPrefix = "" initOnMigrate = true outOfOrder = true } } doLast { tasks.flywayMigrate.execute() } } task migrate(dependsOn: ['migrateFoo','migrateBar']) {} ``` Explicitly calling either migrateFoo or migrateBar from the command line works fine, however, if I try to call the migrate task only database foo is updated. Both the doFirst and doLast tasks of the migrateBar task are called, however, the tasks.flywayMigrate.execute() task isn't called the second time from migrateBar. How can I get flyway to migrate both foo and bar from a single task?

Original source