源码分析:
class LiquibaseChangelogMissingFailureAnalyzer extends AbstractFailureAnalyzer<ChangeLogParseException> {
private static final String MESSAGE_SUFFIX = " does not exist";
@Override
protected FailureAnalysis analyze(Throwable rootFailure, ChangeLogParseException cause) {
if (cause.getMessage().endsWith(MESSAGE_SUFFIX)) {
String changelogPath = extractChangelogPath(cause);
return new FailureAnalysis(getDescription(changelogPath),
"Make sure a Liquibase changelog is present at the configured path.", cause);
}
return null;
}
private String extractChangelogPath(ChangeLogParseException cause) {
return cause.getMessage().substring(0, cause.getMessage().length() - MESSAGE_SUFFIX.length());
}
private String getDescription(String changelogPath) {
return "Liquibase failed to start because no changelog could be found at '" + changelogPath + "'.";
}
}
LiqubaseDatabaseInitliazer:
class LiquibaseDatabaseInitializerDetector extends AbstractBeansOfTypeDatabaseInitializerDetector {
@Override
protected Set<Class<?>> getDatabaseInitializerBeanTypes() {
return Collections.singleton(SpringLiquibase.class);
}
}
整理jar 包: package org.springframework.boot.liquibase;
二。使用: LiquiBase 是一个数据库重构和迁移的开源框架,通过日志的方式来记录数据库的变更。通过执行日志文件中的修改,将数据库更新或回滚到达一致的状态。
三。使用步骤:
<!-- liquibase -->
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<!--mysql jdbc驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
2.2、创建SpringBoot配置文件来指定一些配置文件地址 LiquibaseConfig.java @Configuration public class LiquibaseConfig {
@Bean
public SpringLiquibase liquibase(DataSource dataSource) {
SpringLiquibase liquibase = new SpringLiquibase();
//设置数据源
liquibase.setDataSource(dataSource);
//sql文件位置
liquibase.setChangeLog("classpath:liquibase/master.xml");
return liquibase;
}
}
- application.properties
spring.datasource.username=root spring.datasource.password=123456 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true spring.datasource.driver-class-name=com.mysql.jdbc.Driver
在这里插入代码片
2.3、创建配置文件
<includeAll path="liquibase/sql/" relativeToChangelogFile="false"/>
<include file="liquibase/sql/service-v1.sql" relativeToChangelogFile="false"/>
|