Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Liquibase 2.0 - Rolling Back LiquiBase Formatted SQL

Tags:

liquibase

In 2.0 a really nice freature was added called LiquiBased Formatted SQL. See this link for info http://blog.liquibase.org/2010/05/liquibase-formatted-sql.html.

When I try to add a rollback command to my script which in my case is dropping a view that was created LiquiBase throws an error. Here is my script.

--liquibase formatted sql

--changeset PeterPan:REQ111111
CREATE VIEW all_employees AS
SELECT *
FROM employee;


--rollback 
DROP VIEW all_employees;
--rollback 

I also tried this too

--liquibase formatted sql

--changeset PeterPan:REQ111111
CREATE VIEW all_employees AS
SELECT *
FROM employee;


--rollback 
DROP VIEW all_employees;

Here is my command line input and the output I get. Note I did the update which succeeds and then a rollback which is what fails. Also keep in mind the view gets dropped just the script throws an exception. If I remove my view from the build and run the same commands both update and rollback finish with success.

C:\>liquibase --driver=org.postgresql.Driver --classpath="C:/Program Files/Java/jdk1.6.0_23/lib/postgresql-9.0-801.jdbc4.jar" --url="jdbc:postgresql:/
/localhost:5432/test_db_build" --changeLogFile=C:/DbSource/Common/00_main_changelog.xml --username=postgres --password=password update
INFO 3/3/11 10:05 AM:liquibase: Successfully acquired change log lock
INFO 3/3/11 10:05 AM:liquibase: Reading from databasechangelog
INFO 3/3/11 10:05 AM:liquibase: Reading from databasechangelog
INFO 3/3/11 10:05 AM:liquibase: ChangeSet C:/DbSource/Tables/0001_CreateTableEmployee.xml::1::PeterPan ran successfully in 78ms
INFO 3/3/11 10:05 AM:liquibase: ChangeSet C:/DbSource/Tables/0002_CreateSchema-test.xml::2::PeterPan ran successfully in 15ms
INFO 3/3/11 10:05 AM:liquibase: ChangeSet C:/DbSource/Tables/0003_CreateTable-junk.xml::4::PeterPan ran successfully in 31ms
INFO 3/3/11 10:05 AM:liquibase: ChangeSet C:/DbSource/Tables/0004_CreateTable-test.fun_log.xml::4::PeterPan ran successfully in 156ms
INFO 3/3/11 10:05 AM:liquibase: ChangeSet C:/DbSource/Views/001_all_employees.sql::REQ111111::PeterPan ran successfully in 15ms
INFO 3/3/11 10:05 AM:liquibase: ChangeSet C:/DbSource/DML/0001_InsertInto-public.employee.xml::4::PeterPan ran successfully in 16ms
INFO 3/3/11 10:05 AM:liquibase: ChangeSet C:/DbSource/DML/0002_Truncate-public.employee.xml::4::PeterPan ran successfully in 282ms
INFO 3/3/11 10:05 AM:liquibase: ChangeSet C:/DbSource/DML/0003_InsertInto-public.employee.xml::7::PeterPan ran successfully in 16ms
INFO 3/3/11 10:05 AM:liquibase: ChangeSet C:/DbSource/DML/0004_InsertInto-public.employee.xml::7::PeterPan ran successfully in 33298ms
INFO 3/3/11 10:05 AM:liquibase: Successfully released change log lock
Liquibase Update Successful

C:\>liquibase --driver=org.postgresql.Driver --classpath="C:/Program Files/Java/jdk1.6.0_23/lib/postgresql-9.0-801.jdbc4.jar" --url="jdbc:postgresql:/
/localhost:5432/test_db_build" --changeLogFile=C:/DbSource/Common/00_main_changelog.xml --username=postgres --password=password rollbackCount 10
INFO 3/3/11 10:05 AM:liquibase: Successfully acquired change log lock
INFO 3/3/11 10:05 AM:liquibase: Reading from databasechangelog
INFO 3/3/11 10:05 AM:liquibase: Rolling Back Changeset:C:/DbSource/DML/0004_InsertInto-public.employee.xml::7::PeterPan::(Checksum: 3:a4b6d9d43a4175f753
f7c0a457d1ced1)
INFO 3/3/11 10:05 AM:liquibase: Rolling Back Changeset:C:/DbSource/DML/0003_InsertInto-public.employee.xml::7::PeterPan::(Checksum: 3:d7308afe0c1d4c8350
6a1ffed71a226c)
INFO 3/3/11 10:05 AM:liquibase: Rolling Back Changeset:C:/DbSource/DML/0002_Truncate-public.employee.xml::4::PeterPan::(Checksum: 3:0da476ec239364e44dda
d4f6f6da4321)
INFO 3/3/11 10:05 AM:liquibase: Rolling Back Changeset:C:/DbSource/DML/0001_InsertInto-public.employee.xml::4::PeterPan::(Checksum: 3:afdb713d4f13832efd
f0a0313ead4b84)
INFO 3/3/11 10:05 AM:liquibase: Rolling Back Changeset:C:/DbSource/Views/001_all_employees.sql::REQ111111::PeterPan::(Checksum: 3:3aee7ee7499252540b394
acb470a1ae1)
INFO 3/3/11 10:05 AM:liquibase: Successfully released change log lock
Liquibase Update Failed: No inverse to liquibase.change.core.RawSQLChange created
SEVERE 3/3/11 10:05 AM:liquibase: No inverse to liquibase.change.core.RawSQLChange created
liquibase.exception.RollbackFailedException: liquibase.exception.RollbackImpossibleException: No inverse to liquibase.change.core.RawSQLChange created

        at liquibase.changelog.ChangeSet.rollback(ChangeSet.java:401)
        at liquibase.changelog.visitor.RollbackVisitor.visit(RollbackVisitor.java:23)
        at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:58)
        at liquibase.Liquibase.rollback(Liquibase.java:250)
        at liquibase.integration.commandline.Main.doMigration(Main.java:710)
        at liquibase.integration.commandline.Main.main(Main.java:116)
Caused by: liquibase.exception.RollbackImpossibleException: No inverse to liquibase.change.core.RawSQLChange created
        at liquibase.change.AbstractChange.generateRollbackStatementsFromInverse(AbstractChange.java:145)
        at liquibase.change.AbstractChange.generateRollbackStatements(AbstractChange.java:115)
        at liquibase.database.AbstractDatabase.executeRollbackStatements(AbstractDatabase.java:1029)
        at liquibase.changelog.ChangeSet.rollback(ChangeSet.java:388)
        ... 5 more


For more information, use the --logLevel flag)

C:\>
like image 913
Kuberchaun Avatar asked Mar 03 '11 16:03

Kuberchaun


People also ask

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.

How do I run a SQL file in Liquibase?

To run the execute-sql command, specify the following parameters in the Liquibase properties file, environment variables, or the command prompt while running the command: URL, driver [optional], and user authentication information such as username and password. sql or sql-file parameter.

What is SQL changeset?

Applies to: SQL Server (all supported versions) - Windows only Azure SQL Managed Instance. A changeset is a collection of the pending changes on the master data.


1 Answers

Ok so it appears on the link I supplied the key to getting around this issue was this "add –rollback to the beginning of lines that denote the rollback sql".

So if I updated my script above to either this

--rollback DROP VIEW all_employees;

OR this format will work too.

--rollback DROP 
--rollback VIEW all_employees;

Be nice to have starting comment and ending to allow this type of entry. Maybe like

--StartRollback
DROP TABLE employee;
DROP TABLE pants;
--End Rollback

Here is a site that helped clarify a lot of this. http://forum.liquibase.org/#topic/49382000000028385

like image 190
Kuberchaun Avatar answered Sep 24 '22 12:09

Kuberchaun