migrate/main.go

189 lines
4.0 KiB
Go
Raw Normal View History

2014-08-13 01:58:36 +00:00
// Package main is the CLI.
// You can use the CLI via Terminal.
// import "github.com/mattes/migrate/migrate" for usage within Go.
2014-08-11 01:42:57 +00:00
package main
import (
"flag"
"fmt"
"github.com/fatih/color"
2014-08-12 20:20:17 +00:00
"github.com/mattes/migrate/file"
2014-08-11 01:42:57 +00:00
"github.com/mattes/migrate/migrate"
2014-08-12 20:20:17 +00:00
"github.com/mattes/migrate/migrate/direction"
2014-08-12 01:36:07 +00:00
pipep "github.com/mattes/migrate/pipe"
2014-08-11 01:42:57 +00:00
"os"
2014-08-11 02:28:00 +00:00
"strconv"
2014-08-12 20:20:17 +00:00
"time"
2014-08-11 01:42:57 +00:00
)
2014-08-13 22:22:56 +00:00
var url = flag.String("url", "", "")
var migrationsPath = flag.String("path", "", "")
var version = flag.Bool("version", false, "Show migrate version")
2014-08-11 01:42:57 +00:00
func main() {
flag.Parse()
2014-08-11 02:28:00 +00:00
command := flag.Arg(0)
if *version {
fmt.Printf("%.2f", Version)
os.Exit(0)
}
2014-08-13 22:22:56 +00:00
if *migrationsPath == "" {
*migrationsPath, _ = os.Getwd()
}
2014-08-11 01:42:57 +00:00
switch command {
case "create":
2014-08-11 02:59:51 +00:00
verifyMigrationsPath(*migrationsPath)
2014-08-12 20:20:17 +00:00
name := flag.Arg(1)
if name == "" {
fmt.Println("Please specify name.")
os.Exit(1)
}
2014-08-13 22:22:56 +00:00
migrationFile, err := migrate.Create(*url, *migrationsPath, name)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Version %v migration files created in %v:\n", migrationFile.Version, migrationsPath)
fmt.Println(migrationFile.UpFile.FileName)
fmt.Println(migrationFile.DownFile.FileName)
2014-08-11 02:28:00 +00:00
case "migrate":
2014-08-11 02:59:51 +00:00
verifyMigrationsPath(*migrationsPath)
2014-08-11 02:28:00 +00:00
relativeN := flag.Arg(1)
relativeNInt, err := strconv.Atoi(relativeN)
2014-08-11 01:42:57 +00:00
if err != nil {
fmt.Println("Unable to parse parse param <n>.")
2014-08-11 01:42:57 +00:00
os.Exit(1)
}
2014-08-13 22:22:56 +00:00
timerStart = time.Now()
pipe := pipep.New()
go migrate.Migrate(pipe, *url, *migrationsPath, relativeNInt)
writePipe(pipe)
printTimer()
2014-08-11 02:59:51 +00:00
case "up":
verifyMigrationsPath(*migrationsPath)
2014-08-13 22:22:56 +00:00
timerStart = time.Now()
pipe := pipep.New()
go migrate.Up(pipe, *url, *migrationsPath)
writePipe(pipe)
printTimer()
2014-08-11 02:59:51 +00:00
case "down":
verifyMigrationsPath(*migrationsPath)
2014-08-13 22:22:56 +00:00
timerStart = time.Now()
pipe := pipep.New()
go migrate.Down(pipe, *url, *migrationsPath)
writePipe(pipe)
printTimer()
2014-08-11 02:59:51 +00:00
case "redo":
verifyMigrationsPath(*migrationsPath)
2014-08-13 22:22:56 +00:00
timerStart = time.Now()
pipe := pipep.New()
go migrate.Redo(pipe, *url, *migrationsPath)
writePipe(pipe)
printTimer()
2014-08-11 02:59:51 +00:00
case "reset":
verifyMigrationsPath(*migrationsPath)
2014-08-13 22:22:56 +00:00
timerStart = time.Now()
pipe := pipep.New()
go migrate.Reset(pipe, *url, *migrationsPath)
writePipe(pipe)
printTimer()
2014-08-11 02:59:51 +00:00
case "version":
verifyMigrationsPath(*migrationsPath)
2014-08-13 22:22:56 +00:00
version, err := migrate.Version(*url, *migrationsPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(version)
2014-08-11 02:28:00 +00:00
2014-08-11 02:59:51 +00:00
default:
2014-08-13 22:24:53 +00:00
fallthrough
case "help":
2014-08-11 02:59:51 +00:00
helpCmd()
2014-08-11 01:42:57 +00:00
}
}
2014-08-11 22:58:30 +00:00
func writePipe(pipe chan interface{}) {
if pipe != nil {
for {
select {
case item, ok := <-pipe:
if !ok {
return
} else {
switch item.(type) {
2014-08-11 22:58:30 +00:00
case string:
fmt.Println(item.(string))
2014-08-11 22:58:30 +00:00
case error:
c := color.New(color.FgRed)
2014-08-13 02:22:59 +00:00
c.Println(item.(error).Error(), "\n")
2014-08-12 20:20:17 +00:00
case file.File:
f := item.(file.File)
2014-08-13 02:22:59 +00:00
c := color.New(color.FgBlue)
2014-08-12 20:20:17 +00:00
if f.Direction == direction.Up {
2014-08-13 02:22:59 +00:00
c.Print(">")
2014-08-12 20:20:17 +00:00
} else if f.Direction == direction.Down {
2014-08-13 02:22:59 +00:00
c.Print("<")
2014-08-12 20:20:17 +00:00
}
fmt.Printf(" %s\n", f.FileName)
2014-08-11 22:58:30 +00:00
default:
text := fmt.Sprint(item)
2014-08-13 21:15:03 +00:00
fmt.Println(text)
2014-08-11 22:58:30 +00:00
}
}
}
}
}
}
2014-08-13 22:22:56 +00:00
func verifyMigrationsPath(path string) {
if path == "" {
fmt.Println("Please specify path")
os.Exit(1)
}
}
2014-08-12 20:20:17 +00:00
var timerStart time.Time
func printTimer() {
diff := time.Now().Sub(timerStart).Seconds()
if diff > 60 {
fmt.Printf("\n%.4f minutes\n", diff/60)
} else {
fmt.Printf("\n%.4f seconds\n", diff)
}
2014-08-12 20:20:17 +00:00
}
2014-08-11 02:59:51 +00:00
func helpCmd() {
os.Stderr.WriteString(
2014-08-13 22:26:44 +00:00
`usage: migrate [-path=<path>] -url=<url> <command> [<args>]
2014-08-11 02:59:51 +00:00
Commands:
create <name> Create a new migration
up Apply all -up- migrations
down Apply all -down- migrations
reset Down followed by Up
redo Roll back most recent migration, then apply it again
version Show current migration version
migrate <n> Apply migrations -n|+n
help Show this help
2014-08-13 22:22:56 +00:00
'-path' defaults to current working directory.
2014-08-11 02:59:51 +00:00
`)
}