
Overview
Liquibase allows you to specify desired database changes using SQL and several other database-independent formats such as XML, YAML, and JSON. Developers can abstract database code to make the propagation of changes to different database types much more accessible.
Before you start, If you don’t have a Liquibase account, create your Liquibase Hub account first.
Download and extract Liquibase
Run the installer or extract the downloaded files.
Open a terminal to view your new directory:
Run the installer or extract the downloaded Liquibase files.
Configure Liquibase
Create a liquibase.properties text file to specify the driver classpath, URL, and user credentials for the database to retrieve.
We can take a look at the properties file of the Postgres Database example below:
changeLogFile:dbchangelog.xml
url: jdbc:postgresql://localhost:5432/mydatabase
username: postgres
password: password
classpath: postgresql-42.2.8.jar
liquibaseProLicenseKey: licensekey
liquibase.hub.ApiKey: APIkey
Take a snapshot of your existing database
Create a usable changelog to capture the current state of the database.
liquibase --changeLogFile=mydatabase_changelog.xml generateChangeLog
Let us take a look at an example:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.4.xsd
http://www.liquibase.org/xml/ns/pro
http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.5.xsd">
<changeSet author="lb-generated" id="1185214997195-1">
<createTable name="BONUS">
<column name="NAME" type="VARCHAR2(15)"/>
<column name="JOB" type="VARCHAR2(255)"/>
<column name="SAL" type="NUMBER(255)"/>
</createTable>
</changeSet>
<changeSet author="lb-generated" id="1185214997195-2">
<createTable name="DEPT">
<column name="DEPTNO" type="INTEGER"/>
<column name="DNAME" type="VARCHAR2(15)"/>
<column name="LOC" type="VARCHAR2(255)"/>
</createTable>
</changeSet>
<changeSet author="lb-generated" id="1185214997195-3">
<createView fullDefinition="false" viewName="myView2">SELECT
"DEPT".DEPTNO,
"DEPT".DNAME
FROM "DEPT";</createView>
</changeSet>
<changeSet author="lb-generated" id="1185214997195-4">
<pro:createFunction functionName="myFunction"
path="objects/function/myFunction.sql" relativeToChangelogFile="true"/>
</changeSet>
</databaseChangeLog>
Create your first database schema change
You can start modifying the database by creating the first set of changes in the changelog dbchangelog.xml as follows:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.4.xsd
http://www.liquibase.org/xml/ns/pro
http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.5.xsd">
<changeSet author="BobR" id="myIDNumber123">
<createTable tableName="actor">
<column autoIncrement="true" name="id" type="INTEGER">
<constraints nullable="false" primaryKey="true"
primaryKeyName="actor_pkey"/>
</column>
<column name="firstname" type="VARCHAR(255)"/>
<column name="lastname" type="VARCHAR(255)"/>
<column name="twitter" type="VARCHAR(15)"/>
</createTable>
</changeSet>
</databaseChangeLog>
Register your changelog
Register the changelog with the following command so that you can view the details in Liquibase Hub.
liquibase-4.5.0$ liquibase registerChangeLog
Deploy your database change
Now you can deploy your database changes by running the update command as follows:
liquibase-4.5.0$ liquibase update
If all goes well, you will see the following output:
Liquibase: Update has been successful.
You can now automatically undo the last database change by running the rollback command as follows:
liquibase-4.5.0$ liquibase rollbackCount 1
If all goes well, you will see the following output:
Rolling Back Changeset:dbchangelog.xml::myIDNumber123::BobR
Liquibase: Rollback has been successful.
View your project
Register the changelog with the following command so that you can view the details in Liquibase Hub.
liquibase-4.5.0$ liquibase registerChangeLog
Set up your dashboard
Create a free account on Liquibase Hub to see database version information about changes occurring in each environment in one place.
When you create an account, you will receive an API key that you can use to connect your changelog to your dashboard. Once connected, you can see in real-time which changes succeeded and which failed in which environments.
Conclusion
In conclusion, in this blog, we have learned about Liquibase installation and setup. I will be covering more topics on Liquibase in my future blogs, stay connected. Happy learning 🙂
For more, you can refer to the Liquibase documentation: https://docs.liquibase.com/home.html
For a more technical blog, you can refer to the Knoldus blog: https://blog.knoldus.com/