mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2025-12-08 17:12:28 +08:00
打开flyway 数据库自动升级
This commit is contained in:
@ -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>
|
||||
|
||||
@ -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,实现了数据库版本自动升级! 其他类型的数据库,您可以考虑手工升级~");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user