Why does my rollback not work in Liquibase?

1.9k views Asked by At

I have a Spring Boot application where I'm trying to test some migration with Liquibase. I'm trying to see how the rollback function works but I keep getting errors.

This is the migration file:

<?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"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
                  https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">

    <changeSet id="1" author="Me">
        <createTable tableName="Person">
            <column name="id" type="int" />
            <column name="name" type="string" />
        </createTable>
        <rollback>
            <dropTable tableName="Person" />
        </rollback>
    </changeSet>

</databaseChangeLog>

When I run the application, the table is created correctly... but I don't know how or where to run the command to execute the rollback. I've tried to run the following command in Maven Goal in IntelliJ:

mvn liquibase:rollback

When I run it says:

Failed to execute goal org.liquibase:liquibase-maven-plugin:3.10.3:rollback (default-cli) on project party: 
The database URL has not been specified either as a parameter or in a properties file.

If the database URL was missing or wrong then I'm thinking it shouldn't be able to create the table either?

1

There are 1 answers

1
Rakhi Agrawal On

mvn liquibase:rollback

This command executes the rollback segment of the changeset and should revert the task completed during the update stage. But if we issue this command alone, the build will fail.

The reason for that is, we do not specify the limit of rollback. The database will be completely wiped out by rolling back to the initial stage. Therefore it's mandatory to define one of the three constraints below to limit the rollback operation when the condition is satisfied:

1. rollbackTag - We can define a particular state of our database to be a tag. Therefore, we can reference back to that state. Command to achieve this is :

mvn liquibase:rollback -Dliquibase.rollbackTag=1.0

This executes rollback statements of all the changesets executed after tag “1.0”. Visit this link for details on how to tag database.

2. rollbackCount - We define how many changesets we need to be rolled back. If we define it to be one, the last changeset execute will be rolled back:

mvn liquibase:rollback -Dliquibase.rollbackCount=1

3. rollbackDate - We can set a rollback target as a date, therefore, any changeset executed after that day will be rolled back:

mvn liquibase:rollback "-Dliquibase.rollbackDate=Jun 03, 2017"

For more details, head over to this article which explains it very clearly. Also have a look at this post.