Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Liquibase - Multi-line SQL rollback using rollbackSplitStatements and rollbackEndDelimiter

In 3.5.2 rollbackSplitStatements and rollbackEndDelimiter were introduced to allow us to write our rollback statements in multiple lines and not have to tag every line with "--rollback" or at least I am assuming that from the PR (https://github.com/liquibase/liquibase/pull/334).

But I have not see any documentation on how to use it.

As an example:

--changeset auth:1.1 rollbackSplitStatements:false rollbackEndDelimiter:/
DELETE FROM my_table WHERE id = 3;
--rollback
insert into my_table values (
  3,
  'firstname',
  'lastname'
);
/

This throws an error during migrate:
Unexpected error running Liquibase: ERROR: syntax error at or near "/"

Alternatively this does not throw an error but does execute the statement under "--rollback" during migrate:

--changeset auth:1.1 
DELETE FROM my_table WHERE id = 3;
--rollback rollbackSplitStatements:false rollbackEndDelimiter:/
insert into my_table values (
  3,
  'firstname',
  'lastname'
);
/
like image 765
user3006906 Avatar asked Nov 15 '17 20:11

user3006906


People also ask

Does Liquibase support rollback?

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.

What is SQL changeset?

The changeset tag is a unit of change that Liquibase executes on a database and which is used to group database Liquibase Change Types together. A list of changes created by multiple changesets are tracked in a changelog.

What is Liquibase formatted SQL?

Liquibase (LB) is an open source tool written in Java. It makes defining database changes easy, in a format that's familiar and comfortable to each user. Then, it automatically generates database-specific SQL for you. Database changes (every change is called changeset) are managed in files called changelogs.


1 Answers

I wish multi-line rollback worked the way you presented. I am using 3.5.3, still had to put --rollback in front of each line as

--changeset auth:1.1 rollbackSplitStatements:false
DELETE FROM my_table WHERE id = 3;
--rollback insert into my_table values (
--rollback  3,
--rollback  'firstname',
--rollback  'lastname'
--rollback);

In fact, the rollbackSplitStatements setting does not seem to make any difference in my tests.

like image 68
Bo Guo Avatar answered Sep 22 '22 12:09

Bo Guo