Fix time format flag to be backwards compatible

https://github.com/golang-migrate/migrate/pull/17#issuecomment-397960867
This commit is contained in:
Dale Hui 2018-06-18 00:06:17 -07:00
parent c3794da4ed
commit 3239b18671
2 changed files with 15 additions and 13 deletions

View File

@ -49,9 +49,9 @@ func nextSeq(matches []string, dir string, seqDigits int) (string, error) {
return nextSeqStr, nil
}
func createCmd(dir string, timestamp int64, format string, name string, ext string, seq bool, seqDigits int) {
func createCmd(dir string, startTime time.Time, format string, name string, ext string, seq bool, seqDigits int) {
var base string
if seq && len(format) > 0 {
if seq && format != defaultTimeFormat {
log.fatalErr(errors.New("The seq and format options are mutually exclusive"))
}
if seq {
@ -68,13 +68,15 @@ func createCmd(dir string, timestamp int64, format string, name string, ext stri
}
base = fmt.Sprintf("%v%v_%v.", dir, nextSeqStr, name)
} else {
if len(format) > 0 {
t := time.Unix(timestamp, 0)
version := t.Format(format)
base = fmt.Sprintf("%v%v_%v.", dir, version, name)
} else {
log.Println("Time format not specified")
base = fmt.Sprintf("%v%v_%v.", dir, timestamp, name)
switch format {
case "":
log.fatal("Time format may not be empty")
case "unix":
base = fmt.Sprintf("%v%v_%v.", dir, startTime.Unix(), name)
case "unixNano":
base = fmt.Sprintf("%v%v_%v.", dir, startTime.UnixNano(), name)
default:
base = fmt.Sprintf("%v%v_%v.", dir, startTime.Format(format), name)
}
}

View File

@ -15,6 +15,8 @@ import (
"github.com/golang-migrate/migrate/source"
)
const defaultTimeFormat = "20060102150405"
// set main log
var log = &Log{}
@ -118,7 +120,7 @@ Database drivers: `+strings.Join(database.List(), ", ")+"\n")
createFlagSet := flag.NewFlagSet("create", flag.ExitOnError)
extPtr := createFlagSet.String("ext", "", "File extension")
dirPtr := createFlagSet.String("dir", "", "Directory to place file in (default: current working directory)")
formatPtr := createFlagSet.String("format", "", "Specify the format of the version using a Go time format string. For yyyymmddhhmmss use format of 20060102150405. If not specified the version will be the unix timestamp.")
formatPtr := createFlagSet.String("format", defaultTimeFormat, `The Go time format string to use. If the string "unix" or "unixNano" is specified, then the seconds or nanoseconds since January 1, 1970 UTC respectively will be used. Caution, due to the behavior of time.Time.Format(), invalid format strings will not error`)
createFlagSet.BoolVar(&seq, "seq", seq, "Use sequential numbers instead of timestamps (default: false)")
createFlagSet.IntVar(&seqDigits, "digits", seqDigits, "The number of digits to use in sequences (default: 6)")
createFlagSet.Parse(args)
@ -137,9 +139,7 @@ Database drivers: `+strings.Join(database.List(), ", ")+"\n")
*dirPtr = strings.Trim(*dirPtr, "/") + "/"
}
timestamp := startTime.Unix()
createCmd(*dirPtr, timestamp, *formatPtr, name, *extPtr, seq, seqDigits)
createCmd(*dirPtr, startTime, *formatPtr, name, *extPtr, seq, seqDigits)
case "goto":
if migraterErr != nil {