我是 liquibase 的新手,并使用 spring boot + liquibase 项目创建了一个示例表。我在文件“createSampleTable.xml”中创建表的初始更改日志:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.7"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.7
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.7.xsd">
<changeSet id="1" author="Russ">
<comment>A sample table to see if liquibase works</comment>
<createTable tableName="testy">
<column name="VALUE" type="varchar(32)"/>
</createTable>
<insert tableName="testy">
<column name="value" value="Hello, world!"/>
</insert>
<insert tableName="testy">
<column name="value" value="Riddikulus!"/>
</insert>
</changeSet>
</databaseChangeLog>
现在我已经验证了我的 liquibase 配置,相同的部署已在我们的 2 个较低区域(开发和测试)中运行,但我们尚未启动阶段或生产。我想“撤消”示例表并开始创建我的真实数据库结构。
我的理解是我有两个选择:条件删除表,回滚
我目前正在尝试实现条件删除表 as the documentation states但即使 preconditions documentation 也无法识别建议的属性明确指出onFail
注释应该被识别。这是我对建议解决方案的实现(这是“createSampleTable.xml”文件的当前内容:
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.7"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.7
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.7.xsd">
<!-- The documentation for liquibase says if you ever create a changeset
in error that you never really wanted to follow this approach.
The general idea is to delete the original changeset and then
provide a new changeset that should only run if the original changset
has run as well. In essence this allows us to remove a naughty change
entirely from the code and prevent it from being run in new
environments while providing an "undo" changeset to be ran in
environments where this changeset has unfortunately already ran.
Documentation referenced and approach taken:
http://www.liquibase.org/2008/10/dealing-with-changing-changesets.html
To satisfy developer's curiosity and prevent them from having to
look up the history in the repository the original changeset of id=1
was simply to create a sample table to make sure the initial liquibase
config was working.
-->
<changeSet id="1-undo" author="Russ">
<preConditions onFail="MARK_RAN">
<changeSetExecuted id="1" author="Russ" changeLogFile="liquibase/createSampleTable.xml" />
</preConditions>
<dropTable tableName="testy"/>
</changeSet>
</databaseChangeLog>
但是,运行时 onFail
属性和
<changeSetExecuted>
模式无法识别标签。在此之后,我尝试实现 Maven 插件来回滚,但这是在构建上执行的,因此该目标只会解析一个区域。
普遍接受的撤消更改的方法是什么?如果您正在实现 Spring Boot liquibase 项目,该方法是否有所不同?
最佳答案
看起来您应用的架构相当旧(1.7)。
检查您包含的版本(如果是 Maven,请检查您的 pom)。
如果您使用 3.1 版本 xml 架构,您应该能够使用所需的标签:
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
...
</databaseChangeLog>
注意架构dbchangelog-3.1.xsd
如果使用 Maven,上述架构适用于以下插件配置:
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<propertyFile>src/main/liquibase.properties</propertyFile>
</configuration>
</plugin>
关于java - Liquibase 在多个区域重构或撤消变更集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46474952/