/liquibase-mongodb

MongoDB extension for Liquibase

Primary LanguageJavaApache License 2.0Apache-2.0

Liquibase MongoDB Extension

Table of contents

  1. Introduction
  2. Implemented Changes
  3. Getting Started
  4. Running tests
  5. Integration
  6. Contributing
  7. License

Introduction

This is a Liquibase extension for MongoDB support.

It resulted as an alternative to existing MongoDB evolution tools.
Majority of them are basically wrappers over db.eval shell method that is deprecated staring from MongoDB 4.2.

In order to call specific mongo-java-driver specific methods, Liquibase turned to be the most feasible tool to extend as it allows to define change sets to fit driver methods arguments.

Implemented Changes:

A couple of Changes were implemented until identified that majority of of the operations can be achieved using db.runCommand() and db.adminCommand()

  • createCollection - Creates a collection with validator
  • createIndex - Creates an index for a collection
  • insertMany - Inserts multiple documents into a collection
  • insertOne - Inserts a Single Document into a collection
  • runCommand - Provides a helper to run specified database commands. This is the preferred method to issue database commands, as it provides a consistent interface between the shell and drivers
  • adminCommand - Provides a helper to run specified database commands against the admin database

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Prerequisites

mongo-java-driver:3.10.2

Installing

Running tests

Adjust connection string

Connection url can be adjusted here: db.connection.uri Integration tests are run by enabling run-its profile

Run integration tests

mvn clean install -Prun-its

Integration

Add dependency:

<dependency>
    <groupId>com.mastercard.liquibase</groupId>
    <artifactId>liquibase-mongodb</artifactId>
    <version>${liquibase-mongodb.version}</version>
</dependency>

Java call:

MongoConnection mongoConnection;
MongoExecutor mongoExecutor;
MongoLiquibaseDatabase database;

mongoConnection = new MongoConnection("mongodb://localhost:27017/test_db?socketTimeoutMS=100&connectTimeoutMS=100&serverSelectionTimeoutMS=100");

//Can be achieved by excluding the package to scan or pass package list via system.parameter
//ServiceLocator.getInstance().getPackages().remove("liquibase.executor");
//Another way is to register the executor against a Db

database = (MongoLiquibaseDatabase) DatabaseFactory.getInstance().findCorrectDatabaseImplementation(mongoConnection);
database.setConnection(mongoConnection);

mongoExecutor = new MongoExecutor();
mongoExecutor.setDatabase(database);

ExecutorService.getInstance().setExecutor(database, mongoExecutor);

 final Liquibase liquibase = new Liquibase("liquibase/ext/changelog.create-users.test.xml", new ClassLoaderResourceAccessor(), database);
 liquibase.update("{}");

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

License

This project is licensed under the Apache License Version 2.0 - see the LICENSE.md file for details