From c6c6874d729c6d16bdce71f370cf5cd1863648a8 Mon Sep 17 00:00:00 2001 From: Robert Haupt Date: Mon, 16 Apr 2018 11:12:06 -0600 Subject: [PATCH 1/3] Added -datetime option to create up/down files with version in yyyymmddhhmmss format. --- cli/commands.go | 12 +++++++++++- cli/main.go | 7 +++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/cli/commands.go b/cli/commands.go index aecbd85..527b463 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -10,6 +10,7 @@ import ( "path/filepath" "strconv" "strings" + "time" ) func nextSeq(matches []string, dir string, seqDigits int) (string, error) { @@ -48,8 +49,11 @@ 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) { +func createCmd(dir string, timestamp int64, name string, ext string, seq bool, seqDigits int, datetime bool) { var base string + if seq && datetime { + log.fatalErr(errors.New("seq and datetime options are mutually exclusive")) + } if seq { if seqDigits <= 0 { log.fatalErr(errors.New("Digits must be positive")) @@ -63,6 +67,12 @@ 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) } diff --git a/cli/main.go b/cli/main.go index 75b9b39..082a6fc 100644 --- a/cli/main.go +++ b/cli/main.go @@ -42,9 +42,10 @@ Options: -help Print usage Commands: - create [-ext E] [-dir D] [-seq] [-digits N] NAME + create [-ext E] [-dir D] [-seq] [-digits N] [-datetime] 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. goto V Migrate to version V up [N] Apply all or N up migrations down [N] Apply all or N down migrations @@ -109,12 +110,14 @@ 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)") 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 { @@ -131,7 +134,7 @@ Commands: timestamp := startTime.Unix() - createCmd(*dirPtr, timestamp, name, *extPtr, seq, seqDigits) + createCmd(*dirPtr, timestamp, name, *extPtr, seq, seqDigits, datetime) case "goto": if migraterErr != nil { From 8cdee40695c79f105b9e8aec7fa4612ac583ed32 Mon Sep 17 00:00:00 2001 From: Robert Haupt Date: Tue, 17 Apr 2018 11:09:08 -0600 Subject: [PATCH 2/3] 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 { From 1f389f142215c0a4d148dbd02202e8c3fc575bb4 Mon Sep 17 00:00:00 2001 From: Robert Haupt Date: Tue, 17 Apr 2018 11:17:10 -0600 Subject: [PATCH 3/3] Removed commented code --- cli/commands.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/cli/commands.go b/cli/commands.go index 55bf512..205cbe2 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -83,14 +83,6 @@ func createCmd(dir string, timestamp int64, format string, name string, ext stri 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)