Skip to content

Commit

Permalink
Add option to start SQL Runner from a given step (closes #32)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeemster committed Nov 4, 2015
1 parent 1ddb40d commit d1303b5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func main() {
log.Fatalf("Could not parse playbook (YAML): %s", err.Error())
}

statuses := run.Run(pb, options.sqlroot)
statuses := run.Run(pb, options.sqlroot, options.fromStep)
code, message := review(statuses)

log.Printf(message)
Expand Down
2 changes: 2 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Options struct {
version bool
playbook string
sqlroot string
fromStep string
variables CLIVariables
}

Expand All @@ -55,6 +56,7 @@ func (o *Options) GetFlagSet() *flag.FlagSet {
fs.StringVar(&(o.playbook), "playbook", "", "Playbook of SQL scripts to execute")
fs.StringVar(&(o.sqlroot), "sqlroot", SQLROOT_PLAYBOOK, fmt.Sprintf("Absolute path to SQL scripts. Use %s and %s for those respective paths", SQLROOT_PLAYBOOK, SQLROOT_BINARY))
fs.Var(&(o.variables), "var", "Variables to be passed to the playbook, in the key=value format")
fs.StringVar(&(o.fromStep), "fromStep", "", "Starts from a given step defined in your playbook")
// TODO: add format flag if/when we support TOML

return fs
Expand Down
40 changes: 39 additions & 1 deletion run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
POSTGRESQL_TYPE = "postgresql"

ERROR_UNSUPPORTED_DB_TYPE = "Database type is unsupported"
ERROR_FROM_STEP_NOT_FOUND = "The fromStep argument did not match any available steps"
)

// Reports on any errors from running the
Expand Down Expand Up @@ -55,7 +56,34 @@ type QueryStatus struct {
//
// Handles dispatch to the appropriate
// database engine
func Run(pb playbook.Playbook, sqlroot string) []TargetStatus {
func Run(pb playbook.Playbook, sqlroot string, fromStep string) []TargetStatus {

// Check fromStep argument to ensure that it actually matches a step and to get the
// index to start from
stepIndex := 0
if fromStep != "" {
exists := false
for i := 0; i < len(pb.Steps); i++ {
if pb.Steps[i].Name == fromStep {
exists = true
stepIndex = i
break;
}
}

// Process failure case
if exists == false {
allStatuses := make([]TargetStatus, 0)
for _, tgt := range pb.Targets {
status := fromStepNotFound(tgt.Name, fromStep)
allStatuses = append(allStatuses, status)
}
return allStatuses
}
}

// Trim skippable steps from the array
pb.Steps = pb.Steps[stepIndex:]

targetChan := make(chan TargetStatus, len(pb.Targets))

Expand All @@ -77,6 +105,16 @@ func Run(pb playbook.Playbook, sqlroot string) []TargetStatus {
return allStatuses
}

// Helper for a fromStep not found error
func fromStepNotFound(targetName string, fromStep string) TargetStatus {
errs := []error{fmt.Errorf("%s: %s", ERROR_FROM_STEP_NOT_FOUND, fromStep)}
return TargetStatus{
Name: targetName,
Errors: errs,
Steps: nil,
}
}

// Route to correct database client and run
func routeAndRun(target playbook.Target, sqlroot string, steps []playbook.Step, variables map[string]interface{}, targetChan chan TargetStatus) {
switch strings.ToLower(target.Type) {
Expand Down

0 comments on commit d1303b5

Please sign in to comment.