|
| 1 | +package v500 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 7 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 8 | +) |
| 9 | + |
| 10 | +// UpgradeFromV4 handles state upgrades from v4 Plugin Framework provider (schema_version=0) to v5 (version=500). |
| 11 | +// |
| 12 | +// This performs a full transformation from v4 → v5 format. |
| 13 | +// The v4 state has schema_version=0 (implicit, no Version field), and we transform it to v5 format. |
| 14 | +// |
| 15 | +// Key transformations: |
| 16 | +// - domains: types.Set → *[]types.String (with alphabetical sorting) |
| 17 | +// - All other fields: direct pass-through |
| 18 | +func UpgradeFromV4(ctx context.Context, req resource.UpgradeStateRequest, resp *resource.UpgradeStateResponse) { |
| 19 | + tflog.Info(ctx, "Upgrading turnstile_widget state from v4 provider (schema_version=0)") |
| 20 | + |
| 21 | + // Parse v4 state using v4 model |
| 22 | + var v4State SourceCloudfareTurnstileWidgetModel |
| 23 | + resp.Diagnostics.Append(req.State.Get(ctx, &v4State)...) |
| 24 | + if resp.Diagnostics.HasError() { |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + // Transform v4 → v5 |
| 29 | + v5State, diags := Transform(ctx, v4State) |
| 30 | + resp.Diagnostics.Append(diags...) |
| 31 | + if resp.Diagnostics.HasError() { |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + // Write transformed state |
| 36 | + resp.Diagnostics.Append(resp.State.Set(ctx, v5State)...) |
| 37 | + tflog.Info(ctx, "State upgrade from v4 to v5 completed successfully") |
| 38 | +} |
| 39 | + |
| 40 | +// UpgradeFromV5 handles state upgrades from v5 Plugin Framework provider (version=1) to v5 (version=500). |
| 41 | +// |
| 42 | +// This is a no-op upgrade since the schema is compatible - just bumps the version. |
| 43 | +// This handler is only triggered when TF_MIG_TEST=1 (GetSchemaVersion returns 500). |
| 44 | +// |
| 45 | +// The v5 state with version=1 is the "dormant" state before migration activation. |
| 46 | +// When migrations are activated, this handler copies the state as-is to version=500. |
| 47 | +func UpgradeFromV5(ctx context.Context, req resource.UpgradeStateRequest, resp *resource.UpgradeStateResponse) { |
| 48 | + tflog.Info(ctx, "Upgrading turnstile_widget state from version=1 to version=500 (no-op)") |
| 49 | + |
| 50 | + // CRITICAL: For no-op upgrades, copy raw state directly |
| 51 | + // This preserves all state data without any transformation |
| 52 | + resp.State.Raw = req.State.Raw |
| 53 | + |
| 54 | + tflog.Info(ctx, "State version bump from 1 to 500 completed") |
| 55 | +} |
0 commit comments