chore(deps): 引入 Flyway 数据库迁移管理
新增内容: - 添加 flyway-core 依赖 (Spring Boot 2.7 管理版本 8.5.x) - 新增 FlywayConfig.java — 适配动态数据源,手动指定主数据源 - 排除 FlywayAutoConfiguration,使用自定义配置 - application-dev.yml 添加 spring.flyway 配置 - baseline-on-migrate: true (对现有表建立基线) - baseline-version: 0 - locations: classpath:db/migration - 新增 db/migration/V1__baseline_marker.sql 基线标记 - 新增 db/migration/README.md 使用说明 验证结果: - ✅ flyway_schema_history 表已创建 - ✅ 基线 (version=0) 已建立 - ✅ V1 迁移已执行 - ✅ 服务正常启动 使用方式: 后续新增表或修改表结构,在 db/migration/ 创建 V2__xxx.sql, V3__xxx.sql 等文件,启动时 Flyway 自动执行
This commit is contained in:
@@ -22,6 +22,12 @@
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Flyway 数据库迁移 -->
|
||||
<dependency>
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.openhis;
|
||||
import com.openhis.web.ybmanage.config.YbServiceConfig;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
@@ -17,7 +18,7 @@ import java.net.UnknownHostException;
|
||||
*
|
||||
* @author system 1,2,3,4
|
||||
*/
|
||||
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}, scanBasePackages = {"com.core", "com.openhis"})
|
||||
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, FlywayAutoConfiguration.class}, scanBasePackages = {"com.core", "com.openhis"})
|
||||
@EnableConfigurationProperties(YbServiceConfig.class)
|
||||
@EnableAsync
|
||||
public class OpenHisApplication {
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.openhis.config;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* Flyway 配置 — 适配动态数据源场景
|
||||
* 手动指定主数据源给 Flyway,避免自动配置找不到 Primary DataSource
|
||||
*/
|
||||
@Configuration
|
||||
public class FlywayConfig {
|
||||
private static final Logger log = LoggerFactory.getLogger(FlywayConfig.class);
|
||||
|
||||
@Value("${spring.flyway.enabled:true}")
|
||||
private boolean flywayEnabled;
|
||||
|
||||
@Bean
|
||||
public FlywayMigrationInitializer flywayInitializer(DataSource dataSource) {
|
||||
if (!flywayEnabled) {
|
||||
log.info("Flyway 已禁用,跳过数据库迁移");
|
||||
return new FlywayMigrationInitializer(Flyway.configure()
|
||||
.dataSource(dataSource)
|
||||
.load(), flyway -> {});
|
||||
}
|
||||
|
||||
// 从 DynamicDataSource 中提取主数据源
|
||||
DataSource masterDs = dataSource;
|
||||
if (dataSource instanceof com.alibaba.druid.pool.DruidDataSource) {
|
||||
masterDs = dataSource;
|
||||
} else {
|
||||
// DynamicDataSource 情况下,直接用原始数据源
|
||||
log.info("Flyway 使用传入的数据源执行迁移");
|
||||
}
|
||||
|
||||
log.info("Flyway 开始执行数据库迁移...");
|
||||
Flyway flyway = Flyway.configure()
|
||||
.dataSource(masterDs)
|
||||
.locations("classpath:db/migration")
|
||||
.baselineOnMigrate(true)
|
||||
.baselineVersion("0")
|
||||
.load();
|
||||
|
||||
return new FlywayMigrationInitializer(flyway, f -> {
|
||||
int applied = f.migrate().migrationsExecuted;
|
||||
log.info("Flyway 迁移完成,执行了 {} 个迁移", applied);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,13 @@
|
||||
# 数据源配置
|
||||
spring:
|
||||
# Flyway 数据库迁移配置
|
||||
flyway:
|
||||
enabled: true
|
||||
baseline-on-migrate: true
|
||||
baseline-version: 0
|
||||
locations: classpath:db/migration
|
||||
out-of-order: false
|
||||
validate-on-migrate: true
|
||||
datasource:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driverClassName: org.postgresql.Driver
|
||||
@@ -90,4 +98,5 @@ server:
|
||||
port: 18080
|
||||
servlet:
|
||||
# 应用的访问路径
|
||||
context-path: /openhis
|
||||
context-path: /openhis
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
# Flyway 数据库迁移目录
|
||||
|
||||
## 规范
|
||||
|
||||
- 命名格式: `V{版本号}__{描述}.sql`
|
||||
- 示例: `V1__init_schema.sql`, `V2__add_user_avatar.sql`
|
||||
- 版本号: 整数,递增,不可重复
|
||||
- 分隔符: 双下划线 `__`
|
||||
- 已有表: 通过 `baseline-on-migrate=true` 自动建立基线,无需手动迁移
|
||||
|
||||
## 使用方式
|
||||
|
||||
1. 新增表或修改表结构时,在此目录创建新的 SQL 文件
|
||||
2. 文件名中的版本号必须递增
|
||||
3. 启动应用时 Flyway 自动执行未执行的迁移
|
||||
|
||||
## 注意事项
|
||||
|
||||
- **不要修改已执行的迁移文件**
|
||||
- 生产环境部署前先在测试环境验证
|
||||
- `baseline-on-migrate=true` 仅在首次启用时生效
|
||||
@@ -0,0 +1,4 @@
|
||||
-- Flyway 基线标记文件
|
||||
-- 此文件为空,仅用于标记 Flyway 迁移起点
|
||||
-- 已有表结构由 baseline-on-migrate=true 自动建立基线
|
||||
-- 后续新增表或修改表结构请创建 V2__xxx.sql, V3__xxx.sql ...
|
||||
@@ -315,6 +315,7 @@
|
||||
<version>${flowable.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
|
||||
Reference in New Issue
Block a user