What is Liquibase?
First, Liquibase is an open-source database schema change management tool that makes it simple for you to handle database change revisions.
How Does Liquibase Work?
Regardless of your database platform, changes are defined in a platform-neutral language. In essence, you maintain a running list of modifications. And, Liquibase uses its execution engine to make those modifications for you. It requires the appropriate JDBC driver to update the database because it runs on Java. You must also have the most recent JRE installed, of course. You can run Liquibase manually or from any deployment pipeline because it operates from a shell or command line. To maintain consistency and prevent corruption due to wrongly modified changelogs, Liquibase tracks changes using its own tables in your schema. To prevent you from mistakenly executing two updates at once, it “locks” your database while operating.
Where can we use Liquibase?
- Database schema change using CD
- Controlling schema change versions
- Application and database modifications should be deployed simultaneously to ensure consistency.
Liquibase basics – changeLog files
The changeLog files are the foundation of Liquibase usage. A changelog file is an XML document that records every change that needs to be made to update the database. Tag is parsing when we run the Liquibase migrator, the databaseChangeLog>. We can add changeSet> tags to the databaseChangeLog> tag to organize database changes. The ‘id’ and ‘author’ attributes as well as the name of the changelog file classpath serve to identify each changeSet specifically.
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd"> </databaseChangeLog>
Updating the database
We will examine how to edit the database to add, update, and remove tables, columns, and data. Now that we have a simple changeLog file. By the principle of “one change per changeSet,” it is counseles to create a new changeSet for each distinct insert, update, and delete action. As a result, we use Liquibase to execute a version-based database migration while updating an existing database.
Manipulating database schema and tables
For the most part, you require some fundamental Data Definition Language (DDL) functions to define data structures in databases.
1) Create schema: Since Liquibase is planned to manage objects within the application’s schema, there is no “create schema” tag. However, we can incorporate the creation of a schema into our migration by using a unique SQL statement inside a “SQL” tag.
<changeSet author="asdf" id="1234"> <sql dbms="h2" endDelimiter=";"> CREATE SCHEMA schema </sql> </changeSet>
2) Create Table: The following code is added to the databaseChangeLog tag when a table is created:
<changeSet author="asdf" id="1234"> <createTable tableName="newTable"> <column type="INT" name="newColumn"/> </createTable> </changeSet>
3) Drop Table: We must mention the table’s name and the schema when deleting a table. We also remove all constraints referring to primary and unique keys in the drop table when cascadeConstraints are set to true. It will after delete the corresponding records in the child table or tables. The database will return an error and won’t drop the table if there is a referential integrity constraint but we don’t set the cascadeConstraints to true.
<changeSet author="asdf" id="1234"> <dropTable tableName="newTable" schemaName="public" cascadeConstraints="true"/> </changeSet>
4) Change existing data structure with alter table: The table can be changed by adding, renaming, and dropping columns as well as changing the data type. To change the table’s name, use the tag renameTable.
4) a. Rename Table: We must specify the new table name, the old table name, and the schema name inside the tag “renameTable”.
<changeSet author="asdf" id="1234"> <renameTable newTableName="newName" oldTableName="table" schemaName='schema'/> </changeSet>
4) b. Rename Column: A column’s data type, new column name, old column name, schema name, and table name must all be provided to rename a column.
<changeSet author="asdf" id="1234"> <renameColumn columnDataType="varchar(255)" newColumnName="newColumn" oldColumnName="column" schemaName="schema" tableName="table"/> </changeSet>
4) c. Add Column: The schema name, table name, and the name and type of the new column must be included in the inner tag <column> of the tag <addColumn>.
<changeSet author="asdf" id="1234"> <addColumn schemaName="schema" tableName="table"> <column name="newColumn" type="varchar(255)"/> </addColumn> </changeSet>
4) d. Drop column: Column name, table name, and schema name must all be specified to delete a column.
<changeSet author="asdf" id="1234"> <dropColumn columnName="column" tableName="table", schemaName="schema"/> </changeSet>
4) e. Modify the data type: We need the column name, new data type, schema name, and table name when changing a data type.
<changeSet author="asdf" id="1234"> <modifyDataType columnName="column" newDataType="int" schemaName="schema" tableName="table"/> </changeSet>
Liquibase with Maven
To configure and run Liquibase with Maven, we need to add the following configuration to our pom.xml file:
<dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> <version>x.x.x</version> </dependency>
Maven also enables us to automatically generate a changelog file from:
- already existing database
- the difference between the two databases
In conclusion, in this blog, we have learned about how can we manage databases with the help of Liquibase. 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/