For the complete documentation index, see llms.txt. This page is also available as Markdown.

Generating a MongoDB Changelog from an Existing Database

Automatically generate a Liquibase-compatible changelog from an existing MongoDB database using a Python script in Harness Database DevOps pipelines, and commit it to Git for version control.

When onboarding an existing MongoDB database, you can use the MongoDB.py Python script to extract the current schema and generate a Liquibase-compatible changelog.

This changelog can then be versioned in Git and used in subsequent deployments, ensuring auditability and consistency across environments.

By automating this process in a Harness pipeline, you can:

  • Avoid manual changelog creation for legacy or existing databases

  • Standardize schema tracking using Liquibase-compatible formats (JSON or YAML)

  • Keep your database changes version-controlled with peer review

For the SQL version of changelog generation, refer to our Get Started with Changelogs guide which includes examples for SQL changelogs.

Before you begin

If you are unfamiliar with generating or structuring a changelog file, you may want to explore our general build a changelog guide first, it covers generating SQL changelog, schema migration fundamentals, best practices, and format patterns. Before implementing the pipeline, ensure the following:

  • Pipeline execution environment can connect to your MongoDB instance

  • The Git connector used in the pipeline has commit permissions

  • MongoDB credentials have read-only access for schema extraction

Pipeline implementation

This pipeline will use the MongoDB.py script to generate a changelog file and commit it to Git for version control.

Create a new pipeline

  1. Go to your Harness pipeline.

  2. Click on "Create a Pipeline"

  3. In the Stage, select "Custom" and then create a "Step Group".

  4. Add the GitClone step.

  5. Then add a new step, Run MongoDB Changelog Generation

  • Container Registry: used to pull images from private or public registries.

  • Image: "python:latest"

  • Shell: "Python"

  • Command: Add the following script under the command palette:

In the above script:

  • Set the pipeline variables URL, DB_NAME, USERNAME, and PASSWORD to your MongoDB connection values.

  • Set OUTPUT_FILE to the changelog path you want generated in your repo.

  • Change AUTHOR and CHANGESET_ID to match your changelog naming convention.

MongoDB Changelog Generation Input

You can commit the generated changelog file to your git repository using the Run Command step in the pipeline. This allows you to version control your changelog file and keep track of changes over time. Otherwise, once the pipeline is executed, pods will be deleted and the changelog file will be lost.

  1. In the Pipeline, under the Step Group section, add a new step Run Command as the step type. Commit to Git Step

  • Name: The name of the step.

  • Registry Type: The type of registry to use. We can use Third Party Registry or Harness Artifact Registry.

  • Container Registry: The container registry to use. This is the location where the image is stored. In this case, we will use Docker Hub as the registry.

  • Image: The name of the image to use. In this case, we will use alpine/git.

  • Shell: The shell to use. We can use bash or sh, depending on the image used.

  • Command: The command to be executed. In this case, we will use following command to commit the changelog file to the git repository:

  1. Click on Apply Changes. Save the Pipeline and click on the Run button to run the pipeline.

Final result:

Commit to Git

This step will ensure that the generated changelog file is committed to your Git repository, allowing you to track changes and maintain version control over your database schema changes.

Best practices

Some best practices to follow when generating and committing MongoDB changelogs:

Next steps

  • Go to Build a Changelog to learn how to generate changelogs for SQL databases.

  • Explore our Database DevOps guide for a comprehensive overview of Harness Database DevOps features.

FAQs

1. Can I change the changelog filename?

Yes. Update the OUTPUT_FILE variable in the script to set a custom filename.

2. Does it support JSON output instead of YAML?

Currently, the script outputs YAML. You can modify the yaml.dump section to use json.dump if JSON output is preferred.

3. How are indexes handled?

All non-_id indexes are included in the changelog with createIndex changes. The script preserves uniqueness flags.

4. How do I avoid including Liquibase internal collections?

The script automatically excludes DATABASECHANGELOG and DATABASECHANGELOGLOCK collections.

Last updated

Was this helpful?