Merge pull request #17 from bobmeister/cli_create_yyyymmddhhmmss_migration_files

-datetime option to create files in yyyymmddhhmmss format
This commit is contained in:
Dale Hui 2018-04-17 10:50:48 -07:00 committed by GitHub
commit 2b28a0bc0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 4 deletions

View File

@ -10,6 +10,7 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"time"
) )
func nextSeq(matches []string, dir string, seqDigits int) (string, error) { 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 return nextSeqStr, nil
} }
func createCmd(dir string, timestamp int64, name string, ext string, seq bool, seqDigits int) { func createCmd(dir string, timestamp int64, format string, name string, ext string, seq bool, seqDigits int) {
var base string var base string
if seq && len(format) > 0 {
log.fatalErr(errors.New("The seq and format options are mutually exclusive"))
}
if seq { if seq {
if seqDigits <= 0 { if seqDigits <= 0 {
log.fatalErr(errors.New("Digits must be positive")) log.fatalErr(errors.New("Digits must be positive"))
@ -64,7 +68,14 @@ func createCmd(dir string, timestamp int64, name string, ext string, seq bool, s
} }
base = fmt.Sprintf("%v%v_%v.", dir, nextSeqStr, name) base = fmt.Sprintf("%v%v_%v.", dir, nextSeqStr, name)
} else { } 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) os.MkdirAll(dir, os.ModePerm)

View File

@ -42,9 +42,10 @@ Options:
-help Print usage -help Print usage
Commands: Commands:
create [-ext E] [-dir D] [-seq] [-digits N] 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. 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 -seq option to generate sequential up/down migrations with N digits.
Use -format option to specify a Go time format string.
goto V Migrate to version V goto V Migrate to version V
up [N] Apply all or N up migrations up [N] Apply all or N up migrations
down [N] Apply all or N down migrations down [N] Apply all or N down migrations
@ -113,6 +114,7 @@ Commands:
createFlagSet := flag.NewFlagSet("create", flag.ExitOnError) createFlagSet := flag.NewFlagSet("create", flag.ExitOnError)
extPtr := createFlagSet.String("ext", "", "File extension") extPtr := createFlagSet.String("ext", "", "File extension")
dirPtr := createFlagSet.String("dir", "", "Directory to place file in (default: current working directory)") 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.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.IntVar(&seqDigits, "digits", seqDigits, "The number of digits to use in sequences (default: 6)")
createFlagSet.Parse(args) createFlagSet.Parse(args)
@ -131,7 +133,7 @@ Commands:
timestamp := startTime.Unix() timestamp := startTime.Unix()
createCmd(*dirPtr, timestamp, name, *extPtr, seq, seqDigits) createCmd(*dirPtr, timestamp, *formatPtr, name, *extPtr, seq, seqDigits)
case "goto": case "goto":
if migraterErr != nil { if migraterErr != nil {