Skip to content
This repository has been archived by the owner on Jun 28, 2018. It is now read-only.

add option to choose a timestamp format for migration files in CLI #329

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/commands.go
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
)

func createCmd(dir string, timestamp int64, name string, ext string) {
func createCmd(dir string, timestamp interface{}, name string, ext string) {
base := fmt.Sprintf("%v%v_%v.", dir, timestamp, name)
os.MkdirAll(dir, os.ModePerm)
createFile(base + "up" + ext)
Expand Down
12 changes: 10 additions & 2 deletions cli/main.go
Expand Up @@ -42,8 +42,9 @@ Options:
-help Print usage

Commands:
create [-ext E] [-dir D] NAME
create [-ext E] [-dir D] [-timestamp unix] NAME
Create a set of timestamped up/down migrations titled NAME, in directory D with extension E
-timestamp accepts "unix" for Unix timestamps (e.g. 1516205943) and "datetime" or "time" for datetime timestamps (e.g. 180117181903), default: "unix"
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 @@ -110,6 +111,7 @@ Commands:
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)")
timestampFormatPtr := createFlagSet.String("timestamp", "unix", `Format of a timestamp ("unix" for Unix timestamp, "datetime" or "time" for datetime timestamp, default: "unix")`)
createFlagSet.Parse(args)

if createFlagSet.NArg() == 0 {
Expand All @@ -124,7 +126,13 @@ Commands:
*dirPtr = strings.Trim(*dirPtr, "/") + "/"
}

timestamp := startTime.Unix()
var timestamp interface{}

if *timestampFormatPtr == "datetime" || *timestampFormatPtr == "time" {
timestamp = startTime.Format("060102150405")
} else {
timestamp = startTime.Unix()
}

createCmd(*dirPtr, timestamp, name, *extPtr)

Expand Down