Search code examples
liquibaserollback

Liquibase: Rollback for data modification/deletion


I have little experience with Liquibase so far, but I have read that you should always have a rollback strategy.

https://www.liquibase.org/get-started/best-practices

Always have a rollback plan
Write changesets in a way that they can be rolled back. For example, use a relevant change clause instead of using a custom tag. Include a clause whenever a change doesn’t support an out-of-box rollback. (e.g., sql, insert, etc). Learn more about rollbacks.

In case of needed data modification or when deleting (obsolete) data, I don't know how to handle this in a good way.
I found examples where database entries are deleted and the rollback definition contains two insert statements with fix data. But this is not a real world scenario. Here we possibly have tables with million of records.

Are there any best-practices regarding data manipulation / transformation / deletion, respectively the rollback strategy?
Should data manipulation / destructive operations generally be avoided at all costs?

Hint: I'm not talking about small applications, but enterprise solutions that are developed by many people in different teams and delivered to hundreds of customers. Only to give you a context.


Solution

  • There are various opinions about this but, in real life situations, I don't find rollback strategies very practical. You typically need roll-forward. With that said, liquibase was meant for structural (ddl) changes.

    For data, row data in particular in the millions, I would orchestrate this outside of liquibase as a part of your release plans. I have seen large propriatery bank software handle this by renaming/copying table to another table and then making the change.

    SO, if you have tableX, and you need to add a column. You can copy the table to tableX_old, modify tableX.

    If the migration succeeds, delete tableX_old. If not, rollback using tableX_old to restore your data.