Posted 10 Feb 2020 by Nathan Voxland
In our most recent Liquibase community survey, we learned that many Liquibase users are interested in a feature that allows them to rollback a specific change. Well, we have good news.
While it’s true that it is almost always a superior life choice to roll forward, sometimes life throws curveballs and it’s super handy to have a way to target a changeset that you would like to roll back and forget ever happened. And hey, you did your homework and know that this particular rollback won’t hurt anything else. The case may be rare, but when you need it, you need it. And that is why we are introducing Targeted Rollback, a powerful new feature that allows you to cherry-pick one changeset to undo.
With Liquibase 3.8.6, Community users can still undo changes in reverse order starting from the most recently deployed changeset while users with a Liquibase Pro license can target any specific changeset to roll back, leaving all the changesets that followed it untouched.
Below is a group of changesets labeled a through x, with x being the most recently deployed change. Let’s assume I want to roll back changeset b.
In order to undo changeset b, I have to roll back changesets x through c in reverse order. It’s a domino effect.
With Targeted Rollback, you can select the changeset you’d like to remove without rolling back any other changes that were deployed. It’s like a surgical strike.
Rollback is the only official technique for altering your
DATABASECHANGELOG table (the master table of changeset deployments which tracks which changesets have already been applied). If you need to make changes to this table, Targeted Rollback is an official and precise way to do it (and way better than manipulating that table by hand).
Here’s an example of when a Targeted Rollback is extremely useful. It’s common for many developers to work on a lot of different tables and parts of a database schema that relate to different elements or areas of the business. If you need to rollback one Localization table schema change but have since deployed dozens of additional changesets to the Products and Users tables, it’s quite handy to target the bad change in the Localization table and just roll it back. Targeted Rollback leaves the unrelated changes to the Products and Users tables in place and removes the changeset record from the
DATABASECHANGELOG table, too.
Here’s a general workflow a Liquibase Pro user would take to target a change to rollback:
liquibase <global parameters> rollbackOneChangesetSQL <command parameters>
liquibase <global parameters> rollbackOneChangeset <command parameters> --force
Much like git cherry-pick or git rebase, targeted rollback is powerful. Remember that with great rollback power comes great database responsibility. Targeted Rollback is a powerfully easy way to really mess up objects in your database, so it is important to look for potential unintended consequences, which is why it is a great idea to always execute
Since targeted rollbacks, like all rollbacks, can be detrimental when misused, we added some guardrails.
--force flag is required to execute the
rollbackOneChangeset command, serving as a clear indicator that you wish to proceed. Think of this as a safety on a firearm. Just pulling the trigger isn’t enough, you have to disable the safety and then pull the trigger.
The helper command,
rollbackonechangesetSQL allows you to inspect the rollback SQL before you execute rollbackOneChangeset. We’ve documented this in the suggested workflow so that it’s easier to remember to use this helper command.
What if I want to roll back three specific changesets that aren’t grouped by a tag? You would need to run rollbackOneChangeset three times. You could not pass a file of three specially formatted changeset identifiers or three sets of changeset params, etc.
Can I delete or comment out the changeset that I specify with the targeted rollback feature? No. We are not yet offering the capability to automatically delete or comment out changesets in this release. If you would like these removed so they never deploy again, or to hand-edit them to make changes for their next deployment, you can do so manually in the changelog file.
Download the latest version of Liquibase (3.8.6) and sign up for a free license key. Once you sign up, you’ll get a Pro license key immediately. Once you’ve set up the key, check out our documentation for details on how the new targeted rollbacks work. Let us know if you run into any unexpected issues.