-
Notifications
You must be signed in to change notification settings - Fork 788
feat: state upgrader #6686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: state upgrader #6686
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package migrations | ||
|
|
||
| import "os" | ||
|
|
||
| // GetSchemaVersion returns the appropriate schema version based on TF_MIG_TEST environment variable. | ||
| // | ||
| // This function allows controlled rollout of StateUpgrader migrations: | ||
| // - During development/testing: Set TF_MIG_TEST=1 to enable migrations (returns postMigration version) | ||
| // - In production: StateUpgraders remain dormant (returns preMigration version) | ||
| // - For coordinated release: Remove this wrapper and set Version directly to enable all migrations at once | ||
| // | ||
| // Parameters: | ||
| // - preMigration: The version to use when migrations are disabled (typically 0) | ||
| // - postMigration: The version to use when migrations are enabled (typically 500) | ||
| // | ||
| // Example usage: | ||
| // | ||
| // Version: GetSchemaVersion(0, 500) // Returns 0 normally, 500 when TF_MIG_TEST=1 | ||
| func GetSchemaVersion(preMigration, postMigration int64) int64 { | ||
| if os.Getenv("TF_MIG_TEST") == "" { | ||
| return preMigration | ||
| } | ||
| return postMigration | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package v500 | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework/resource" | ||
| "github.com/hashicorp/terraform-plugin-log/tflog" | ||
| ) | ||
|
|
||
| // UpgradeFromV0 handles state upgrades from earlier v500 versions (schema_version=0) to current v500. | ||
| // This is a no-op upgrade since the schema is compatible - just copy state through. | ||
| // | ||
| func UpgradeFromV0( | ||
| ctx context.Context, | ||
| req resource.UpgradeStateRequest, | ||
| resp *resource.UpgradeStateResponse, | ||
| ) { | ||
| tflog.Info(ctx, "Upgrading DNS record state from schema_version=0") | ||
| // No-op upgrade: schema is compatible, just copy raw state through | ||
| // We use the raw state value directly to avoid issues with custom field type serialization | ||
| resp.State.Raw = req.State.Raw | ||
| } | ||
|
|
||
| // UpgradeFromLegacyV3 handles state upgrades from the legacy cloudflare_record resource to cloudflare_dns_record. | ||
| // This is triggered when users manually run `terraform state mv cloudflare_record.x cloudflare_dns_record.x` | ||
| // (Terraform < 1.8), which preserves the source schema_version=3 from the legacy provider. | ||
| // | ||
| // Note: schema_version=3 was the final schema version of cloudflare_record in the legacy (SDKv2) provider | ||
| // before it was deprecated. The state structure matches SourceCloudflareRecordModel. | ||
| func UpgradeFromLegacyV3(ctx context.Context, req resource.UpgradeStateRequest, resp *resource.UpgradeStateResponse) { | ||
| tflog.Info(ctx, "Upgrading DNS record state from legacy cloudflare_record (schema_version=3)") | ||
|
|
||
| // Parse the state (schema_version=3, source resource type) | ||
| var sourceState SourceCloudflareRecordModel | ||
| resp.Diagnostics.Append(req.State.Get(ctx, &sourceState)...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
|
|
||
| // Transform to target | ||
| targetState, diags := Transform(ctx, sourceState) | ||
| resp.Diagnostics.Append(diags...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
|
|
||
| // Set the upgraded state | ||
| resp.Diagnostics.Append(resp.State.Set(ctx, targetState)...) | ||
|
|
||
| tflog.Info(ctx, "State upgrade from legacy cloudflare_record completed successfully") | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.