-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add "plan" command #16
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ const ( | |
pending = "... pending ..." | ||
execDo = "... do ..." | ||
execUndo = "... undo ..." | ||
execPlan = "... plan ..." | ||
stopped = "... stopped ..." | ||
) | ||
|
||
|
@@ -60,6 +61,14 @@ func cmdCreateMigration(c *cli.Context) error { | |
} | ||
|
||
func cmdMigrationsUp(c *cli.Context) error { | ||
return runMigrations(c, !true) | ||
} | ||
|
||
func cmdMigrationsPlan(c *cli.Context) error { | ||
return runMigrations(c, true) | ||
} | ||
|
||
func runMigrations(c *cli.Context, isLogOnly bool) error { | ||
mtx, err := getMigrationContext(c) | ||
if err != nil { | ||
printError(err.Error()) | ||
|
@@ -84,26 +93,38 @@ func cmdMigrationsUp(c *cli.Context) error { | |
reportError(mtx.stateProvider.Config(), "up until stop", errors.New("No such migration file: "+stopAfter)) | ||
return errAbort | ||
} | ||
prettyWidth := largestWithOf(all) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. largestWidthOf |
||
for _, each := range all { | ||
log.Println(statusSeparator) | ||
log.Println(execDo, pretty(each.Filename)) | ||
if err := ExecuteAll(each.DoSection, mtx.config().shellEnv(), c.GlobalBool("v")); err != nil { | ||
reportError(mtx.stateProvider.Config(), "do", err) | ||
return errAbort | ||
leadingTitle := execDo | ||
if isLogOnly { | ||
leadingTitle = execPlan | ||
} | ||
mtx.lastApplied = each.Filename | ||
// save after each succesful migration | ||
if err := mtx.stateProvider.SaveState(mtx.lastApplied); err != nil { | ||
reportError(mtx.stateProvider.Config(), "save state", err) | ||
return errAbort | ||
log.Printf("%s %-"+strconv.Itoa(prettyWidth)+"s (%s)\n", leadingTitle, pretty(each.Filename), each.Filename) | ||
if isLogOnly { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extract to a func that accepts 1 param (isLogOnly) will reduce the size of this function significantly maybe a runAll private function? |
||
log.Println("") | ||
if LogAll(each.DoSection, mtx.config().shellEnv(), true); err != nil { | ||
reportError(mtx.stateProvider.Config(), "plan do", err) | ||
return errAbort | ||
} | ||
} else { | ||
if err := ExecuteAll(each.DoSection, mtx.config().shellEnv(), c.GlobalBool("v")); err != nil { | ||
reportError(mtx.stateProvider.Config(), "do", err) | ||
return errAbort | ||
} | ||
mtx.lastApplied = each.Filename | ||
// save after each succesful migration | ||
if err := mtx.stateProvider.SaveState(mtx.lastApplied); err != nil { | ||
reportError(mtx.stateProvider.Config(), "save state", err) | ||
return errAbort | ||
} | ||
} | ||
// if not empty then stop after applying this migration | ||
if stopAfter == each.Filename { | ||
log.Println(stopped) | ||
log.Println(statusSeparator) | ||
break | ||
} | ||
log.Println(statusSeparator) | ||
} | ||
return nil | ||
} | ||
|
@@ -139,6 +160,17 @@ func cmdMigrationsDown(c *cli.Context) error { | |
return nil | ||
} | ||
|
||
func largestWithOf(list []Migration) int { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. largestWidthOf, not largestWithOf There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo |
||
prettyWidth := 0 | ||
for _, each := range list { | ||
pf := pretty(each.Filename) | ||
if len(pf) > prettyWidth { | ||
prettyWidth = len(pf) | ||
} | ||
} | ||
return prettyWidth | ||
} | ||
|
||
func cmdMigrationsStatus(c *cli.Context) error { | ||
mtx, err := getMigrationContext(c) | ||
if err != nil { | ||
|
@@ -152,13 +184,7 @@ func cmdMigrationsStatus(c *cli.Context) error { | |
} | ||
log.Println(statusSeparator) | ||
var last string | ||
prettyWidth := 0 | ||
for _, each := range all { | ||
pf := pretty(each.Filename) | ||
if len(pf) > prettyWidth { | ||
prettyWidth = len(pf) | ||
} | ||
} | ||
prettyWidth := largestWithOf(all) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't forget to rename this after renaming the function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. refactor tooling helps here |
||
for _, each := range all { | ||
status := applied | ||
if each.Filename > mtx.lastApplied { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why !true instead of false?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks nice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:' )