MongoDB Database Clearing Process
Purpose
The MongoDB clearing process is implemented as a GitHub Action that runs before API deployments to develop and staging environments. This process serves several critical purposes:
-
Simulates Production Deployment: By clearing the database before each deployment, we simulate how our first release to production will work (with a clean database).
-
Validates Data Migrations: Ensures all initial data migrations run successfully during each environment deployment.
-
Prevents Data Contamination: Prevents old data structures from causing unexpected behavior after schema changes.
-
Consistent Testing Environments: Provides a consistent baseline for testing new features and migrations.
Workflow Design
The database clearing process is implemented as a reusable GitHub Action workflow that:
- Is triggered automatically as part of the API CI/CD pipeline for develop and staging environments
- Can also be triggered manually via workflow dispatch
- Has appropriate safeguards to prevent running against production environments
- Provides detailed logging and error reporting
- Sends notifications upon completion
Configuration
Required Secrets
The workflow requires the following secrets to be configured in your GitHub repository:
MONGO_CONNECTION_STRING: MongoDB connection string for our mongo clusterSLACK_WEBHOOK_URL: Webhook URL for Slack notifications
Trigger Configuration
The database clearing happens automatically as part of the API CI/CD pipeline through workflow calls:
- For the
developbranch, it clears the develop environment database - For the
mainbranch, it clears the staging environment database
Security Considerations
-
Connection String Security: MongoDB connection strings are stored as GitHub secrets and never exposed in logs or outputs.
-
Environment Isolation: The workflow contains explicit checks to ensure it only runs against develop and staging environments.
-
Limited Database Operations: The script only deletes data from collections, preserving database structure and not affecting system collections.
-
Minimal Permissions: The MongoDB users specified in the connection strings should have only the necessary permissions for the environments they access.
-
Execution Logging: All actions are logged to enable audit trails and troubleshooting.
Maintenance Notes
After the initial production release, this workflow will be updated to:
- Restore a sanitized copy of production data to staging environments instead of clearing the database
- Maintain the database clearing for develop environments
Validation Process
The success of the database clearing and subsequent migrations is validated through:
- GitHub Action logs showing successful collection clearing
- API startup logs confirming successful migrations
- Application functionality tests in the cleared environments
Troubleshooting
If the database clearing fails:
- Check the GitHub Action logs for specific error messages
- Verify the MongoDB connection strings are correct and the users have appropriate permissions
- Ensure the MongoDB server is accessible from the GitHub Action runner
- Check for any locks or active connections that might prevent collection clearing