Liquibase Rollback Workflow
Liquibase provides commands to allow you to undo changes you have made to your database, either automatically or with a custom rollback command. The intention of a rollback script is to return the database to a previous specified point in time.
Note: Rollback support is available in command line, Ant, and Maven.
There are three rollback modes:
|rollback <tag> command||This command will revert all changes made to the database after the specified tag. It is used when you want to undo a series of changes related to a specific tag such as a numbered release.|
This command is used to revert all changes made to the database from the current date to the date and time you specify.
There are several ways to revert your changes with the
There are three options:
You can use
|rollbackCount <value> command||This command is used to roll back a specified number of changesets, where
Liquibase Pro rollback commands
Liquibase Pro contains additional built in commands that allow you to perform targeted rollback tasks.
|rollbackOneChangeSet command||This command will revert one non-sequential changeset made during a previous change to the database without affecting any other changes made to your database.|
|rollbackOneUpdate command||This command will revert all changesets related to a specific
Rollback scripts are one of the most difficult aspects of application development to create and maintain, especially when data is modified.
If the database change is simple such as adding a new index, or an update to a stored procedure, then the rollback is straight forward.
Rolling back complex changes that involve structural changes and data migrations are much more difficult.
So before performing a rollback, it is important to validate the changes Liquibase will run prior to making the change to the database. These two commands help in validating and testing rollbacks.
|updateTestingRollback command||This command is used to test rollback functionality when deploying changesets in your changelog sequentially. It tests the rollback by deploying all pending changesets, executing a sequential rollback in reverse order for the changesets that were deployed, then running the update again.|
|futureRollbackSQL command||Produces the raw SQL Liquibase would use to revert changes associated with undeployed changesets. Used when auditors need to verify that all database changes have a rollback.|
Rollbacks are also validated with the help of SQL helper commands.
|rollbackSQL <tag> command||This helper command is used in conjunction with the
|rollbackToDateSQL command||This helper command is used in conjunction with the
|updateCountSQL <value> command||This helper command is used in conjunction with the
|rollbackOneChangeSetSQL command||This helper command is used in conjunction with the
|rollbackOneUpdateSQL command||This helper command is used in conjunction with the
Rollback test cycle
Rollback scripts need to be tested just like application code so we know they work as designed.
You will need to carefully test your rollback scripts. This includes determining who will test the rollback script, how and when they will be tested.
A complete test cycle should include:
Deploying all changes to the database and validating that they were deployed.
Rolling back all changes to the database, validating that all changes were reversed, and verification that the database was brought back to the previous state.
Redeploying all changes to the database. This is required to verify that the rollback did not miss any changes that could impact a future deployment.
Creating rollback statements
Many Change Types such as create table, rename column, and add column can automatically create rollback statements. If your changelog contains only statements that fit into this category, your rollback commands will be generated automatically.
Other Change Types such as drop table and insert data have no corresponding rollback commands that can be automatically generated. In these cases, and cases where you want to override the default generated rollback commands, you can specify the rollback commands via the tag within the changeset tag. If you do not want to undo a change in a rollback mode, use an empty rollback tag:
<changeSet id="3" author="liquibaseuser"> <createTable tableName="changeRollback"> <column name="id" type="int"/> </createTable> <rollback/> </changeSet>
To determine if Liquibase can automatically rollback a specific change type, check the Change Types documentation. Go to the Database Support section to determine whether auto rollback is available for your database platform.
Note: Automatic rollback is not supported for formatted SQL changesets. You should add custom rollback statements to formatted SQL changesets if you want to use rollback commands.
Examples of custom rollback statements in the formatted SQL changesets:
-- changeset liquibaseuser:1 create table Details1 ( id int primary key, name varchar(255) ); -- rollback drop table Details1;
-- liquibase formatted sql -- changeset liquibaseuser:2 insert into Details1 values ('1','The First', 'Country') insert into Details1 values ('1','The Second', 'Country2') -- rollback delete from Details1 where id='1'