Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tag a changeset in liquibase to rollback

I have configured the maven pluggin for liquibase as specified in maven configuration.
Now created a changeset like :-

<changeSet id="changeRollback" author="nvoxland">   <createTable tableName="changeRollback1">      <column name="id" type="int"/>   </createTable>   <rollback>      <dropTable tableName="changeRollback1"/>   </rollback> </changeSet> 

Created the sql to update DB using command line :- mvn liquibase:updateSQL

But just want to know how to rollback using a "rollbackTag" parameter. i.e. If run the command "mvn liquibase:rollbackSQL", what should be the value of "rollbackTag" parameter.

And is it possible to rollback using the changeset id ?

like image 772
Manu Avatar asked Jun 21 '12 05:06

Manu


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 do I tag a database using Liquibase?

To tag your current database state, release, or version, follow these steps: Step 1: Add the tagDatabase Change Type to your changeset with the tag attribute as it is shown in the examples. Step 2: Deploy your changeset by running the update command.


2 Answers

Rollback tags are designed to checkpoint your database's configuration.

The following commands will roll the database configuration back by 3 changesets and create a tag called "checkpoint":

mvn liquibase:rollback -Dliquibase.rollbackCount=3 mvn liquibase:tag -Dliquibase.tag=checkpoint 

You can now update the database, and at any stage rollback to that point using the rollback tag:

mvn liquibase:rollback -Dliquibase.rollbackTag=checkpoint 

or alternatively generate the rollback SQL:

mvn liquibase:rollbackSQL -Dliquibase.rollbackTag=checkpoint 

Revised example

I initially found it difficult to figure out how to configure the liquibase Maven plugin. Just in case it helps here's the example I've used.

The liquibase update is configured to run automatically, followed by tagging the database at the current Maven revision number.

<project>     <modelVersion>4.0.0</modelVersion>     <groupId>com.myspotontheweb.db</groupId>     <artifactId>liquibase-demo</artifactId>     <version>1.0-SNAPSHOT</version>     <properties>         <!-- Liquibase settings -->         <liquibase.url>jdbc:h2:target/db1/liquibaseTest;AUTO_SERVER=TRUE</liquibase.url>         <liquibase.driver>org.h2.Driver</liquibase.driver>         <liquibase.username>user</liquibase.username>         <liquibase.password>pass</liquibase.password>         <liquibase.changeLogFile>com/myspotontheweb/db/changelog/db-changelog-master.xml</liquibase.changeLogFile>         <liquibase.promptOnNonLocalDatabase>false</liquibase.promptOnNonLocalDatabase>     </properties>     <dependencies>         <dependency>             <groupId>com.h2database</groupId>             <artifactId>h2</artifactId>             <version>1.3.162</version>         </dependency>     </dependencies>     <profiles>         <profile>             <id>dbupdate</id>             <activation>                 <activeByDefault>true</activeByDefault>             </activation>             <build>                 <plugins>                     <plugin>                         <groupId>org.liquibase</groupId>                         <artifactId>liquibase-maven-plugin</artifactId>                         <version>2.0.2</version>                         <executions>                             <execution>                                 <phase>process-resources</phase>                                 <configuration>                                     <tag>${project.version}</tag>                                 </configuration>                                 <goals>                                     <goal>update</goal>                                     <goal>tag</goal>                                 </goals>                             </execution>                         </executions>                     </plugin>                 </plugins>             </build>         </profile>     </profiles> </project> 

Liquibase is now configured as part of the standard life-cycle so can be run as follows:

mvn clean compile 
like image 129
Mark O'Connor Avatar answered Sep 25 '22 13:09

Mark O'Connor


I personally prefer to put the tag as part of the changeset files, so if you have to rollback or delete all the records in the DATABASECHANGELOG you won't loose your tagging records.

<databaseChangeLog>     <changeSet id="001_create_tables" .../>     <changeSet id="002_alter_tables" .../>     <changeSet id="003_load_user_data" .../>      <!-- Also include the tagging itself as a changeSet... -->     <changeSet author="userId" id="tag_version_0_1_0">         <tagDatabase tag="version_0.1.0" />     </changeSet>     <!-- version 0.1.0 ends here -->  </databaseChangeLog> 
like image 21
kothvandir Avatar answered Sep 23 '22 13:09

kothvandir