打开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> <version>${jeecgboot.version}</version>
</dependency> </dependency>
<!-- flyway 数据库自动升级 <!-- flyway 数据库自动升级 -->
<dependency> <dependency>
<groupId>org.flywaydb</groupId> <groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId> <artifactId>flyway-core</artifactId>
<version>7.15.0</version> <version>7.15.0</version>
</dependency>--> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

View File

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