Use Go time format string if version other than timestamp is desired.

This commit is contained in:
Robert Haupt 2018-04-17 11:09:08 -06:00
parent c6c6874d72
commit 8cdee40695
2 changed files with 23 additions and 15 deletions

View File

@ -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,21 +67,30 @@ 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 {
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)
createFile(base + "up" + ext)
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)

View File

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