【问题标题】:Liquibase refactor or undo changeset in multiple regionsLiquibase 在多个区域重构或撤消变更集
【发布时间】:2018-03-10 12:57:43
【问题描述】:

我是 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 个较低的区域(开发和测试)中运行,但我们还没有建立阶段或生产。我想“撤消”示例表并开始创建我真正的数据库结构。

我的理解是我有两个选择:条件drop table,rollback

我目前正在尝试实现条件删除表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 属性和 &lt;changeSetExecuted&gt; 标记无法被架构识别。在此之后,我尝试实现 maven 插件来回滚,但这会在构建时执行,所以这个目标只会解析一个区域。

普遍接受的撤消更改的方法是什么?如果您正在实施 Spring Boot liquibase 项目,该方法是否会有所不同?

【问题讨论】:

    标签: java spring-boot liquibase undo changeset


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2012-12-14
      • 2016-10-05
      • 1970-01-01
      • 2021-03-27
      • 1970-01-01
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      • 2012-06-06
      相关资源
      最近更新 更多