Manage multiple databases with flyway migration gradle

We have two databases for which we would like to manage our migrations using the flyway gradle plugin.

I would like to have one task that can migrate both databases. However, I cannot get the flywayMigrate task to be called twice from the same task.

Here is 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']) {} 

An explicit call to either migrateFoo or migrateBar from the command line works fine, however, if I try to call the migration task, only the foo database will be updated.

The doFirst and doLast tasks of the migrateBar task are called, however the tasks.flywayMigrate.execute () task is not called a second time from the migrateBar.

How can I get a span to transfer both foo and bar from the same task?

+6
source share
2 answers

First, you should never call execute() on a task (there will be bad things). In addition, the task will be executed no more than once per call to Gradle.

To answer your question, apparently, the flyway plugin does not support several tasks of the same type. Considering its implementation, I think you will have to minimize your own task. Sort of:

 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 submitting a function request to support several tasks of the same type with a convenient way to configure them.

+2
source

I also had the same problem. I wanted to start migration of flight routes for different databases and even for the same database with different configurations in the ONE gradle build. for each database I need to migrate regular data tables and static data tables, so I use two span version tables, as well as two scripting locations. For instance.

 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 .... 

According to Peter, the flight tasks are only performed no matter how often you name them.

The workaround I found doesn't seem to be the most enjoyable, 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 construct for each of the configurations.

"buildFile = 'build.gradle'"

refers to itself, so all code is contained in a single build.gradle file. Then call gradle:

 gradle migrateFlywayDevData migrateFlywayDevStatic ... 

This is the first version. therefore, the code can be easily improved. However, this solution allows you to perform multiple route tasks with a single gradle call.

Do not forget to comment (plugin configuration is not shown here)

0
source

Source: https://habr.com/ru/post/951351/


All Articles