Liquibase Auto Rollback
Liquibase supports Change Types that automatically create rollback statements when you run rollback commands and Change Types that don’t include an automatic rollback. If you want to use the rollback functionality or override the default generated rollback statement, you need to specify the <rollback>
tag within the needed changeset.
Formatted SQL
Liquibase does not support an automatic rollback for formatted SQL changesets. You should add custom rollback statements to the formatted SQL changesets if you want to use rollback
commands.
-- liquibase formatted sql
-- changeset liquibaseuser:1
create table example1 ( id int primary key, name varchar(255) );
-- rollback drop table example1;
-- changeset liquibaseuser:2
insert into example1 values ('1','The First', 'Country')
insert into example1 values ('1','The Second', 'Country2')
-- rollback delete from example1 where id='1'
Using the <rollback>
tag
The <rollback>
tag describes how to roll back a change using SQL statements, Change Types, or a reference to a previous changeset.
You can have plain SQL in the content part of the <rollback>
element. Liquibase treats the text in the element as the <sql>
Change Type with stripComments
set to true
, splitStatements
set to true
, and endDelimiter
set to ;
. For more details, see the XML example from the sql documentation.
<changeSet id="1" author="bob">
<createTable tableName="testTable">
<rollback>
drop table testTable
</rollback>
</changeSet>
Also, you can have any Change Type in the <rollback>
element:
<changeSet id="1" author="bob">
<createTable tableName="testTable">
<rollback>
<dropTable tableName="testTable"/>
</rollback>
</changeSet>
The following example shows how you can refer a <rollback>
element to another changeset. To roll back the changeset with id="2"
, apply the changeset with id="1"
:
<changeSet id="2" author="bob">
<dropTable tableName="testTable"/>
<rollback changeSetId="1" changeSetAuthor="bob"/>
</changeSet>
To reference the changeset that originally created a statement, use the <rollback>
tag with changeRollback2-create
:
<changeSet id="changeRollback2-drop" author="liquibase">
<dropTable tableName="changeRollback2"/>
<rollback changeSetId="changeRollback2-create" changeSetAuthor="liquibase"/>
</changeSet>
If you do not want to revert a change in a rollback mode, use an empty rollback tag:
<changeSet id="3" author="liquibaseuser">
<createTable tableName="changeRollback">
<column name="id" type="int"/>
</createTable>
<rollback/>
</changeSet>
Using Change Types and auto rollback
You can use an automatic rollback with the XML, JSON, and YAML changelogs.
Change Type | Supported |
---|---|
addAutoIncrement
|
Not Supported |
addCheckConstraint
|
Supported |
addColumn
|
Supported |
addDefaultValue
|
Supported |
addForeignKeyConstraint
|
Supported |
addLookupTable
|
Supported |
addNotNullConstraint
|
Supported |
addPrimaryKey
|
Supported |
addUniqueConstraint
|
Supported |
alterSequence
|
Not Supported |
createFunction
|
Not Supported |
createIndex
|
Supported |
createPackage
|
Not Supported |
createPackageBody
|
Not Supported |
createProcedure
|
Not Supported |
createSequence
|
Supported |
createSynonym
|
Supported |
createTable
|
Supported |
createTrigger
|
Not Supported |
createView
|
Supported |
customChange
|
Not Supported |
delete
|
Not Supported |
disableCheckConstraint
|
Supported |
disableTrigger
|
Supported |
dropAllForeignKeyConstraints
|
Not Supported |
dropCheckConstraint
|
Not Supported |
dropColumn
|
Not Supported |
dropDefaultValue
|
Not Supported |
dropForeignKeyConstraint
|
Not Supported |
dropFunction
|
Not Supported |
dropIndexNot
|
Not Supported |
dropNotNullConstraint
|
Supported |
dropPackage
|
Not Supported |
dropPackageBody
|
Not Supported |
dropPrimaryKey
|
Not Supported |
dropProcedure
|
Not Supported |
dropSequence
|
Not Supported |
dropSynonym
|
Not Supported |
dropTable
|
Not Supported |
dropTrigger
|
Not Supported |
dropUniqueConstraint
|
Not Supported |
dropView
|
Not Supported |
empty
|
Not Supported |
enableCheckConstraint
|
Supported |
enableTrigger
|
Supported |
executeCommand
|
Not Supported |
insert
|
Not Supported |
loadData
|
Not Supported |
loadUpdateData
|
Not Supported |
markUnused
|
Not Supported |
mergeColumns
|
Not Supported |
modifyDataType
|
Not Supported |
output
|
Not Supported |
renameColumn
|
Supported |
renameSequence
|
Supported |
renameTable
|
Supported |
renameTrigger
|
Supported |
renameView
|
Supported |
setColumnRemarks
|
Not Supported |
setTableRemarks
|
Not Supported |
sql
|
Not Supported |
sqlFile
|
Not Supported |
stop
|
Not Supported |
tagDatabase
|
Supported |
update
|
Not Supported |