Using Liquibase MongoDB Pro with Amazon DocumentDB
You can use Liquibase Pro with Amazon DocumentDB, which is a NoSQL database that offers partial MongoDB compatibility. You can use most of the same drivers, application code, and tools in Amazon DocumentDB as you do in MongoDB. There are some functional differences between MongoDB and Amazon DocumentDB, but you can configure your Liquibase project to work with DocumentDB with only a few tweaks.
Amazon DocumentDB emulates the MongoDB API while running over Amazon Aurora on the back-end. Your Amazon DocumentDB database contains one or more clusters containing your database instance. Clusters are deployed within an Amazon Virtual Private Cloud (Amazon VPC) environment.
You need a valid Liquibase Pro license key to use Liquibase with Amazon DocumentDB through the MongoDB Pro Extension. If you want to use Liquibase Pro with MongoDB instead of Amazon DocumentDB, see Using Liquibase MongoDB Pro with MongoDB Community and Enterprise Server.
Verified database versions
Amazon DocumentDB versions do not correspond directly to MongoDB versions. Liquibase has been tested on the following Amazon DocumentDB version:
- 4.0
For more information on Liquibase Pro and MongoDB version requirements, see Verified database versions.
Prerequisites
- Introduction to Liquibase: Dive into Liquibase concepts.
- Install Liquibase: Download Liquibase on your machine.
- Ensure Java is installed: Liquibase requires Java to run. If you used the Liquibase Installer, Java is included automatically. Otherwise, you must install Java manually.
- Get Started with Liquibase: Learn how to use Liquibase with an example database.
- Design Your Liquibase Project: Create a new Liquibase project folder and organize your changelogs.
- How to Apply Your Liquibase Pro License Key: If you use Liquibase Pro, activate your license.
Install drivers
CLI users
To use Liquibase and Amazon DocumentDB (with MongoDB compatibility), you must download the JAR file that contains the Liquibase MongoDB Pro extension and the JDBC drivers.
liquibase/lib
directory.
Maven users
To use Liquibase with Maven, pom.xml
file. Using this information, Maven automatically downloads the driver JAR from Maven Central when you build your project.
<dependency>
<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-commercial-mongodb</artifactId>
<version>1.5.0</version>
</dependency>
Implement Amazon DocumentDB
- Ensure that your desired Role Based Access Controls (RBAC) are established before continuing.
- Download and Install mongosh if it is not already installed on your machine.
- Download Java 11. The MongoDB Pro Extension requires it.
- Set the
supportsValidator
argument tofalse
in one of the following ways:- Command line:
--supports-validator=false
liquibase.properties
file:liquibase.mongodb.supportsValidator: false
- Environment variable:
LIQUIBASE_MONGODB_SUPPORTS_VALIDATOR=false
- Command line:
- Set the
retryWrites
argument tofalse
in one of the following ways:- Command line:
--retry-writes=false
liquibase.properties
file:liquibase.mongodb.retryWrites: false
- Environment variable:
LIQUIBASE_MONGODB_RETRY_WRITES=false
- JDBC URL:
&retryWrites=false
. See the following section for examples of full URLs.
- Command line:
- (Optional) If you are using TLS/SSL, configure the TLS/SSL encrypted connection for Amazon DocumentDB. For a step-by-step guide, see the below drop down guide
Note: mongosh
is mandatory to use MongoDB with Liquibase Pro and it must be accessible to Liquibase. We recommend that mongosh is in the system PATH
environment variable. If it is not, that location of mongosh must be manually specified in the liquibase.mongosh.conf
file.
Tip: Java 11 may already be present on your machine if you used the installer to install Liquibase. We recommend installing Liquibase with Java 11 with the installer asset available on GitHub.
To use Liquibase with Amazon DocumentDB successfully, you must utilize the MongoDB Pro extension to create a connection between the two. If you want to use Amazon DocumentDB with TLS enabled, follow these instructions:
- Follow the AWS instruction Connecting to an Amazon DocumentDB Cluster from Outside an Amazon VPC if needed to connect from outside an Amazon Virtual Private Cloud (VPC).
- Create Java Truststore. Items 1 and 2 in Connecting Programmatically to Amazon DocumentDB § Connecting with TLS Enabled (for Java snippet)
- Add Amazon DocumentDB credentials (
liquibase.command.username
,liquibase.command.password
) to theliqubase.properties
file, environment variables, or command line. - Specify the
supportsValidator
property: - Specify a connection string:
- Add information about Truststore (created on the second step) to environment variables before running Liquibase commands:
liquibase.mongodb.supportsValidator: false
liquibase.command.url: mongodb://localhost:27017/my_dbname?directConnection=true&serverSelectionTimeoutMS=2000&tls=true&tlsAllowInvalidHostnames=true&retryWrites=false&tlsCAFile=PATH_TO_CAFILE/global-bundle.pem
Note: The location localhost:27017
is used from the SSH tunnel (created on the first step)
For connection from outside an Amazon VPC, it should contain attributes tls=true&tlsAllowInvalidHostnames=true&retryWrites=false&tlsCAFile=PATH_TO_CAFILE/rds-combined-ca-bundle.pem
On Linux:
export JAVA_OPTS="-Djavax.net.ssl.trustStore=PATH_TO_TRUSTSTORE/rds-truststore.jks -Djavax.net.ssl.trustStorePassword=PASSWORD"
On Windows:
set JAVA_OPTS="-Djavax.net.ssl.trustStore=PATH_TO_TRUSTSTORE\\rds-truststore.jks -Djavax.net.ssl.trustStorePassword=PASSWORD"
Configure connection
- Ensure your Amazon DocumentDB database is configured. For more information, see AWS Documentation: Get Started with Amazon DocumentDB.
-
Ensure your Liquibase Pro license key is specified. For example, in a
liquibase.properties
file (defaults file):licenseKey: zQl8kNZjZgSp9LvqWQFAtGwiHrpg97UyAfQrNSiJQBCDH8FQPdDzANUpIe4Bj3CZA2IXgDBaoYZFvMw0E/s4JcECB3/A6jO+...
- Specify the database URL in the
liquibase.properties
file (defaults file), along with other properties you want to set a default value for. Liquibase does not parse the URL. You can either specify the full database connection string or specify the URL using your database's standard connection format:
TLS on:
url: mongodb://localhost:27070/lbcat?tls=true&tlsAllowInvalidHostnames=true&retryWrites=false&tlsCAFile=rds-combined-ca-bundle.pem
TLS off:
url: mongodb://localhost:27069/lbcat?tls=false&retryWrites=false
Note: If you set retryWrites
to false
anywhere in your Liquibase project and to true
elsewhere in your project—in the CLI, in your liquibase.properties
file, as an environment variable, in the url
argument, and so on—the value of false
will always take priority, regardless of where you set it.
Tip: To apply a Liquibase Pro key to your project, add the following property to the Liquibase properties file: licenseKey: <paste code here>
Test connection
-
Create a text file called
changelog
(.js
,.yaml
,.json
, or.xml
) in your project directory and add a changeset. - Native MongoDB Shell (
mongosh
) scripts in MongoDB Query Language (MQL): let developers use Liquibase without modifying existing MQL scripts, which may be JavaScript (.js
) files. - Formatted Mongo changelogs (MongoDB Pro 1.3.0+): add Liquibase changeset metadata to your MQL scripts to use features like rollback, contexts, labels, and the
include
andincludeAll
tags. These must be saved as.js
files. - YAML, JSON, and XML modeled changelogs: specify changes for Liquibase to deploy without the need for MQL scripts. However, you can still deploy MQL scripts in YAML, JSON, and XML changelogs by using the
mongo
andmongoFile
Change Types. Using these Change Types requires you to specifymongosh
as the value of therunWith
attribute for allmongo
andmongoFile
changesets. - Navigate to your project folder in the CLI and run the Liquibase
status
command to see whether the connection is successful: - Then execute these changes to your database with the
update
command: - From a database UI tool, ensure that your database contains the
myCollection
object you added along with the DATABASECHANGELOG collection and DATABASECHANGELOGLOCK collection.
You can write Liquibase changelogs in the MongoDB Pro extension in three ways:
The MongoDB Pro extension lets you use MongoDB's native language of MongoDB Query Language (MQL), which you may be storing in JavaScript files, in Liquibase changesets and Change Types. This is possible because MongoDB Shell (mongosh
) is compatible with Liquibase Pro. For more information, see MongoDB: Write Scripts. Example syntax:
db.createCollection('customers');
With the Liquibase MongoDB Pro extension 1.3.0+, you can use a formatted Mongo changelog for MongoDB Pro, similar to a formatted SQL changelog. This lets you use mongosh
scripts written in MongoDB Query Language (MQL) directly in Liquibase while also having access to all Liquibase features.
All Liquibase formatted Mongo changelogs must use the file extension .JS
or .js
(JavaScript). They must also begin with a changelog header: // liquibase formatted mongodb
. Your changesets must each specify runWith:mongosh
. For example:
// liquibase formatted mongodb
// changeset your.name:1 labels:example-label context:example-context runWith:mongosh
// comment: example comment
db.createCollection('customers');
// rollback db = db.person.drop()
databaseChangeLog:
- changeSet:
id: 1
author: your.name
runWith: mongosh
changes:
- mongo:
mongo: "db.createCollection('person')"
dbms: mongodb
- rollback:
mongo:
mongo: "db.person.drop()"
- changeSet:
id: 2
author: your.name
runWith: mongosh
changes:
- mongoFile:
path: "scriptFile.txt"
dbms: mongodb
relativeToChangelogFile: "true"
- rollback:
mongo:
mongo: "db.company.drop()"
{
"databaseChangeLog": [
{
"changeSet": {
"id": "1",
"author": "your.name",
"runWith": "mongosh",
"changes": [
{
"mongo": {
"dbms": "mongodb",
"mongo": "db.createCollection('person')"
}
}
],
"rollback": [
{
"mongo": {
"mongo": "db.person.drop()"
}
}
]
}
},
{
"changeSet": {
"id": "2",
"author": "your.name",
"runWith": "mongosh",
"changes": [
{
"mongoFile": {
"dbms": "mongodb",
"path": "scriptFile.txt",
"relativeToChangelogFile": true
}
}
],
"rollback": [
{
"mongo": {
"mongo": "db.company.drop()"
}
}
]
}
}
]
}
The Liquibase MongoDB Pro extension uses a unique mongodb-pro
XML namespace and XSD files in the changelog header. However, the ext
prefix used with other extensions is backwards-compatible:
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongodb-pro="http://www.liquibase.org/xml/ns/pro-mongodb"
xmlns:mongodb="http://www.liquibase.org/xml/ns/mongodb"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
http://www.liquibase.org/xml/ns/pro-mongodb http://www.liquibase.org/xml/ns/pro-mongodb/liquibase-pro-mongodb-latest.xsd
http://www.liquibase.org/xml/ns/mongodb http://www.liquibase.org/xml/ns/mongodb/liquibase-mongodb-latest.xsd">
<changeSet id="1" author="your.name" runWith="mongosh">
<mongodb-pro:mongo dbms="mongodb">
db.createCollection('person')
</mongodb-pro:mongo>
<rollback>
<mongodb-pro:mongo>
db.person.drop()
</mongodb-pro:mongo>
</rollback>
</changeSet>
<changeSet id="2" author="your.name" runWith="mongosh">
<mongodb-pro:mongoFile dbms="mongodb" path="scriptFile.txt" relativeToChangelogFile="true"/>
<rollback>
<mongodb-pro:mongo>
db.company.drop()
</mongodb-pro:mongo>
</rollback>
</changeSet>
</databaseChangeLog>
Tip: The preceding examples show only the mongo
and mongoFile
Change Types for Liquibase Pro. For a list of all Liquibase Pro and Liquibase Open Source Change Types for MongoDB, including Change Types that you can without the mongosh
native executor, see Liquibase Change Types for MongoDB.
Note: You can specify arguments in the CLI or keep them in the Liquibase properties file.
If your connection is successful, you'll see a message like this:
4 changesets have not been applied to <your_connection_url>
Liquibase command 'status' was executed successfully.
liquibase update --changelog-file=<changelog.xml>
If your update
is successful, Liquibase runs each changeset and displays a summary message ending with:
Liquibase: Update has been successful.
Liquibase command 'update' was executed successfully.
Tip: You can use MongoDB Compass to easily view collections in your database. For example, run the commands use myDatabase
and db.myCollection.find()
.
Now you're ready to start making deployments with Liquibase!