Skip to content

Commit 80a9d6b

Browse files
authored
PSS: Prevent the state store provider being upgraded via the init command, unless the store is reconfigured (#38391)
* test: Move existing provider upgrade test into subtest, preparing for more subtests later * feat: `init` command blocks upgrading the PSS provider * test: Add test showing that upgrading non-state store providers isn't blocked as long as the state store provider is pinned in required_providers. * feat: Upgrading the state store provider is possible during init, but it requires reconfiguring the state store. * test: Revert change to shared test fixture, update -upgrade tests to create their own main.tf files.
1 parent 2c125d7 commit 80a9d6b

2 files changed

Lines changed: 551 additions & 104 deletions

File tree

internal/command/init_run.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,35 @@ func (c *InitCommand) run(initArgs *arguments.Init, view views.Init) int {
220220
header = true
221221
}
222222

223+
// The init command is not allowed to upgrade the provider used for PSS (unless we're reconfiguring the state store).
224+
// Unless users choose to reconfigure, they must upgrade the state store provider separately using `terraform state migrate -upgrade`.
225+
if initArgs.Upgrade && !initArgs.Reconfigure && config.Module.StateStore != nil {
226+
pAddr := config.Module.StateStore.ProviderAddr
227+
old := previousLocks.Provider(pAddr)
228+
new := configLocks.Provider(pAddr)
229+
if old == nil || new == nil {
230+
panic(fmt.Sprintf(`Unexpected missing provider lock for %s during init -upgrade:
231+
prior lock: %#v
232+
new lock: %#v`, pAddr.ForDisplay(), old, new))
233+
}
234+
if !new.Version().Same((old.Version())) {
235+
// The upgrade has impacted the provider
236+
diags = diags.Append(tfdiags.Sourceless(
237+
tfdiags.Error,
238+
"Cannot upgrade the provider used for pluggable state storage during \"terraform init -upgrade\"",
239+
fmt.Sprintf(`While upgrading providers Terraform attempted to upgrade the %s (%q) provider, which is used by the state_store block in your configuration.
240+
Please use \"terraform state migrate -upgrade\" to upgrade the state store provider and navigate migrating your state between the two versions. You can then re-attempt \"terraform init -upgrade\" to upgrade the rest of your providers.
241+
242+
If you do not intend to upgrade the state store provider, please update your configuration to pin to the current version (%s), and re-run \"terraform init -upgrade\" to upgrade the rest of your providers.
243+
`,
244+
pAddr.Type, pAddr.ForDisplay(), old.Version()),
245+
),
246+
)
247+
view.Diagnostics(diags)
248+
return 1
249+
}
250+
}
251+
223252
// If we outputted information, then we need to output a newline
224253
// so that our success message is nicely spaced out from prior text.
225254
if header {

0 commit comments

Comments
 (0)