Skip to content

Commit

Permalink
Restore and deprecate the to_skip_plan in a single-state migration
Browse files Browse the repository at this point in the history
This is a follow-up to #193. Renaming the `to_skip_plan` attribute in a
single-state migration block to `skip_plan` was an incompatible change
to the HCL file's syntax. Since it is technically possible to maintain
backward compatibility, we will continue to accept the old
`to_skip_plan` and log a warning. This attribute will be removed in a
future version. Use `skip_plan` instead.
  • Loading branch information
minamijoyo committed Dec 3, 2024
1 parent 5357815 commit 96a2e6b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
46 changes: 46 additions & 0 deletions config/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,52 @@ migration "state" "test" {
},
ok: true,
},
{
desc: "state with skip_plan",
source: `
migration "state" "test" {
skip_plan = true
actions = [
"mv null_resource.foo null_resource.foo2",
]
}
`,
want: &tfmigrate.MigrationConfig{
Type: "state",
Name: "test",
Migrator: &tfmigrate.StateMigratorConfig{
Dir: "",
Actions: []string{
"mv null_resource.foo null_resource.foo2",
},
SkipPlan: true,
},
},
ok: true,
},
{
desc: "state with to_skip_plan",
source: `
migration "state" "test" {
to_skip_plan = true
actions = [
"mv null_resource.foo null_resource.foo2",
]
}
`,
want: &tfmigrate.MigrationConfig{
Type: "state",
Name: "test",
Migrator: &tfmigrate.StateMigratorConfig{
Dir: "",
Actions: []string{
"mv null_resource.foo null_resource.foo2",
},
ToSkipPlan: true,
},
},
ok: true,
},
{
desc: "multi state with from_dir and to_dir",
source: `
Expand Down
12 changes: 11 additions & 1 deletion tfmigrate/state_migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ type StateMigratorConfig struct {
Force bool `hcl:"force,optional"`
// SkipPlan controls whether or not to run and analyze Terraform plan.
SkipPlan bool `hcl:"skip_plan,optional"`
// ToSkipPlan controls whether or not to run and analyze Terraform plan.
// Note: This variable exists only to maintain compatibility with an old bug
// but is deprecated. It will be removed in a future version.
ToSkipPlan bool `hcl:"to_skip_plan,optional"`
// Workspace is the state workspace which the migration works with.
Workspace string `hcl:"workspace,optional"`
}
Expand Down Expand Up @@ -64,7 +68,13 @@ func (c *StateMigratorConfig) NewMigrator(o *MigratorOption) (Migrator, error) {
c.Workspace = "default"
}

return NewStateMigrator(dir, c.Workspace, actions, o, c.Force, c.SkipPlan), nil
// This logic remains to maintain compatibility with an old bug but will be
// removed in a future version.
skipPlan := c.SkipPlan || c.ToSkipPlan
if c.ToSkipPlan {
log.Printf("[WARN] [migrator] `to_skip_plan` is deprecated. Use `skip_plan` instead.")
}
return NewStateMigrator(dir, c.Workspace, actions, o, c.Force, skipPlan), nil
}

// StateMigrator implements the Migrator interface.
Expand Down
15 changes: 12 additions & 3 deletions tfmigrate/state_migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,24 @@ func TestStateMigratorConfigNewMigrator(t *testing.T) {
Dir: "dir1",
Actions: []string{
"mv null_resource.foo null_resource.foo2",
"mv null_resource.bar null_resource.bar2",
"rm time_static.baz",
"import time_static.qux 2006-01-02T15:04:05Z",
},
SkipPlan: true,
},
o: nil,
ok: true,
},
{
desc: "with to_skip_plan true",
config: &StateMigratorConfig{
Dir: "dir1",
Actions: []string{
"mv null_resource.foo null_resource.foo2",
},
ToSkipPlan: true,
},
o: nil,
ok: true,
},
}

for _, tc := range cases {
Expand Down

0 comments on commit 96a2e6b

Please sign in to comment.