aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiall Sheridan <nsheridan@gmail.com>2018-08-07 23:27:22 +0100
committerNiall Sheridan <nsheridan@gmail.com>2018-08-07 23:27:22 +0100
commitf089aaf1b66309756d3ee0e44cab30e17139b71a (patch)
treeb9c85d7efab539505faa6847fa5bfd0229ad2217
parentb79648e0304dc5eb49b2f6f007e7ef750d729390 (diff)
Add a tool to create empty migrations
-rw-r--r--Makefile6
-rw-r--r--generate/migration/migration.go53
-rw-r--r--generate/static/static.go (renamed from generate/static.go)0
3 files changed, 58 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 5c41cdd..d78efbd 100644
--- a/Makefile
+++ b/Makefile
@@ -29,8 +29,12 @@ cashierd: generate
clean:
rm -f cashier cashierd
+# usage: make migration name=whatever
+migration:
+ go run ./generate/migration/migration.go $(name)
+
dep:
go get -u github.com/golang/lint/golint
go get -u golang.org/x/tools/cmd/goimports
-.PHONY: all build dep generate test cashier cashierd clean
+.PHONY: all build dep generate test cashier cashierd clean migration
diff --git a/generate/migration/migration.go b/generate/migration/migration.go
new file mode 100644
index 0000000..37515bf
--- /dev/null
+++ b/generate/migration/migration.go
@@ -0,0 +1,53 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "io/ioutil"
+ "log"
+ "os/exec"
+ "path"
+ "strings"
+ "time"
+)
+
+const (
+ dateFormat = "20060102150405"
+ migrationsPath = "server/store/migrations"
+)
+
+var (
+ contents = []byte(`-- +migrate Up
+
+
+-- +migrate Down`)
+)
+
+func main() {
+ flag.Usage = func() {
+ fmt.Println("Usage: migration <migration name>")
+ }
+ flag.Parse()
+ if len(flag.Args()) != 1 {
+ flag.Usage()
+ }
+ name := fmt.Sprintf("%s_%s.sql", time.Now().UTC().Format(dateFormat), flag.Arg(0))
+ gitRoot, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
+ if err != nil {
+ log.Fatal(err)
+ }
+ root := strings.TrimSpace(string(gitRoot))
+ ents, err := ioutil.ReadDir(path.Join(root, migrationsPath))
+ if err != nil {
+ log.Fatal(err)
+ }
+ for _, e := range ents {
+ if e.IsDir() {
+ filename := path.Join(migrationsPath, e.Name(), name)
+ fmt.Printf("Wrote empty migration file: %s\n", filename)
+ if err := ioutil.WriteFile(filename, contents, 0644); err != nil {
+ log.Fatal(err)
+ }
+ }
+ }
+}
diff --git a/generate/static.go b/generate/static/static.go
index c8e7f43..c8e7f43 100644
--- a/generate/static.go
+++ b/generate/static/static.go