配置信息
liquibase.properties
1 2 3 4 5 6 7
| driver: com.mysql.cj.jdbc.Driver classpath: ./mysql-connector-java-8.0.11.jar url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&serverTimezone=UTC&autoReconnect=true&allowPublicKeyRetrieval=true&useOldAliasMetadataBehavior=true username: root password: abc123 changeLogFile: myChangeLog.xml liquibase.hub.mode=off
|
mysql-connector-java-8.0.11.jar
提前下载好放到同级目录下面.
如果使用 liquibase diff
需要增加 reference
配置节点
1 2 3
| referenceUrl: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&serverTimezone=UTC&autoReconnect=true&allowPublicKeyRetrieval=true&useOldAliasMetadataBehavior=true referenceUsername: root referencePassword: abc123
|
myChangeLog.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?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-3.1.xsd">
<include file="./sql/create_table.xml" relativeToChangelogFile="true"></include> <include file="./sql/init_data.sql" relativeToChangelogFile="true"></include> </databaseChangeLog>
|
- include 可以设置多个, 现在这个配置
create_table.xml
是表结构, init_data.sql
是数据.
create_table.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <?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-3.1.xsd">
<changeSet id="1" author="zhaopeisheng"> <createTable tableName="department"> <column name="id" type="int"> <constraints primaryKey="true" nullable="false"/> </column> <column name="name" type="varchar(50)"> <constraints nullable="false"/> </column> <column name="active" type="varchar(1)" defaultValue="Y"/> </createTable> </changeSet> </databaseChangeLog>
|
id
和 author
必填
init_data.sql
1 2 3 4 5 6
|
insert into department values(1, '研发部', 'Y'); insert into department values(2, '销售部', 'Y');
insert into department values(3, '后勤部', 'Y');
|
- 文件开始必须以
-- liquibase formatted sql
开头
- 每一个
changeset
格式 -- changeset author:id
整体项目结构图
设置数据库
更新
执行日志
查看数据库
前两张表databasechangelog
databasechangeloglock
是liquibase的业务表.
给当前版本数据库打标签
1 2 3 4 5 6 7 8 9 10 11
| liquibase tag v1.0 liquibase history +---------------+----------------+----------------------+------------------+--------------+------+ | Deployment ID | Update Date | Changelog Path | Changeset Author | Changeset ID | Tag | +---------------+----------------+----------------------+------------------+--------------+------+ | 4357274202 | 24-4-29 下午6:21 | sql/create_table.xml | zhaopeisheng | 1 | | +---------------+----------------+----------------------+------------------+--------------+------+ | 4357274202 | 24-4-29 下午6:21 | sql/init_data.sql | jack | 2022_04_2 | | +---------------+----------------+----------------------+------------------+--------------+------+ | 4357274202 | 24-4-29 下午6:21 | sql/init_data.sql | tom | 2022_04_22 | v1.0 | +---------------+----------------+----------------------+------------------+--------------+------+
|
新增changeset记录
这种场景是项目在开发过程中新增表了.
在create_table.xml
记录增加新表
1 2 3 4 5 6 7 8 9 10
| <changeSet id="2" author="zhaopeisheng"> <createTable tableName="org"> <column name="id" type="int"> <constraints primaryKey="true" nullable="false"/> </column> <column name="name" type="varchar(50)"> <constraints nullable="false"/> </column> </createTable> </changeSet>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| liquibase update liquibase tag v2.0 liquibase history
+---------------+----------------+----------------------+------------------+--------------+------+ | Deployment ID | Update Date | Changelog Path | Changeset Author | Changeset ID | Tag | +---------------+----------------+----------------------+------------------+--------------+------+ | 4357274202 | 24-4-29 下午6:21 | sql/create_table.xml | zhaopeisheng | 1 | | +---------------+----------------+----------------------+------------------+--------------+------+ | 4357274202 | 24-4-29 下午6:21 | sql/init_data.sql | jack | 2022_04_2 | | +---------------+----------------+----------------------+------------------+--------------+------+ | 4357274202 | 24-4-29 下午6:21 | sql/init_data.sql | tom | 2022_04_22 | v1.0 | +---------------+----------------+----------------------+------------------+--------------+------+
+---------------+----------------+----------------------+------------------+--------------+------+ | Deployment ID | Update Date | Changelog Path | Changeset Author | Changeset ID | Tag | +---------------+----------------+----------------------+------------------+--------------+------+ | 4358382582 | 24-4-29 下午6:39 | sql/create_table.xml | zhaopeisheng | 2 | v2.0 |
|
回滚版本
回滚版本必须有 tag
标签的.
1 2 3 4 5 6 7 8 9 10 11
| liquibase rollback v1.0 liquibase history +---------------+----------------+----------------------+------------------+--------------+------+ | Deployment ID | Update Date | Changelog Path | Changeset Author | Changeset ID | Tag | +---------------+----------------+----------------------+------------------+--------------+------+ | 4357274202 | 24-4-29 下午6:21 | sql/create_table.xml | zhaopeisheng | 1 | | +---------------+----------------+----------------------+------------------+--------------+------+ | 4357274202 | 24-4-29 下午6:21 | sql/init_data.sql | jack | 2022_04_2 | | +---------------+----------------+----------------------+------------------+--------------+------+ | 4357274202 | 24-4-29 下午6:21 | sql/init_data.sql | tom | 2022_04_22 | v1.0 | +---------------+----------------+----------------------+------------------+--------------+------+
|
v2.0 新增的表被删除掉了
根据数据库逆向生成changelog.xml
把原来的myChangeLog.xml
改个名字. liquibase
是根据liquibase.properties
里面的changeLogFile
来生成的文件, 还原的数据库是liquibase.properties
配置的数据库.
1
| liquibase generate-changelog
|
新生成的表结构xml
, 这个文件可以直接复制到 create_table.xml
,
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?xml version="1.1" encoding="UTF-8" standalone="no"?> <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd"> <changeSet author="zhaopeisheng (generated)" id="1714362443795-1"> <createTable tableName="department"> <column name="id" type="INT"> <constraints nullable="false" primaryKey="true"/> </column> <column name="name" type="VARCHAR(50)"> <constraints nullable="false"/> </column> <column defaultValue="Y" name="active" type="VARCHAR(1)"/> </createTable> </changeSet> </databaseChangeLog>
|
用生成的xml
还原数据库
1 2 3 4 5 6 7 8 9 10 11 12
| liquibase update liquibase history
+---------------+----------------+----------------------+--------------------------+-----------------+-----+ | Deployment ID | Update Date | Changelog Path | Changeset Author | Changeset ID | Tag | +---------------+----------------+----------------------+--------------------------+-----------------+-----+ | 4362687615 | 24-4-29 下午7:51 | sql/create_table.xml | zhaopeisheng (generated) | 1714362443795-1 | | +---------------+----------------+----------------------+--------------------------+-----------------+-----+ | 4362687615 | 24-4-29 下午7:51 | sql/init_data.sql | jack | 2022_04_2 | | +---------------+----------------+----------------------+--------------------------+-----------------+-----+ | 4362687615 | 24-4-29 下午7:51 | sql/init_data.sql | tom | 2022_04_22 | | +---------------+----------------+----------------------+--------------------------+-----------------+-----+
|
代码地址
https://gitee.com/zhaops_learning/spring-cloud-learning/tree/master/mysql/liquibaseCmd