Skip to content

Commit

Permalink
Allow to pass a name label of migration block
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Apr 22, 2022
1 parent 2e04eae commit cf1bd13
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
4 changes: 2 additions & 2 deletions migration/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ 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)
migration.AppendActions(actions...)
current = next
}

return &migration
return migration
}
19 changes: 14 additions & 5 deletions migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}",
Expand All @@ -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...)
Expand Down

0 comments on commit cf1bd13

Please sign in to comment.