Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

I also had the same problem. I wanted to run flyway migrations for different databases and even for the same database with different configurations in ONE gradle build. for each database i need to migrate normal data tables and static data tables, so i use two flyway version tables and also two locations for the scripts. E.g.

ENV: dev MIGRATION1: data   (locations: db/scripts/data   table: _flyway_version_data)
         MIGRATION2: static (locations: db/scripts/static table: _flyway_version_static) 

ENV: test MIGRATION1 ....
          MIGRATION2 ....

As Peter states above, the flyway tasks are only executed ONCE no matter how often you call them.

The workaround i found does not seem to be nicest, but it works:

in build.gradle

task migrateFlywayDevData(type: GradleBuild) {
    buildFile = 'build.gradle'
    tasks = ['flywayMigrate']
    startParameter.projectProperties = [env: "dev", type="data"]
}

task migrateFlywayDevStatic(type: GradleBuild) {
    buildFile = 'build.gradle'
    tasks = ['flywayMigrate']
    startParameter.projectProperties = [env: "test", type="static"]
}
....(task defs for test env)

Basically i create a new gradle build for each of the configurations.

"buildFile = 'build.gradle'"

refers to itself, so all code is contained in one build.gradle file. The gradle call is then:

gradle migrateFlywayDevData migrateFlywayDevStatic ...

This is the first version. so code might be easily improved. However this solution lets you execute flyway tasks multiple times with one gradle call.

Feel free to comment (flyway plugin configuration is not shown here)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top