Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple datasources migrations using Flyway in a Spring Boot application

Tags:

We use Flyway for db migration in our Spring Boot based app and now we have a requirement to introduce multi tenancy support while using multiple datasources strategy. As part of that we also need to support migration of multiple data sources. All data sources should maintain the same structure so same migration scripts should be used for migrating of all data sources. Also, migrations should occur upon application startup (as opposed to build time, whereas it seems that the maven plugin can be configured to migrate multiple data sources). What is the best approach to use in order to achieve this? The app already has data source beans defined but Flyway executes the migration only for the primary data source.

like image 799
Eli Avzak Avatar asked May 17 '16 16:05

Eli Avzak


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.

Does Flyway support multiple schemas?

To create multiple identical schemas you have to invoke Flyway once for each schema, with the flyway. schemas property set the to correct value. Flyway will then set the correct schema as the default one, letting you run your migration scripts unchanged (as long as you don't prefix the object names).

What is repeatable migration in Flyway?

Repeatable migrations have a description and a checksum, but no version. Instead of being run just once, they are (re-)applied every time their checksum changes. This is very useful for managing database objects whose definition can then simply be maintained in a single file in version control.

How do you implement a Flyway in a spring boot?

Configuring Flyway Database gradle file. In application properties, we need to configure the database properties for creating a DataSource and also flyway properties we need to configure in application properties. For properties file users, add the below properties in the application. properties file.


3 Answers

To make @Roger Thomas answer more the Spring Boot way:

Easiest solution is to annotate your primary datasource with @Primary (which you already did) and just let bootstrap migrate your primary datasource the 'normal' way.

For the other datasources, migrate those sources by hand:

@Configuration
public class FlywaySlaveInitializer {

     @Autowired private DataSource dataSource2;
     @Autowired private DataSource dataSource3;
     //other datasources

     @PostConstruct
     public void migrateFlyway() {
         Flyway flyway = new Flyway();
         //if default config is not sufficient, call setters here

         //source 2
         flyway.setDataSource(dataSource2);
         flyway.setLocations("db/migration_source_2");
         flyway.migrate();

         //source 3
         flyway.setDataSource(dataSource3);
         flyway.setLocations("db/migration_source_3");
         flyway.migrate();
     }
}
like image 105
Jacob van Lingen Avatar answered Sep 28 '22 11:09

Jacob van Lingen


Flyway supports migrations coded within Java and so you can start Flyway during your application startup.

https://flywaydb.org/documentation/migration/java

I am not sure how you would config Flyway to target a number of data sources via the its config files. My own development is based around using Java to call Flyway once per data source I need to work against. Spring Boot supports the autowiring of beans marked as @FlywayDataSource, but I have not looked into how this could be used.

For an in-java solution the code can be as simple as

    Flyway flyway = new Flyway();

    // Set the data source
    flyway.setDataSource(dataSource);

    // Where to search for classes to be executed or SQL scripts to be found
    flyway.setLocations("net.somewhere.flyway");

    flyway.setTarget(MigrationVersion.LATEST);
    flyway.migrate();
like image 10
Roger Thomas Avatar answered Sep 24 '22 11:09

Roger Thomas


Having your same problem... I looked into the spring-boot-autoconfigure artifact for V 2.2.4 in the org.springframework.boot.autoconfigure.flyway package and I found an annotation FlywayDataSource.

Annotating ANY datasource you want to be used by Flyway should do the trick.
Something like this:

@FlywayDataSource
@Bean(name = "someDatasource")
public DataSource someDatasource(...) {
        <build and return your datasource>
}
like image 6
RobMcZag Avatar answered Sep 28 '22 11:09

RobMcZag