1. Flyway
可以将初始化sql在项目启动时候执行,取代单独的DBN更新包
2. 依赖
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>5.2.1</version>
</dependency>
3. 配置说明(springboot配置版本)
配置名 | 配置说明 |
---|
flyway.baseline-description | 对执行迁移时基准版本的描述. | flyway.baseline-on-migrate | 当迁移时发现目标schema非空,而且带有没有元数据的表时,是否自动执行基准迁移,默认false. | flyway.baseline-version | 开始执行基准迁移时对现有的schema的版本打标签,默认值为1. | flyway.check-location | 检查迁移脚本的位置是否存在,默认false. | flyway.clean-on-validation-error | 当发现校验错误时是否自动调用clean,默认false. | flyway.enabled | 是否开启flywary,默认true. | flyway.encoding | 设置迁移时的编码,默认UTF-8. | flyway.ignore-failed-future-migration | 当读取元数据表时是否忽略错误的迁移,默认false. | flyway.init-sqls | 当初始化好连接时要执行的SQL. | flyway.locations | 迁移脚本的位置,默认db/migration. | flyway.out-of-order | 是否允许无序的迁移,默认false. | flyway.password | 目标数据库的密码. | flyway.schemas | 设定需要flywary迁移的schema,大小写敏感,默认为连接默认的schema. | flyway.sql-migration-prefix | 迁移文件的前缀,默认为V. | flyway.sql-migration-separator | 迁移脚本的文件名分隔符,默认__ | flyway.sql-migration-suffix | 迁移脚本的后缀,默认为.sql | flyway.tableflyway | 使用的元数据表名,默认为schema_version | flyway.target | 迁移时使用的目标版本,默认为latest version | flyway.url | 迁移时使用的JDBC URL,如果没有指定的话,将使用配置的主数据源 | flyway.user | 迁移数据库的用户名 | flyway.validate-on-migrate | 迁移时是否校验,默认为true. |
4. API方式集成
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.configuration.ClassicConfiguration;
import org.flywaydb.core.api.configuration.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.nio.charset.Charset;
/**
* @author pp_lan
* @description
*/
@Component
public class MainDataSourceMigration {
private static final Logger LOGGER = LoggerFactory.getLogger(MainDataSourceMigration.class);
@Autowired
@Qualifier("mainDataSource")
private DataSource dataSource;
/**
* 生成配置
* @param location 配置路径
* @param dataSource 数据源
* @return
*/
public Configuration buildConfiguration(String location, DataSource dataSource) {
ClassicConfiguration configuration = new ClassicConfiguration();
configuration.setBaselineOnMigrate(true);
configuration.setTable("java_record_version");
configuration.setEncoding(Charset.forName("utf-8"));
configuration.setOutOfOrder(true);
configuration.setLocationsAsStrings(location);
configuration.setDataSource(dataSource);
return configuration;
}
@PostConstruct
public void init() {
Configuration configuration = buildConfiguration("db/migration/main", dataSource);
Flyway flyway = new Flyway(configuration);
try {
flyway.migrate();
LOGGER.info("初始化Sql成功");
} catch (Exception e) {
flyway.repair();
LOGGER.error("初始化Sql异常", e);
}
}
}
5. 屏蔽配置启动方式
springboot中默认使用flyway配置进行加载,使用api方式进行灵活配置时候需要将其(FlywayAutoConfiguration)进行屏蔽,不然会报错,部分报错信息及屏蔽方式如下所示
5.1 报错信息
Caused by: java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:70)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800)
... 18 common frames omitted
Caused by: org.flywaydb.core.api.FlywayException: Found non-empty schema(s) `xxxx` without schema history table! Use baseline() or set baselineOnMigrate to true to initialize the schema history table.
at org.flywaydb.core.Flyway$1.execute(Flyway.java:1371)
at org.flywaydb.core.Flyway$1.execute(Flyway.java:1341)
at org.flywaydb.core.Flyway.execute(Flyway.java:1696)
at org.flywaydb.core.Flyway.migrate(Flyway.java:1341)
... 25 common frames omitted
5.2 屏蔽方法
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
@SpringBootApplication(exclude = {FlywayAutoConfiguration.class})
public class HzBaseApplication {
public static void main(String[] args) {
SpringApplication.run(HzBaseApplication.class, args);
}
}
6. 执行结果
6.1 日志
2022-12-16 11:20:34,800 INFO (DruidDataSource.java:998)- {dataSource-1} inited
2022-12-16 11:20:35,116 INFO (Slf4jLog.java:49)- Database: jdbc:mysql://106.14.11.239:3306/hz_base (MySQL 5.7)
2022-12-16 11:20:35,343 INFO (Slf4jLog.java:49)- Successfully validated 1 migration (execution time 00:00.105s)
2022-12-16 11:20:35,492 INFO (Slf4jLog.java:49)- Current version of schema `hz_base`: 1.0.0
2022-12-16 11:20:35,493 WARN (Slf4jLog.java:53)- outOfOrder mode is active. Migration of schema `hz_base` may not be reproducible.
2022-12-16 11:20:35,509 INFO (Slf4jLog.java:49)- Schema `xxxx` is up to date. No migration necessary.
2022-12-16 11:20:35,565 INFO (MainDataSourceMigration.java:56)- 初始化Sql成功
6.2 数据库验证
?7.?扩展多数据源
多数据源通过buildConfiguration构建多个配置(数据源及script脚本),执行多次即可完成多数据源sql的初始化。
|