Hey!
I did some research on multiple rollback statements for a single changeset, e.g. on this page of the LB docs: Liquibase Rollback Workflow
We are working with plain SQL, but the question would be the same for all other formats. In the documentation, we have the following example:
-- changeset liquibaseuser:2
insert into example1 values ('**1**','The First', 'Country')
insert into example1 values ('**2**','The Second', 'Country2')
-- rollback delete from example1 where id='**1**'
-- rollback delete from example1 where id='**2**'
Intuitively, I’d expect that the order of rollback statements should be reversed, because it may not always be possible to rollback the second change (e.g. a table insert) after the first change (e.g. the table creation).
Am I missing something?
Thanks in advance!
Yes, you’ll need to order the rollback statement in correct logical order. A create table will need to occur before DML can occur.
Thanks for your answer!
Does that mean that Liquibase reverses the order under the hood?
No, Liquibase will run the sql statements in the order that you provide.
Then I should provide an example to clarify what I’m failing to understand. Suppose we have the following changeset:
-- changeset JohnDoe:1
CREATE TABLE MY_TABLE (id VARCHAR);
ALTER TABLE MY_TABLE ADD my_column VARCHAR;
-- rollback DROP TABLE my_table;
-- rollback ALTER TABLE my_table DROP COLUMN my_column;
If I were to run a rollback, and Liquibase tried to rollback from top to bottom without reversing the order, the second rollback statement cannot succeed because the underlying table no longer exists after the first rollback statement.
That is why I would have expected that these rollback statements must either be specified in reverse order, or are reversed by Liquibase itself
Since you have to write your own custom rollback sql statements in sql format, you just decide what the rollback needs to do.
For your example there is no need to include the “alter table” statement in the rollback section since the first statement is dropping the table anyway.
Just because you have 2 sqls in your changeset doesn’t mean that you need to have 2 rollback sqls.
This clears it up, thank you again!
1 Like