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
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
Go to your Harness pipeline.
Click on "Create a Pipeline"
In the Stage, select "Custom" and then create a "Step Group".
Add the GitClone step.
Then add a new step, Run

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, andPASSWORDto your MongoDB connection values.Set
OUTPUT_FILEto the changelog path you want generated in your repo.Change
AUTHORandCHANGESET_IDto match your changelog naming convention.

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.
In the Pipeline, under the
Step Groupsection, add a new stepRun Commandas the step type.
Name: The name of the step.
Registry Type: The type of registry to use. We can use
Third Party RegistryorHarness 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
bashorsh, 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:
Click on
Apply Changes. Save the Pipeline and click on theRunbutton to run the pipeline.
Final result:

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?