Database CI/CD and Schema Migration with MongoDB

Estimated: 20 mins
Database CI/CD and Schema Migration with MongoDB

A series of articles about Database CI/CD and Schema Migration with MongoDB.


MongoDB is a document database used to build highly available and scalable internet applications.

This tutorial will guide you step-by-step to set up database change management for MongoDB (Cloud and Server) in Bytebase. With Bytebase, a team can have a formalized review and rollout process to make MongoDB data change.

You’ll have a GUI and the full change history. You can use Bytebase free version to finish the tutorial.

Features included

  • Change Workflow
  • Change History

Prerequisites

Before you start this tutorial, make sure:

Step 1 - Start Bytebase in Docker

  1. Make sure your docker daemon is running, and start the Bytebase docker container.

    docker run --rm --init \
      --name bytebase \
      --publish 8080:8080 \
      --volume ~/.bytebase/data:/var/opt/bytebase \
      bytebase/bytebase:2.14.1
  2. Bytebase is running successfully in Docker, and you can visit it via localhost:8080. docker

  3. Visit localhost:8080 in your browser. Register the first admin account which will be granted Workspace Admin. register

Step 2 - Add MongoDB in Bytebase

In Bytebase, ​​an Instance could be your on-premises MySQL instance, an AWS RDS instance etc, in this tutorial, a MongoDB Atlas account or a MongoDB Server instance.

  1. Visit localhost:8080 and log in as Workspace Admin. login

  2. Click Add Instance. bb-add-instance

  3. Add a MongoDB instance and click Create.

Pay attention to some fields:

Environment: choose Test, if you choose Prod,  issues will wait for approval by default. In this tutorial, we try to keep it simple. However, it’s all configurable.

Cloud (MongoDB Atlas)

If you use Cloud version.

Go to Atlas MongoDB Security > Network Access and add IP allowing access from Anywhere.

Go to Atlas MongoDB Deployment > Database, and click Connect. atlas-connect

Click Connect to your application.

atlas-connect-to-cluster

Find this string cluster0.8bxxxbz.mongodb.net. Go back to Bytebase, put the string into Host or Socket, and leave Port empty.

Choose mongodb+srv:// as Connection String Schema.

Server

If you run MongoDB server, fill in Host or Socket with host.docker.internal.

Choose mongodb:// as Connection String Schema.

Step 3 - Create a project with MongoDB instance

In Bytebase, Project is the container to group logically related Databases, Issues and Users together, which is similar to the project concept in other dev tools such as Jira, GitLab. So before you deal with the database, a project must be created.

  1. After the instance is created, click Projects on the top bar.

  2. Click New Project to create a new project TestMongo, key is TMG, mode is standard. Click Create.

Step 4 - Create a database in MongoDB via Bytebase

In Bytebase, a Database is the one created by 'CREATE DATABASE xxx'. A database always belongs to a single Project. Issue represents a specific collaboration activity between Developer and DBA such as creating a database, altering a schema. It's similar to the issue concept in other issue management tools.

  1. Go into the project TestMongo. Click New DB on the project top bar. bb-project-new-db

  2. Fill the form with New Database Name - dbdemo, New Collection Name - cdemo, Environment - Test, and your created instance. Click Create. bb-create-db

  3. It will create an issue automatically. Because it’s for Test environment, the issue will run without waiting for your approval by default. Click Resolve, and the issue is Done. A database and a collection are created. bb-issue-dbdemo-done

  4. Go back to the home page by clicking Home on the left sidebar. On the home page, you can see the project, the database, and the issue you just resolved.

Step 5 - Add data in MongoDB via Bytebase

In Step 4, you created an issue to create a database and a collection via UI workflow and then executed it. Let’s try to create another issue to add data to it.

  1. Go to project TestMongoDB, and click Change Data. bb-project-change-data

  2. Choose dbdemo and click Next. It could generate a pipeline if you have different databases for different environments. bb-project-change-data-select-db

  3. Input statement as follows, and click Create.

db.cdemo.insertOne({
  id: 1,
  name: "Adela"
})
  1. Bytebase will do some basic checks and then execute the statement. Since it’s for Test environment, the issue is automatically approved by default. Click Resolve issue, and the issue status will become Done. bb-issue-insert-done

  2. Go to Change History under the project to view the full history. Or go into a specific database to view its history. bb-project-change-history bb-db-change-history

Summary and Next

Now you have connected MongoDB with Bytebase, and tried out the UI workflow to do data change. Bytebase will record the full change history for you.

In the next article, you’ll try out GitOps workflow, which will store your MongoDB in GitHub and trigger the change upon committing the change to the repository, to bring your MongoDB change workflow to the next level, aka Database DevOps - Database as Code.

Edit this page on GitHub

Subscribe to Newsletter

By subscribing, you agree with Bytebase's Terms of Service and Privacy Policy.