打开flyway 数据库自动升级

This commit is contained in:
JEECG
2025-11-25 17:11:28 +08:00
parent 2740a2f419
commit 62549e0a1c
2 changed files with 48 additions and 45 deletions

View File

@ -25,12 +25,12 @@
<version>${jeecgboot.version}</version>
</dependency>
<!-- flyway 数据库自动升级
<!-- flyway 数据库自动升级 -->
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>7.15.0</version>
</dependency>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -1,7 +1,10 @@
package org.jeecg.config.flyway;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.FlywayException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@ -13,11 +16,11 @@ import javax.sql.DataSource;
import java.util.Map;
/**
* @Description: 初始化flyway配置 修改之后支持多数据源,当出现异常时打印日志,不影响项目启动
*
* @author: wangshuai
* @date: 2024/3/12 10:03
*/
* @Description: 初始化flyway配置 修改之后支持多数据源,当出现异常时打印日志,不影响项目启动
*
* @author: wangshuai
* @date: 2024/3/12 10:03
*/
@Slf4j
@Configuration
public class FlywayConfig {
@ -93,42 +96,42 @@ public class FlywayConfig {
*/
@Value("${spring.flyway.clean-disabled:true}")
private Boolean cleanDisabled;
// @Bean
// public void migrate() {
// if(!enabled){
// return;
// }
//
// DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
// Map<String, DataSource> dataSources = ds.getDataSources();
// dataSources.forEach((k, v) -> {
// if("master".equals(k)){
// String databaseType = environment.getProperty("spring.datasource.dynamic.datasource." + k + ".url");
// if (databaseType != null && databaseType.contains("mysql")) {
// try {
// Flyway flyway = Flyway.configure()
// .dataSource(v)
// .locations(locations)
// .encoding(encoding)
// .sqlMigrationPrefix(sqlMigrationPrefix)
// .sqlMigrationSeparator(sqlMigrationSeparator)
// .placeholderPrefix(placeholderPrefix)
// .placeholderSuffix(placeholderSuffix)
// .sqlMigrationSuffixes(sqlMigrationSuffixes)
// .validateOnMigrate(validateOnMigrate)
// .baselineOnMigrate(baselineOnMigrate)
// .cleanDisabled(cleanDisabled)
// .load();
// flyway.migrate();
// log.info("【升级提示】平台集成了MySQL库的Flyway数据库版本自动升级! ");
// } catch (FlywayException e) {
// log.error("【升级提示】flyway执行sql脚本失败", e);
// }
// } else {
// log.warn("【升级提示】平台只集成了MySQL库的Flyway实现了数据库版本自动升级! 其他类型的数据库,您可以考虑手工升级~");
// }
// }
// });
// }
@PostConstruct
public void migrate() {
if(!enabled){
return;
}
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
Map<String, DataSource> dataSources = ds.getDataSources();
dataSources.forEach((k, v) -> {
if("master".equals(k)){
String databaseType = environment.getProperty("spring.datasource.dynamic.datasource." + k + ".url");
if (databaseType != null && databaseType.contains("mysql")) {
try {
Flyway flyway = Flyway.configure()
.dataSource(v)
.locations(locations)
.encoding(encoding)
.sqlMigrationPrefix(sqlMigrationPrefix)
.sqlMigrationSeparator(sqlMigrationSeparator)
.placeholderPrefix(placeholderPrefix)
.placeholderSuffix(placeholderSuffix)
.sqlMigrationSuffixes(sqlMigrationSuffixes)
.validateOnMigrate(validateOnMigrate)
.baselineOnMigrate(baselineOnMigrate)
.cleanDisabled(cleanDisabled)
.load();
flyway.migrate();
log.info("【数据库升级】平台集成了MySQL库的Flyway数据库版本自动升级! ");
} catch (FlywayException e) {
log.error("【数据库升级】flyway执行sql脚本失败", e);
}
} else {
log.warn("【数据库升级】平台只集成了MySQL库的Flyway实现了数据库版本自动升级! 其他类型的数据库,您可以考虑手工升级~");
}
}
});
}
}