How to use Liquibase with MongoDB?

liquibase
Reading Time: 2 minutes

Hey, tech geeks, so today we’ll be looking into how we can use Liquibase with MongoDB so that we can create a Liquibase project. So Liquibase is an open-source library that is used to track, manage and apply database schemas. It is an independent library.

Installing Liquibase on Linux

For installing Liquibase, Go to the official page of Liquibase to download that tar package. After downloading the tar package, extract the package content in the local directory.

After that, we have to add the Liquibase installation directory to the path of our system. For the same, use the below command:

export PATH=$PATH:<Liquibase extracted directory>

Now we have to download the MongoDB JDBC jar file.

After that, we have to download the Liquibase extension for MongoDB, and that can be found here. Now add those jar files into the extracted Liquibase installation directory, i.e. liquibase/lib.

Creating Liquibase project using MongoDB

Now go to a directory and make a folder with any name but I would prefer LiquibaseProject.

After that go to the directory that you have made and create a file with an XML extension. Open the XML file and paste the below code into that.

<?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:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
	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.9.0.xsd
		http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd
		http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.9.0.xsd">
</databaseChangeLog>

Now in the same directory, we have to define some properties in another file. so we have to create a file for properties with the .properties extension. And paste the below code into that file:

changelog-file: dbchangelog.xml
url: mongodb://hostname:27017/myDatabase
username: username
password: password

Now we have to add a changeset to the changelog. So what are changesets? Changesets are uniquely identified by author  id attributes. So we’ll be adding a new collection into the changesets as below:

<changeSet id="1" author="bob">
	<ext:createCollection collectionName="myCollection">
		<ext:options>
		{
		validator: {
			$jsonSchema: {
				bsonType: "object",
				required: ["name", "address"],
				properties: {
					name: {
					bsonType: "string",
					description: "The Name"
					},
			address: {
				bsonType: "string",
				description: "The Address"
						}
					}
				}
			},
			validationAction: "warn",
			validationLevel: "strict"
			}
			</ext:options>
		</ext:createCollection>
	</changeSet>

The above code is to be entered into the XML file so that a new collection is introduced with some parameters.

Now we have to navigate to the project directory through our terminal and update the database. For the same, we have to run a command which is given below:

liquibase update
How to use Liquibase with MongoDB?

Now when you go to the MongoDB Compass Community tool UI, you can check that a database has been created with the name myDatabase, and under that, a new collection has been introduced with myCollection.

So as we have seen that we can use Liquibase with any of our databases for managing revisions of our database schema scripts. It also supports various file formats. Liquibase uses scripts that are known as changesets as we have used one above. So if you want to manage your schema scripts in your databases, you can go for Liquibase.

knoldus

Written by 

Shubham Saini is a DevOps Engineer who loves to play with DevOps tools, Security methods and is also interested in Ethical Hacking & Cyber Security. He is a gamer also.

Leave a Reply