Added -datetime option to create up/down files with version in yyyymmddhhmmss format.

This commit is contained in:
Robert Haupt 2018-04-16 11:12:06 -06:00
parent 22f249514d
commit c6c6874d72
2 changed files with 16 additions and 3 deletions

View File

@ -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)
}

View File

@ -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 {