From 8cdee40695c79f105b9e8aec7fa4612ac583ed32 Mon Sep 17 00:00:00 2001 From: Robert Haupt Date: Tue, 17 Apr 2018 11:09:08 -0600 Subject: [PATCH] Use Go time format string if version other than timestamp is desired. --- cli/commands.go | 29 +++++++++++++++++++---------- cli/main.go | 9 ++++----- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/cli/commands.go b/cli/commands.go index 527b463..55bf512 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -49,10 +49,10 @@ func nextSeq(matches []string, dir string, seqDigits int) (string, error) { return nextSeqStr, nil } -func createCmd(dir string, timestamp int64, name string, ext string, seq bool, seqDigits int, datetime bool) { +func createCmd(dir string, timestamp int64, format string, name string, ext string, seq bool, seqDigits int) { var base string - if seq && datetime { - log.fatalErr(errors.New("seq and datetime options are mutually exclusive")) + if seq && len(format) > 0 { + log.fatalErr(errors.New("The seq and format options are mutually exclusive")) } if seq { if seqDigits <= 0 { @@ -67,14 +67,15 @@ func createCmd(dir string, timestamp int64, name string, ext string, seq bool, s log.fatalErr(err) } base = fmt.Sprintf("%v%v_%v.", dir, nextSeqStr, name) - } else if datetime { - now := time.Now(); - year, month, day := now.Date(); - var m = int(month); - hr, min, sec := now.Clock(); - base = fmt.Sprintf("%04d%02d%02d%02d%02d%02d_%v.",year,m,day, hr, min, sec, name) } else { - base = fmt.Sprintf("%v%v_%v.", dir, timestamp, name) + 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) + } } os.MkdirAll(dir, os.ModePerm) @@ -82,6 +83,14 @@ func createCmd(dir string, timestamp int64, name string, ext string, seq bool, s createFile(base + "down" + ext) } +/*else if datetime { + now := time.Now(); + year, month, day := now.Date(); + var m = int(month); + hr, min, sec := now.Clock(); + base = fmt.Sprintf("%04d%02d%02d%02d%02d%02d_%v.",year,m,day, hr, min, sec, name) + } */ + func createFile(fname string) { if _, err := os.Create(fname); err != nil { log.fatalErr(err) diff --git a/cli/main.go b/cli/main.go index 082a6fc..20c32ff 100644 --- a/cli/main.go +++ b/cli/main.go @@ -42,10 +42,10 @@ Options: -help Print usage Commands: - create [-ext E] [-dir D] [-seq] [-digits N] [-datetime] NAME + create [-ext E] [-dir D] [-seq] [-digits N] [-format] NAME Create a set of timestamped up/down migrations titled NAME, in directory D with extension E. Use -seq option to generate sequential up/down migrations with N digits. - Use -datetime option to generate migrations where the version is yyyymmddhhmmss. + Use -format option to specify a Go time format string. goto V Migrate to version V up [N] Apply all or N up migrations down [N] Apply all or N down migrations @@ -110,14 +110,13 @@ Commands: args := flag.Args()[1:] seq := false seqDigits := 6 - datetime := false 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.") 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.BoolVar(&datetime, "datetime", datetime, "Generate migrations where the version is yyyymmddhhmmss") createFlagSet.Parse(args) if createFlagSet.NArg() == 0 { @@ -134,7 +133,7 @@ Commands: timestamp := startTime.Unix() - createCmd(*dirPtr, timestamp, name, *extPtr, seq, seqDigits, datetime) + createCmd(*dirPtr, timestamp, *formatPtr, name, *extPtr, seq, seqDigits) case "goto": if migraterErr != nil {