在Spring Boot应用程序中使用Flyway进行多个数据源迁移 [英] Multiple datasources migrations using Flyway in a Spring Boot application
问题描述
我们在基于Spring Boot的应用程序中使用Flyway进行数据库迁移,现在我们需要在使用多个数据源策略的同时引入多租户支持.作为其中的一部分,我们还需要支持多个数据源的迁移.所有数据源应保持相同的结构,因此应使用相同的迁移脚本来迁移所有数据源.同样,迁移应该在应用程序启动时进行(与构建时间相反,而似乎可以将Maven插件配置为迁移多个数据源).为了达到此目的,最好的方法是什么?该应用程序已经定义了数据源bean,但是Flyway仅对主要数据源执行迁移.
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.
推荐答案
Flyway支持在Java中编码的迁移,因此您可以在应用程序启动期间启动Flyway.
Flyway supports migrations coded within Java and so you can start Flyway during your application startup.
https://flywaydb.org/documentation/migration/java
我不确定您如何通过其配置文件配置Flyway来定位多个数据源.我自己的开发基于使用Java对每个需要使用的数据源调用一次Flyway. Spring Boot支持标记为@FlywayDataSource
的bean的自动装配,但是我还没有研究如何使用它.
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.
对于Java内解决方案,代码可以简单到
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();
这篇关于在Spring Boot应用程序中使用Flyway进行多个数据源迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!