Skip to content

Commit

Permalink
Added -datetime option to create up/down files with version in yyyymm…
Browse files Browse the repository at this point in the history
…ddhhmmss format.
  • Loading branch information
Robert Haupt committed Apr 16, 2018
1 parent 22f2495 commit c6c6874
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
12 changes: 11 additions & 1 deletion cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"strconv"
"strings"
"time"
)

func nextSeq(matches []string, dir string, seqDigits int) (string, error) {
Expand Down Expand Up @@ -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"))
Expand All @@ -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)
}
Expand Down
7 changes: 5 additions & 2 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit c6c6874

Please sign in to comment.