之前有篇文章结合我司项目描述了SpringBoot项目如何整合Liquibase,【文章链接:https://blog.csdn.net/NathanniuBee/article/details/90079840】,但是这个玩意在维护的时候还是有一些问题,个人感觉用起来很麻烦,可有啥办法呢,公司就这规范,我太难了。
这篇文章主要是写Liquibase的预校验相关的东西,好了正文。
http://www.liquibase.org/documentation/preconditions.html 官网
可以将前置条件附加到change logs或changesets ,以控制基于数据库状态的更新的执行。
使用前置条件有几个原因,包括:
- 记录变更日志的作者在创建变更日志时的假设。
- 确保运行变更日志的用户不会违反这些假设
- 在执行不可恢复的更改(如drop_Table)之前执行数据检查
- 根据数据库的状态控制运行哪些更改集,哪些更改集不运行
如果需要,前置条件可以是中的唯一标记。 更改日志级别的前提条件适用于所有更改集,而不仅仅是当前更改日志或其子更改日志中列出的更改集。
Sample With Preconditions
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.8"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.8
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.8.xsd">
<preConditions>
<dbms type="oracle" />
<runningAs username="SYSTEM" />
</preConditions>
<changeSet id="1" author="bob">
<preConditions onFail="WARN">
<sqlCheck expectedResult="0">select count(*) from oldtable</sqlCheck>
</preConditions>
<comment>Comments should go after preCondition. If they are before then liquibase usually gives error.</comment>
<dropTable tableName="oldtable"/>
</changeSet>
</databaseChangeLog>
只有在执行的数据库是Oracle且执行脚本的数据库用户是“SYSTEM”时,才会运行上述更改日志。如果“oldtable”中没有值,它也只会运行drop_Table命令。
处理故障和错误
Liquibase区分前提条件“失败”(检查失败)和“错误”(执行检查时抛出的异常),并且可以通过标签上的“onFail”和“onError”属性来控制对两者的反应。从1.8开始 可用属性
Available attributes
Attribute | Description |
---|---|
==onFail | 当前提条件失败时该怎么做(可执行以下操作 HALT/CONTINUE/MARK_RAN/WARN)。 |
onError | 当前置条件错误时该怎么办(可执行以下操作 HALT/CONTINUE/MARK_RAN/WARN)。 |
onUpdateSQL | 在updateSQL模式下可以做什么(见下文)。自1.9.5 |
onFailMessage | 在前提条件失败时输出的自定义消息。从2.0开始 |
onErrorMessage | 在前提条件失败时输出的自定义消息。从2.0开始 |
Possible onFail/onError values
Value | Description |
---|---|
HALT[停] | 立即停止执行整个更改日志。 [默认] |
CONTINUE | 跳过更改集。将在下次更新时再次尝试执行更改集。继续更改日志。 |
MARK_RAN | 跳过更改集,但将其标记为已执行。继续更改日志。 |
WARN | 输出警告并继续正常执行更改设置/更改日志。 |
在变更集之外(例如,在更改日志的开头),只有HALT和WARN是可能的值。
Possible onUpdateSQL values
Value Description
RUN Run the changeSet in updateSQL mode.
FAIL Fail the preCondition in updateSQL mode.
IGNORE Ignore the preCondition in updateSQL mode.
AND/OR/NOT Logic
条件逻辑可以使用可嵌套的, and ,标记应用于前提条件。如果未指定条件标记,则默认为AND。
Examples:
<preConditions onFail="WARN">
<dbms type="oracle" />
<runningAs username="SYSTEM" />
</preConditions>
Will check that the update is running on Oracle AND with the SYSTEM user, but will only generate a warning if the precondition fails.
<preConditions>
<dbms type="oracle" />
<dbms type="mysql" />
</preConditions>
Will require running on Oracle AND MySQL, which will always be false, unless a huge and unexpected merger takes place.
<preConditions>
<or>
<dbms type="oracle" />
<dbms type="mysql" />
</or>
</preConditions>
Will require running on Oracle OR MySQL which makes more sense than the above example.
<preConditions>
<or>
<and>
<dbms type="oracle" />
<runningAs username="SYSTEM" />
</and>
<and>
<dbms type="mssql" />
<runningAs username="sa" />
</and>
</or>
</preConditions>
Will require running as SYSTEM if executing against an Oracle database or running as SA if running against a MS-SQL database.
eg:
<changeSet author="zerah" id="2019052801-dev" context="dev">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="1">SELECT COUNT(*) FROM sbw WHERE sbw_id='78aa4ec7-dd34-4d45-a6dd-91a5e270a451'</sqlCheck>
</preConditions>
<sql>UPDATE UPDATE sbw SET is_delete=1,status='DELETE' WHERE sbw_id in('78aa4ec7-dd34-4d45-a6dd-91a5e270a451','') AND is_delete='0'</sql>
</changeSet>
修改字段名称:
<changeSet author="liquibase-docs" id="renameColumn-example">
<renameColumn catalogName="cat"
columnDataType="int"
newColumnName="id"
oldColumnName="id"
remarks="A String"
schemaName="public"
tableName="person"/>
</changeSet>
[外链图片转存失败(img-349Nc4ln-1565147921366)(BC9A515A8ADF458DA7A3EF0D27D00027)]
Liquibase官网预校验说明:http://www.liquibase.org/documentation/preconditions.html
Groovy脚本用法示例:https://www.jianshu.com/p/dcab2df0a105
liquibase 组件使用示例:https://blog.csdn.net/qq_35752353/article/details/82779935
附:
SQL优化:
https://www.cnblogs.com/yunfeifei/p/3850440.html
查看Mysql数据库引擎:
https://www.cnblogs.com/kerrycode/p/6999550.html