Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage multiple database with the flyway migrations gradle plugin

Tags:

gradle

flyway

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?

like image 439
Jeremy Jarrell Avatar asked Aug 08 '13 21:08

Jeremy Jarrell


People also ask

Does Flyway support multiple databases?

During development you need a fast, automated way to build multiple copies of a database on any development or test server, with each database at the right version.

Which is better Flyway or Liquibase?

While both tools are based on Martin Fowler's Evolutionary Database, there are many differences in what these tools offer. Here's where Liquibase and Flyway differ. The bottom line is that Liquibase is more powerful and flexible — covering more database change and deployment use cases than Flyway.

What does Flyway migrate do?

Migrate is the centerpiece of the Flyway workflow. It will scan the filesystem or your classpath for available migrations. It will compare them to the migrations that have been applied to the database. If any difference is found, it will migrate the database to close the gap.


1 Answers

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.

like image 57
Peter Niederwieser Avatar answered Sep 21 '22 08:09

Peter Niederwieser