Multiple datasources migrations using Flyway in a Spring Boot application

后端 未结 4 529
刺人心
刺人心 2020-12-31 03:39

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 pa

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-31 04:15

    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();
         }
    }
    

提交回复
热议问题