From cf1bd13b2e09df033e7e31fad7d271e2fd0803af Mon Sep 17 00:00:00 2001 From: Masayuki Morita Date: Fri, 22 Apr 2022 23:14:52 +0900 Subject: [PATCH] Allow to pass a name label of migration block --- migration/analyzer.go | 4 ++-- migration/migration.go | 19 ++++++++++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/migration/analyzer.go b/migration/analyzer.go index 559994f..a33ea85 100644 --- a/migration/analyzer.go +++ b/migration/analyzer.go @@ -32,7 +32,7 @@ func NewDefaultPlanAnalyzer() PlanAnalyzer { func (a *defaultPlanAnalyzer) Analyze(plan *Plan) *StateMigration { subject := NewSubject(plan) - var migration StateMigration + migration := NewStateMigration("fromplan") current := subject for _, r := range a.resolvers { next, actions := r.Resolve(current) @@ -40,5 +40,5 @@ func (a *defaultPlanAnalyzer) Analyze(plan *Plan) *StateMigration { current = next } - return &migration + return migration } diff --git a/migration/migration.go b/migration/migration.go index 078ea58..ee1a860 100644 --- a/migration/migration.go +++ b/migration/migration.go @@ -6,18 +6,20 @@ import ( "text/template" ) -// StateMigration is a type which is equivalent to tfmigrate.StateMigratorConfig of -// minamijoyo/tfmigrate. +// StateMigration is a type which corresponds to tfmigrate.StateMigratorConfig +// and config.MigrationBlock in minamijoyo/tfmigrate. // The current implementation doesn't encode migration actions to a file // directly with gohcl, so we define only what we need here. type StateMigration struct { - // Dir is a working directory for executing terraform command. + // A name label of migration block + Name string + // A working directory for executing terraform command. Dir string - // Actions is a list of state action. + // A list of state action. Actions []StateAction } -var migrationTemplate = `migration "state" "awsv4upgrade" { +var migrationTemplate = `migration "state" "{{ .Name }}" { actions = [ {{- range .Actions }} "{{ .MigrationAction }}", @@ -28,6 +30,13 @@ var migrationTemplate = `migration "state" "awsv4upgrade" { var compiledMigrationTemplate = template.Must(template.New("migration").Parse(migrationTemplate)) +// NewStateMigration returns a new instance of StateMigration. +func NewStateMigration(name string) *StateMigration { + return &StateMigration{ + Name: name, + } +} + // AppendActions appends a list of actions to migration. func (m *StateMigration) AppendActions(actions ...StateAction) { m.Actions = append(m.Actions, actions...)