What is Liquibase?
Liquibase is an open-source database schema change management solution that enables you to manage revisions of your database changes easily. Liquibase makes it easy for anyone involved in the application release process to:
- Eliminate errors and delays when releasing databases.
- Deploy and roll back changes for specific versions without knowing what has already been deployed.
- Deploy database and application changes together so they always stay in sync.
Download Liquibase: Liquibase for Windows
For Non-spring Boot project, we need to make use of batch(.bat) file.
1. update.bat file
liquibase --driver=oracle.jdbc.driver.OracleDriver --url=jdbc:oracle:thin:@localhost:1521:orcl --changeLogFile=liquibase-changelogs.xml --username=system --password=root12345 update
Here --changeLogFile=liquibase-changelogs.xml
liquibase-changelogs.xml consists of all the changesets that we want to apply to our database.
For best practice, we don't add the changesets in a single file.
Instead, we will have a separate file for each changeset with the proper id and author name.
Here we have included the <includeAll> tag to mention the path where we are having all the changesets files. It can be any location within the project.
Suppose I have 3 SQL files with 3 changesets.
Explanation:
Line 1: --liquibase formatted sql
To make liquibase understand this is the SQL file that we have to use as a changeset.
Line 3: --changeset Aamir:1
The required changeset that we have to execute with author name as Aamir and id as 1.
Line 4-7: The standard SQL statement to create a table
Line 8: --rollback drop table person
This is the custom rollback that we have to create.
After --liquibase, we have to give the SQL statement that act as rollback.
After writing all the changeset we can execute the "update.bat" file from the command line/Terminal.
This will execute all the changeset SQL file inside the given folder.
So it will create a table "person" along with 2 other tables:
i) databasechangelog
ii) databaseChangeLogLock
2. rollback.bat file
Now after executing all the changesets file, you want to rollback the last change. Then we need to create a separate "rollback.bat" file.
liquibase --driver=oracle.jdbc.driver.OracleDriver --url=jdbc:oracle:thin:@localhost:1521:orcl --changeLogFile=liquibase-changelogs.xml --username=system --password=root12345 rollbackCount 1
Here, we are using "rollbackCount 1". This means it will rollback the last executed changeset.
It can be rollbackCount 3, which will rollback the last three changesets.
Like this, we can rollback as per our need.
3. rollbackTag
We can also assign a tag to our changesets and then can rollback based on that
liquibase --driver=oracle.jdbc.driver.OracleDriver --url=jdbc:oracle:thin:@localhost:1521:orcl --changeLogFile=liquibase-changelogs.xml --username=system --password=root12345 tag prev_version
Here we are giving "tag pre_version", so after all the changeset are executed, then at the last changesets, the tag column in the databaseChangeLog will get updated with "prev_version".
So next time, after you have executed more changeset and if you have any issues, you can rollback to prev_version.
4. rollbackTime
Similarly, we can also rollback based on the Time the changesets were executed.
In this databaseChangeLog, we have one column related to Time of execution.
5. update-sql
The
update-sql
command is a helper command that allows you to inspect the SQL Liquibase will run while using the update command.Uses
The update-sql
command is used when you want to inspect the raw SQL before running the update command, so you can correct any issues that may arise before running the command. Liquibase uses the raw SQL to apply database changes you have added to the changelog file.
When we run "update-sql" command, it will create a new file(path and name we can give) with the SQL that is going to be executed.