In the previous blog, we learned about best practices for managing Liquibase changelogs. In this blog – “Working With LIquibase Changelogs” we will see the actual working of Liquibase changelogs and all we can implement using Liquibase Changelogs.
Let’s re-visit to see what are changelogs –
Liquibase Changelogs
Liquibase changelogs are a collection of database changes that can be executed on a database to bring it up to the desired state. They are used to manage the evolution of a database schema over time, hence allowing developers to make changes to the database in a controlled and predictable manner.
A Liquibase changelog is typically a file that contains a list of changesets. A changeset is a group of database changes that are intended to be applied together as a unit. Each changeset includes the details of the changes to be made, such as the SQL statements to be executed or the database objects to be created or modified.
Moreover, liquibase allows developers to define their database changes in a database-agnostic format, so that the exact changelogs can be used to update different database types, such as MySQL, Oracle, or PostgreSQL. This makes it easier to support multiple database platforms and automate the process of applying changes to a database.
Furthermore, Liquibase also tracks which changesets have been applied to a database. As a result, they can be rolled back or re-applied if necessary. This helps to ensure that the database remains in a consistent state, even if changes are made or undone.
Types of Liquibase Changelogs
XML –
A Liquibase XML changelog is a file that contains a list of database changes written in XML format. Each changeset in the changelog is represented as an XML element. Further, the details of the changes to be made are specified using attributes and nested elements as shown below –
Similarly, here is an example of a simple Liquibase XML changelog that creates a table and inserts a row into it:
<?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"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<changeSet id="create-table" author="liquibase">
<createTable tableName="my_table">
<column name="id" type="int" autoIncrement="true"/>
<column name="name" type="varchar(255)"/>
<column name="description" type="varchar(255)"/>
</createTable>
</changeSet>
<changeSet id="insert-row" author="liquibase">
<insert tableName="my_table">
<column name="name" value="Example name"/>
<column name="description" value="Example description"/>
</insert>
</changeSet>
</databaseChangeLog>
SQL –
A Liquibase SQL changelog is a file that contains a list of database changes written in the SQL language. Additionally, Each changeset in the changelog is represented as a block of SQL statements.
Similarly, here is an example of a simple Liquibase SQL changelog that creates a table and inserts a row into it:
--liquibase formatted sql
--changeset liquibase:create-table
CREATE TABLE my_table (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
description VARCHAR(255)
);
--changeset liquibase:insert-row
INSERT INTO my_table (name, description)
VALUES ('Example name', 'Example description');
JSON –
A Liquibase JSON changelog is a file that contains a list of database changes written in JSON format. Each changeset in the changelog is represented as a JSON object. Further, the details of the changes to be made are specified using properties and nested objects.
Similarly, here is an example of a simple Liquibase JSON changelog that creates a table and inserts a row into it:
{
"databaseChangeLog": {
"changeSet": [
{
"id": "create-table",
"author": "liquibase",
"createTable": {
"tableName": "my_table",
"column": [
{
"name": "id",
"type": "int",
"autoIncrement": true
},
{
"name": "name",
"type": "varchar(255)"
},
{
"name": "description",
"type": "varchar(255)"
}
]
}
},
{
"id": "insert-row",
"author": "liquibase",
"insert": {
"tableName": "my_table",
"column": [
{
"name": "name",
"value": "Example name"
},
{
"name": "description",
"value": "Example description"
}
]
}
}
]
}
}
YAML –
A Liquibase YAML changelog is a file that contains a list of database changes written in the YAML format. Each changeset in the changelog is represented as a YAML object. Further, the details of the changes to be made are specified using properties and nested objects.
Similarly, here is an example of a simple Liquibase YAML changelog that creates a table and inserts a row into it:
databaseChangeLog:
changeSet:
- id: create-table
author: liquibase
createTable:
tableName: my_table
column:
- name: id
type: int
autoIncrement: true
- name: name
type: varchar(255)
- name: description
type: varchar(255)
- id: insert-row
author: liquibase
insert:
tableName: my_table
column:
- name: name
value: Example name
- name: description
value: Example description
Types of changes that can be made using Liquibase Changelogs with examples
Creating and modifying tables
createTable
: Creates a new table in the database.
<createTable tableName="customers">
<column name="id" type="int" autoIncrement="true">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="name" type="varchar(255)"/>
<column name="email" type="varchar(255)"/>
<column name="created_at" type="timestamp" defaultValueComputed="CURRENT_TIMESTAMP"/>
</createTable>
addColumn
: Adds a new column to an existing table.
<addColumn tableName="customers">
<column name="address" type="varchar(255)"/>
</addColumn>
modifyDataType
: Modifies the data type of a column.
<modifyDataType tableName="customers" columnName="name" newDataType="varchar(100)"/>
insert
: Inserts new rows into a table.
<insert tableName="customers">
<column name="name" value="John Smith"/>
<column name="email" value="john@example.com"/>
</insert>
update
: Updates existing rows in a table.
<update tableName="customers" where="email = 'john@example.com'">
<column name="name" value="John Doe"/>
</update>
delete
: Deletes rows from a table.
<delete tableName="customers" where="email = 'john@example.com'"/>
Creating and Modifying Views
createView
: Creates a new view in the database.
<createView viewName="customer_names">
SELECT name FROM customers;
</createView>
dropView
: Drops an existing view from the database
<dropView viewName="customer_names"/>
Similarly, we can use changelogs for Stored Procedures.
Creating And Modifying Stored Procedures
createProcedure
: Creates a new stored procedure in the database
<createProcedure procedureName="get_customer_by_id">
CREATE PROCEDURE get_customer_by_id(IN customer_id INT)
BEGIN
SELECT * FROM customers WHERE id = customer_id;
END
</createProcedure>
dropProcedure
: Drops an existing stored procedure from the database.
<dropProcedure procedureName="get_customer_by_id"/>
Similarly, we can use changelogs for Triggers.
Creating And Modifying Triggers
createTrigger
: Creates a new trigger in the database
<createTrigger triggerName="customer_insert_trigger" beforeInsert="true" tableName="customers">
Conclusion
After knowing about Liquibase Changelogs we can conclude that Liquibase changelogs are very important and can be used to create and modify your database without much hassle. Moreover, you don’t have to spend a lot of money and time updating your schema. Liquibase is the simplest way out to keep a track of your database updates as well as to make schema changes and updates.
Interested in Tech blogs? Please visit Knoldus Blogs.
HAPPY LEARNING!!