Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

oracle - Liquibase rollback never executes

<dependency>
  <groupId>org.liquibase</groupId>
  <artifactId>liquibase-maven-plugin</artifactId>
  <version>3.5.3</version>
</dependency>

<dependency>
  <groupId>oracle.jdbc</groupId>
  <artifactId>ojdbc7</artifactId>
  <version>12.1.0.2.0</version>
</dependency>

We can successfully creating tables, index, triggers, etc, but when building these out for the first time, we were getting errors from the scripts and the rollback is never executing, or at least the tables still existed. We worked through our issues and got scripts working successfully, but want to make sure the rollbacks are working. Was the rollback not executing because we are using <sqlfile> to build out everything and using <sql> to drop tables? What are we missing?

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <changeSet id="ddl_v1" author="AUTHOR_NAME" runInTransaction="true" failOnError="true">
        <sqlFile path="ddl/v1/Create_Table1_And_Relations.sql" relativeToChangelogFile="true"  splitStatements="true" endDelimiter="/"/>
        <sqlFile path="ddl/v1/Create_Table2_And_Relations.sql" relativeToChangelogFile="true" splitStatements="true" endDelimiter="/"/>
        <sqlFile path="ddl/v1/Create_Table3_And_Relations.sql" relativeToChangelogFile="true" splitStatements="true" endDelimiter="/"/>
        <sqlFile path="ddl/v1/Create_Table4_And_Relations.sql" relativeToChangelogFile="true" splitStatements="true" endDelimiter="/"/>
        <sqlFile path="ddl/v1/Create_Table5_And_Relations.sql" relativeToChangelogFile="true" splitStatements="true" endDelimiter="/"/>
        <rollback>
            <sql>DROP TABLE TABLE1;</sql>
            <sql>DROP TABLE TABLE2;</sql>
            <sql>DROP TABLE TABLE3;</sql>
            <sql>DROP TABLE TABLE4;</sql>
            <sql>DROP TABLE TABLE5;</sql>
        </rollback>
    </changeSet>

    <changeSet id="dml_v1" author="AUTHOR_NAME" runInTransaction="true" failOnError="true">
        <sqlFile path="dml/v1/Insert_TABLE1.sql" relativeToChangelogFile="true"/>
        <sqlFile path="dml/v1/Insert_TABLE5.sql" relativeToChangelogFile="true"/>
    </changeSet>

</databaseChangeLog>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Liquibase rollback mechanism is a bit confusing.

It's not supposed to be executed if the changeSet fails with some error. All changeSets are executed within a transaction, so if the changeSet fails, then the transaction should rollback itself. That's why the changeSets should be as atomic as possible and each changeSet should have preConditions. But it has nothing to do with the rollback tag.

The rollback is needed if you want to actually roll back to the previously fixated state of the database schema. For example you can use tag mechanism to fixate the state of the database schema.

In order to trigger the rollback you're supposed to run the specific liquibase rollback command.

Check out this article about Liquibase rollbacks. It has all the rollback basics.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...