Skip to content

feat: support automatic MongoDB database cleanup on stack destroy#178

Open
baotn166 wants to merge 2 commits intomainfrom
feature/add-support-clean-database
Open

feat: support automatic MongoDB database cleanup on stack destroy#178
baotn166 wants to merge 2 commits intomainfrom
feature/add-support-clean-database

Conversation

@baotn166
Copy link
Contributor

@baotn166 baotn166 commented Mar 9, 2026

Overview

When mongoDbDestroyDatabase: true is set in cloudExtras of a stack client config, the provisioner will automatically drop the corresponding MongoDB database before the Pulumi stack is destroyed.

Changes

pkg/clouds/pulumi/mongodb/drop_db.go (new file)

  • Adds DropDatabase(ctx, mongoUri, dbName) helper that connects to MongoDB (with a 30s timeout) and drops the target database
  • The URI must include credentials with dbAdmin privileges on the target database

pkg/clouds/pulumi/destroy.go

  • Extends destroyStack signature to accept variadic preDestroyHooks ...func(auto.Stack)
  • Hooks are executed after stack refresh but before the actual destroy (skipped on preview runs)

pkg/clouds/pulumi/provisioner.go

  • Adds dropMongoDbIfEnabled method called as a preDestroyHook in DestroyChildStack
  • Reads cloudExtras.mongoDbDestroyDatabase from the stack's client config
  • Fetches stack outputs and finds MongoDB service-user credentials (stack output keys ending in -service-user)
  • Parses the DbUserOutput JSON to extract dbUri, userName, and password
  • Constructs the full MongoDB URI and calls DropDatabase; failures are logged as warnings (non-fatal) so the destroy proceeds regardless

go.mod / go.sum

  • Adds go.mongodb.org/mongo-driver and its transitive dependencies

Configuration example

# stack client config
cloudExtras:
  mongoDbDestroyDatabase: true

Behavior notes

  • If mongoDbDestroyDatabase is absent or false, no MongoDB operation is performed
  • If the database drop fails (e.g. wrong credentials, network issue), the error is logged as a warning and the stack destroy continues normally
  • Only stack outputs whose key ends with -service-user are used to discover MongoDB databases to drop

baotn166 added 2 commits March 9, 2026 23:58
When `mongoDbDestroyDatabase: true` is set in `cloudExtras` of a stack
client config, the provisioner will automatically drop the corresponding
MongoDB database before destroying the Pulumi stack.

- Add `pkg/clouds/pulumi/mongodb/drop_db.go` with `DropDatabase` helper
  that connects to MongoDB and drops the target database
- Extend `destroyStack` to accept optional `preDestroyHooks` so callers
  can inject cleanup logic before the actual destroy runs
- In `DestroyChildStack`, read stack outputs to discover MongoDB service
  user credentials (keys ending in `-service-user`), then call
  `DropDatabase` when the feature flag is enabled
- Add `go.mongodb.org/mongo-driver` and related transitive dependencies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant