baomidou / dynamic-datasource

dynamic datasource for springboot 多数据源 动态数据源 主从分离 读写分离 分布式事务
https://www.kancloud.cn/tracy5546/dynamic-datasource/2264611
Apache License 2.0
4.74k stars 1.19k forks source link

多数据源下实现数据库升级脚本自动更新 #258

Closed Li-Jin closed 4 years ago

Li-Jin commented 4 years ago

Enviroment

JDK Version(required): 1.8

SpringBoot Version(required): 2.2

dynamic-datasource-spring-boot-starter Version(required):3.2+

druid Version(optional):1.1.21

Describe

场景:多数据源,多租户,所有租户数据库完全相同,系统升级数据库升级脚本需要在系统中根据所有数据源进行升级

huayanYu commented 4 years ago

你可以利用系统启动初始化来执行一次。

Li-Jin commented 4 years ago

已处理好,整合flyway 在动态数据源里面拿出所有数据源 循坏遍历数据源,flyway执行升级脚本

public class FlywayConfiguration {
private final DynamicRoutingDataSource dataSourceProperties;

public FlywayConfiguration(DynamicRoutingDataSource dataSourceProperties) {
this.dataSourceProperties = dataSourceProperties;
}

@PostConstruct
public void migrate() {
Map<String, DataSource> dataSourceMap = dataSourceProperties.getCurrentDataSources();
dataSourceMap.forEach((k, v) -> {
Flyway flyway = Flyway.configure().dataSource(v).load();
flyway.setBaselineOnMigrate(true);
flyway.setCleanDisabled(true);
flyway.setBaselineVersion(MigrationVersion.fromVersion("0"));
flyway.migrate();
});
}
}