Skip to content

fix: Preserve mongo_db_major_version state after out-of-band major version upgrade#4400

Open
marcabreracast wants to merge 6 commits intomasterfrom
CLOUDP-399538_Preserve_MongoDBMajorVersion_Outbound_Upgrades
Open

fix: Preserve mongo_db_major_version state after out-of-band major version upgrade#4400
marcabreracast wants to merge 6 commits intomasterfrom
CLOUDP-399538_Preserve_MongoDBMajorVersion_Outbound_Upgrades

Conversation

@marcabreracast
Copy link
Copy Markdown
Collaborator

@marcabreracast marcabreracast commented Apr 23, 2026

Description

Fixes a bug where mongodbatlas_advanced_cluster would retain a stale mongo_db_major_version in Terraform state after an out-of-band major version upgrade (e.g. upgrading from MongoDB 7.0 to 8.0 via Atlas UI and unpinning FCV). Even after updating the config to match the new version, the provider would perpetually show a diff.

The root cause was in overrideAttributesWithPrevStateValue: it unconditionally overwrote the API value with the prior state value whenever they differed, which was intended to suppress cosmetic formatting differences (e.g. "8" vs "8.0") but also masked real version transitions.

The fix narrows the override to cases where the major component is the same, handling two scenarios: formatting differences (e.g. "8" vs "8.0") and minor version bumps from version_release_system = "CONTINUOUS" (e.g. "8.0" vs "8.2") where the version is no longer user-controlled.

Link to any related issue(s): CLOUDP-399538

Closes #4398

Type of change:

  • Bug fix (non-breaking change which fixes an issue). Please, add the "bug" label to the PR.
  • New feature (non-breaking change which adds functionality). Please, add the "enhancement" label to the PR. A migration guide must be created or updated if the new feature will go in a major version.
  • Breaking change (fix or feature that would cause existing functionality to not work as expected). Please, add the "breaking change" label to the PR. A migration guide must be created or updated.
  • This change requires a documentation update
  • Documentation fix/enhancement

Required Checklist:

  • I have signed the MongoDB CLA
  • I have read the contributing guides
  • I have checked that this change does not generate any credentials and that they are NOT accidentally logged anywhere.
  • I have added tests that prove my fix is effective or that my feature works per HashiCorp requirements
  • I have added any necessary documentation (if appropriate)
  • I have run make fix and verified my code
  • If changes include deprecations or removals I have added appropriate changelog entries.
  • If changes include removal or addition of 3rd party GitHub actions, I updated our internal document. Reach out to the APIx Integration slack channel to get access to the internal document.

Further comments

Related external PR: #4399 (opened by the issue reporter with the same fix — this PR adapts it to follow our internal conventions).

Copilot AI review requested due to automatic review settings April 23, 2026 09:30
@github-actions github-actions Bot added the bug label Apr 23, 2026
@marcabreracast marcabreracast changed the title fix: Preserve mongo db major version outbound upgrades fix: Preserve mongo_db_major_version state after out-of-band major version upgrade Apr 23, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts advanced cluster state reconciliation so MongoDB major version values are only preserved from prior state when the difference is purely formatting (e.g., "8" vs "8.0"), while allowing real major-version transitions reported by the Atlas API to be reflected in Terraform state.

Changes:

  • Updates overrideAttributesWithPrevStateValue to preserve mongo_db_major_version only for formatting-only diffs.
  • Introduces ShouldUsePreviousMongoDBMajorVersion helper encapsulating the formatting comparison logic.
  • Adds a unit test covering the helper’s expected behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
internal/service/advancedcluster/resource_compatiblity.go Refines state override logic for mongo_db_major_version and adds a helper for formatting-only comparisons.
internal/service/advancedcluster/resource_compatiblity_test.go Adds unit test coverage for the new helper behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/service/advancedcluster/resource_compatibility_test.go
Comment thread internal/service/advancedcluster/resource_compatiblity_test.go Outdated
modelOut.UseEffectiveFields = modelIn.UseEffectiveFields
}

func ShouldUsePreviousMongoDBMajorVersion(beforeVersion, afterVersion string) bool {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function helps preserve the prior state value when the major component is the same, which handles two cases:

  1. Formatting differences: user sets "8" but Atlas returns "8.0" which is the same version, but different format. This is to preserve SDKv2 behaviour
  2. version_release_system = "CONTINUOUS": Atlas can return a minor bump (e.g. "8.2") that is not user-controlled. Letting it through causes: provider produced an unexpected new value: .mongo_db_major_version: was cty.StringVal("8.0"), but now cty.StringVal("8.2"). `

The reason why step 2 is implemented, is to avoid unit test failure

@marcabreracast marcabreracast marked this pull request as ready for review April 24, 2026 09:34
@marcabreracast marcabreracast requested review from a team as code owners April 24, 2026 09:34
@github-actions
Copy link
Copy Markdown
Contributor

APIx bot: a message has been sent to Docs Slack channel

@augmentcode
Copy link
Copy Markdown

augmentcode Bot commented Apr 24, 2026

🤖 Augment PR Summary

Summary: Fixes perpetual plan drift for mongodbatlas_advanced_cluster when mongo_db_major_version changes out-of-band (e.g., via Atlas UI).

Changes:

  • Narrows the state-override logic to only keep the prior version when the major component matches (avoids masking real major upgrades).
  • Adds helpers to normalize/compare version major components.
  • Adds unit tests covering formatting differences, real major transitions, and same-major minor drift.

🤖 Was this summary useful? React with 👍 or 👎

Copy link
Copy Markdown

@augmentcode augmentcode Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. 2 suggestions posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.

beforeVersion := conversion.NilForUnknown(modelIn.MongoDBMajorVersion, modelIn.MongoDBMajorVersion.ValueStringPointer())
if beforeVersion != nil && !modelIn.MongoDBMajorVersion.Equal(modelOut.MongoDBMajorVersion) {
afterVersion := conversion.NilForUnknown(modelOut.MongoDBMajorVersion, modelOut.MongoDBMajorVersion.ValueStringPointer())
// Only preserve prior state value for formatting differences (e.g. "8" vs "8.0"), not real version transitions.
Copy link
Copy Markdown

@augmentcode augmentcode Bot Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

internal/service/advancedcluster/resource_compatiblity.go:99 — This comment says the override is only for formatting differences, but ShouldUsePreviousMongoDBMajorVersion also preserves the prior value when the minor version changes within the same major (e.g., CONTINUOUS returning 8.2). Consider clarifying the comment so future readers don’t assume this logic is formatting-only.

Severity: low

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

)

func TestAdvancedCluster_ShouldUsePreviousMongoDBMajorVersion(t *testing.T) {
testCases := []struct {
Copy link
Copy Markdown

@augmentcode augmentcode Bot Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

internal/service/advancedcluster/resource_compatibility_test.go:11 — The table covers major-change vs same-major outcomes, but it doesn’t exercise the beforeVersion == afterVersion early-return branch in ShouldUsePreviousMongoDBMajorVersion. Consider adding an explicit equality case to lock in that behavior.

Severity: low

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

Copy link
Copy Markdown
Collaborator

@EspenAlbert EspenAlbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Really good PR description 👏

Copy link
Copy Markdown
Member

@AgustinBettati AgustinBettati left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes LGTM, would just confirm with Alex and team on potential impact

Comment thread .changelog/4400.txt Outdated
return majorComponent(beforeVersion) == majorComponent(afterVersion)
}

func majorComponent(version string) string {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Users that have an outdate mongo_db_major_version value would now receive a non-empty plan (cases where major version upgrade was done outside of TF). Would confirm with Alex we are okay with this impact.

return majorComponent(beforeVersion) == majorComponent(afterVersion)
}

func majorComponent(version string) string {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: Does version_release_system = "CONTINUOUS" only upgrade minor versions?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both, per my understanding of the docs:

Latest Version With Auto Upgrades: If you set your cluster to this release option, it receives automatic upgrades to the latest available MongoDB major or minor version

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, would make sure to test if an error is returned if version_release_system = "CONTINUOUS" and mongo_db_major_version attribute are used together (as per our docs). Using this combination would lead to non-empty plan once automatic major version upgrades are made.

Copy link
Copy Markdown

@jromo-mdb jromo-mdb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: mongodbatlas_advanced_cluster keeps previous mongo_db_major_version in state after out-of-band major upgrade

6 participants