java - Liquibase 在多个区域重构或撤消变更集

标签 java spring-boot liquibase undo changeset

我是 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/

相关文章:

java - 获取有关 SQL 更新记录的详细信息

java - Apache HttpClient(4.1 及更高版本): how to do basic authentication?

java - 使用 Spring security oauth,使用自定义 OAuth 提供程序,我得到 [authorization_request_not_found],我应该自己处理回调方法吗?

Spring liquibase 在应用程序崩溃时恢复

postgresql - Liquibase:我真的需要为单行插入添加回滚部分吗?

java - 同步是否锁定结果集对象?

java - Spring MVC 处理未映射的请求

java - 为什么 hibernate 不在 OneToMany 的所有者端设置外键

java - Broadleaf 未使用 Java > 8 构建或启动 : Unable to start embedded Tomcat

sql - 将不可为空的列添加到现有表失败。 "value"属性是否被忽略?