Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A transactional Rollback of a liquibase changeset

Tags:

liquibase

I am currently using liquibase with SQL based changeset, and most of them contain INSERT statements. According to the documentation this type of update operation does not produce, (by the tool) automatic rollback statements.

My question is (I might be missing something), since we can declare that a certain changeset can run in the context of a DB transaction, if an error occurs during applying the changeset, can the tool (liquibase) just issue a transaction roll back for this specific changeset?

My case is that currently all these scripts are part of a development process and these scripts are not yet final, meaning that someone changes the content, and we reply them from scratch. If in a 2000 line insert script there is a error in the SQL, I would like the tool to automatically rollback the currently transaction and not commit the changes in the DB.

Many thanks for any tips

like image 713
javapapo Avatar asked Mar 18 '14 11:03

javapapo


People also ask

How do I rollback a changeset in Liquibase?

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.

How do I rollback a changeset?

To roll back a changeset from Source Control ExplorerIn Source Control Explorer, select an item, open its shortcut menu, and choose Rollback. The items you select determine the scope that the rollback changes. In the Rollback dialog box, select Rollback changes from a range of changesets.

How does rollback work in Liquibase?

When you run rollback , Liquibase will roll back sequentially all the deployed changes until it reaches the tag row in the DATABASECHANGELOG table. For example, you can use the rollback command when you want to undo a series of changes made to your database related to a specific tag such as a numbered release.

Is Liquibase transactional?

Liquibase attempts to execute each changeset in a transaction that is committed at the end, or rolled back if there is an error. Some databases will auto-commit statements which interferes with this transaction setup and could lead to an unexpected database state.


1 Answers

During the update process, liquibase runs each changeSet in a transaction and rolls it back if there are any errors. So If you have a 2000 line insert with an error half way through it will fail as expected and roll back automatically.

What is not generated automatically is SQL to "roll back" (in this case delete) the inserts after they have been committed. If you specify a block in your changeSet, then after update successfully executes and been committed, you can later on run "liquibase rollback v2.3" and it will undo changes since the v2.3 tag. It cannot rely on the database rollback functionality because it is already committed.

It is probably a bit confusing since rollback in this case is different than the normal database use of the term "rollback" in the context of a transaction.

like image 187
Nathan Voxland Avatar answered Jan 01 '23 18:01

Nathan Voxland