概述
Liquibase是一个数据库修改的解决方案,使您能够从开发到生产更快、更安全地修改和发布数据库变更。
Liquibase有多种使用方式, 直接集成到SpringBoot中是比较简单的一种, 它会在SpringBoot启动时自动检测运行, 省略了升级时手动运行Liquibase的步骤.
更详细的描述请查看官网
一、 引入依赖
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.28.0</version>
</dependency>
二、 单数据源配置
1. application.yml中添加liquibase配置
spring:
liquibase:
enabled: true
change-log: classpath:/liquibase/changelog-master.xml
2. 注册JavaBean
其实也可以不添加配置, 直接使用JavaBean配置,
@Configuration
public class LiquibaseConfiguration() {
@Bean
@ConfigurationProperties(prefix = "spring.liquibase")
public LiquibaseProperties getLiquibaseProperties() {
// 不修改配置文件的写法
LiquibaseProperties liquibaseProperties = new LiquibaseProperties();
if (StringUtils.equals(liquibaseProperties.getChangeLog(), "classpath:/db/changelog/db.changelog-master.yaml")) {
liquibaseProperties.setChangeLog("classpath:liquibase/changelog-master.xml");
}
return liquibaseProperties;
// 修改配置文件的写法
// return new LiquibaseProperties();
}
@Bean
public SpringLiquibase liquibase(DataSource dataSource) {
LiquibaseProperties liquibaseProperties = getLiquibaseProperties();
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setChangeLog(liquibaseProperties.getChangeLog());
liquibase.setDataSource(dataSource);
liquibase.setShouldRun(true);
return liquibase;
}
}
3. 添加changelog-master.xml和changelog-1.0.xml
changelog-master.xml
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.28.xsd">
<include file="classpath:/liquibase/changelog/changelog-1.0.xml"/>
</databaseChangeLog>
changelog-1.0.xml
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.28.xsd">
<changeSet id="createTable t_book" author="xiegb">
<createTable tableName="t_book">
<column name="id" type="int(8)" remarks="主键">
<constraints primaryKey="true"/>
</column>
<column name="name" type="varchar(200)" remarks="书名"/>
</createTable>
</changeSet>
</databaseChangeLog>
三、 多数据源配置
1. application.yml
# 多数据源配置
spring:
datasource:
admin:
username: root
password: root
jdbc-url: jdbc:mysql://localhost:3306/admin
driver-class-name: com.mysql.jdbc.Driver
liquibase:
change-log: classpath:liquibase/admin/changelog-master.xml
web:
username: root
password: root
jdbc-url: jdbc:mysql://localhost:3306/web
driver-class-name: com.mysql.jdbc.Driver
liquibase:
change-log: classpath:liquibase/web/changelog-master.xml
2. 注册JavaBean
@Configuration
public class LiquibaseConfiguration() {
/**
* admin数据库
*/
@Bean
public SpringLiquibase adminLiquibase(AdminDataSource dataSource) {
SpringLiquibase liquibase = new SpringLiquibase();
// Liquibase文件路径
liquibase.setChangeLog("classpath:liquibase/admin/changelog-master.xml");
liquibase.setDataSource(dataSource);
liquibase.setShouldRun(true);
liquibase.setResourceLoader(new DefaultResourceLoader());
// 覆盖Liquibase changelog表名
liquibase.setDatabaseChangeLogTable("changelog_admin");
liquibase.setDatabaseChangeLogLockTable("changelog_lock_admin");
return liquibase;
}
/**
* web数据库
*/
@Bean
public SpringLiquibase webLiquibase(WebDataSource dataSource) {
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setChangeLog("classpath:liquibase/web/changelog-master.xml");
liquibase.setDataSource(dataSource);
liquibase.setShouldRun(true);
liquibase.setResourceLoader(new DefaultResourceLoader());
liquibase.setDatabaseChangeLogTable("changelog_web");
liquibase.setDatabaseChangeLogLockTable("changelog_lock_web");
return liquibase;
}
}
3. changelog-master.xml文件和changelog-1.0.xml
admin/changelog-master.xml
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.28.xsd">
<include file="classpath:/liquibase/admin/changelog/changelog-1.0.xml"/>
</databaseChangeLog>
web/changelog-master.xml
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.28.xsd">
<include file="classpath:/liquibase/web/changelog/changelog-1.0.xml"/>
</databaseChangeLog>
changelog-1.0.xml文件同但数据源, 这里不再重复
四. 配置项描述
配置项 | 默认值 | 注释 |
---|---|---|
spring.liquibase.change-log | classpath:/db/changelog/db.changelog-master.yaml | changeLogFile 配置路径 |
spring.liquibase.check-change-log-location | true | 是否检查 changelog 配置路径存在 |
spring.liquibase.contexts | 只有指定的 context 的 changelog 才会被执行,多个 context 之间以逗号分隔 | |
spring.liquibase.default-schema | 默认数据库 | |
spring.liquibase.liquibase-schema | 用于存储 liquibase 对象的数据库 | |
spring.liquibase.liquibase-tablespace | 用于 liquibase 对象的表空间 | |
spring.liquibase.database-change-log-table | DATABASECHANGELOG | 存储数据库改变记录执行情况的表名 |
spring.liquibase.database-change-log-lock-table | DATABASECHANGELOGLOCK | 存储当前使用 liquibase 的用户信息表名 |
spring.liquibase.drop-first | false | 是否先删除表 |
spring.liquibase.enabled | true | 是否启用 liquibase |
spring.liquibase.user | liquibase 使用的数据库用户名,不指定时使用 spring.datasource 中的 | |
spring.liquibase.password | liquibase 使用的数据库用户密码,不指定时使用 spring.datasource 中的 | |
spring.liquibase.url | liquibase 使用的数据库url,不指定时使用 spring.datasource 中的 | |
spring.liquibase.labels | 指定标签的才会运行,多个标签以逗号分隔 | |
spring.liquibase.parameters | changelog 参数 | |
spring.liquibase.rollback-file | 当执行升级时写回滚 SQL 的文件 | |
spring.liquibase.test-rollback-on-update | 执行更新前是否验证回滚 |
网友评论