多个区域中的Liquibase重构或撤消变更集

时间:2017-09-28 17:20:03

标签: 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 个答案:

答案 0 :(得分:0)

看起来您应用的架构很旧(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>