Hi Guys! I am back with a new blog post titled “Power of Liquibase”. This post is meant for those who have a basic idea of using Liquibase. As we know that Liquibase is used to keep a track of the changes that we apply to the database. It not only keeps a snapshot of the database state but also provides various other features. Here is a standard migrator runner given on Liquibase Documentation Page:
java -jar liquibase.jar --driver=com.mysql.jdbc.Driver \ --classpath=mysql-connector-java-5.1.22.jar \ --changeLogFile=changeLogFile.xml \ --url="jdbc:mysql://localhost:3306/dbName" \ --username=username \ --password=password \ update
We need to provide the changLog file path,database connector jar file,driverName, db url, username and password before executing the script.
Some of the advanced features of liquibase are:
Contexts : You can think of “Context” as a group name/category which is used to differentiate different changesets . Let’s say, you want to execute some changesets against your local database and some on your server database. So, in that case you can tag some changesets to run locally and some on the server’s database.
<changeSet id="SomeId" author="Author Name" context="prod"> ......... ............. </changeSet>
The string “prod” is not pre-defined, you can choose any string you like. Now when you execute a changeset using the Standard command-line script you can specify a set of contexts(–contexts = prod,test) which will run changesets specified in the script and also those changeset which are not tagged using context tag.
Pre-conditions : It is just like an if condition used in programming language. You can associate a pre-condition with a changelog or changeset. The execution of a changeset would depend on the precondition specified. The changeset is executed if the precondition becomes true. If the precondition becomes false, then the current changeset is skipped and continues with next one, or the whole execution is halted depending on “onFail” tag.
<changeSet id="1" author="bob"> <preConditions onFail="MARK_RAN"> <sqlCheck expectedResult="0">select count(*) from oldtable</sqlCheck> </preConditions> ... ........ .............. </changeSet>
The onFail tag takes four pre-defined values:
- HALT : It terminates the execution of change log.
- CONTINUE : It skips the current changeset and continues with the next one. On the next run, it attempts to execute the changeset again.
- MARK_RAN : Marks the changeset as ran and continues with the next one.
Comparing Databases : We can also do the comparison between 2 databases. To compare the 2 databases we use the following:
java -jar liquibase.jar --driver=com.mysql.jdbc.Driver \ --classpath=mysql-connector-java-5.1.22.jar \ --url=jdbc:mysql://localhost:3306/database1 \ --username=username \ --password=password \ diffChangeLog \ --referenceUrl=jdbc:mysql://localhost:3306/database2 \ --referenceUsername=username \ --referencePassword=password
It will generate a changeLog by checking what is missing in the target database as compared to the referenced database. The changeLog generates a set of changes that is required to update the target database.