start working on migrate-cli

This commit is contained in:
mattes 2014-08-11 04:28:00 +02:00
parent 9b4dd699e2
commit 34e959f3b5
3 changed files with 85 additions and 44 deletions

View File

@ -20,33 +20,33 @@ Need another driver? Just implement the [Driver interface](http://godoc.org/gith
go get github.com/mattes/migrate
# create new migration
migrate -url="postgres://user@host:port/database" -path="./db/migrations" create
migrate -url postgres://user@host:port/database -path ./db/migrations create add_field_to_table
# apply all *up* migrations
migrate -url="postgres://user@host:port/database" -path="./db/migrations" up
migrate -url postgres://user@host:port/database -path ./db/migrations up
# apply all *down* migrations
migrate -url="postgres://user@host:port/database" -path="./db/migrations" down
migrate -url postgres://user@host:port/database -path ./db/migrations down
# roll back the most recently applied migration, then run it again.
migrate -url="postgres://user@host:port/database" -path="./db/migrations" redo
migrate -url postgres://user@host:port/database -path ./db/migrations redo
# down and up again
migrate -url="postgres://user@host:port/database" -path="./db/migrations" reset
migrate -url postgres://user@host:port/database -path ./db/migrations reset
# show current migration version
migrate -url="postgres://user@host:port/database" -path="./db/migrations" version
migrate -url postgres://user@host:port/database -path ./db/migrations version
# apply the next n migrations
migrate -url="postgres://user@host:port/database" -path="./db/migrations" migrate +1
migrate -url="postgres://user@host:port/database" -path="./db/migrations" migrate +2
migrate -url="postgres://user@host:port/database" -path="./db/migrations" migrate +n
migrate -url postgres://user@host:port/database -path ./db/migrations migrate +1
migrate -url postgres://user@host:port/database -path ./db/migrations migrate +2
migrate -url postgres://user@host:port/database -path ./db/migrations migrate +n
# apply the *down* migration of the current version
# and the previous n-1 migrations
migrate -url="postgres://user@host:port/database" -path="./db/migrations" migrate -1
migrate -url="postgres://user@host:port/database" -path="./db/migrations" migrate -2
migrate -url="postgres://user@host:port/database" -path="./db/migrations" migrate -n
migrate -url postgres://user@host:port/database -path ./db/migrations migrate -1
migrate -url postgres://user@host:port/database -path ./db/migrations migrate -2
migrate -url postgres://user@host:port/database -path ./db/migrations migrate -n
```
@ -58,7 +58,7 @@ See http://godoc.org/github.com/mattes/migrate/migrate
import "github.com/mattes/migrate/migrate"
migrate.Up("postgres://user@host:port/database", "./db/migrations")
migrate.Up postgres://user@host:port/database, "./db/migrations")
// ...
// ...
```

71
main.go
View File

@ -5,40 +5,81 @@ import (
"fmt"
"github.com/mattes/migrate/migrate"
"os"
"strconv"
)
var db = flag.String("db", "schema://url", "Driver connection URL")
var path = flag.String("path", "./db/migrations:./migrations:./db", "Migrations search path")
var help = flag.Bool("help", false, "Show help")
var url = flag.String("url", "schema://url", "Driver connection URL")
var path = flag.String("path", "", "Path to migrations")
func main() {
flag.Parse()
if *help {
usage()
os.Exit(0)
command := flag.Arg(0)
if *path == "" {
fmt.Println("Please specify path")
os.Exit(1)
}
command := flag.Arg(0)
switch command {
case "create":
if *path != "" {
fmt.Println("Please specify path")
os.Exit(1)
}
files, err := migrate.Create(*db, *path, "blablabla")
createCmd(*url, *path, flag.Arg(1))
case "migrate":
relativeN := flag.Arg(1)
relativeNInt, err := strconv.Atoi(relativeN)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(files)
migrateCmd(*url, *path, relativeNInt)
case "help":
helpCmd()
}
// fmt.Println(*db)
// fmt.Println(*url)
}
func usage() {
func helpCmd() {
fmt.Fprint(os.Stderr, "Usage of migrate:\n")
flag.PrintDefaults()
}
func createCmd(url, migrationsPath, name string) {
files, err := migrate.Create(url, migrationsPath, name)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(files)
}
func upCmd(url, migrationsPath string) {
}
func downCmd(url, migrationsPath string) {
}
func redoCmd(url, migrationsPath string) {
}
func resetCmd(url, migrationsPath string) {
}
func versionCmd(url, migrationsPath string) {
}
func migrateCmd(url, migrationsPath string, relativeN int) {
if err := migrate.Migrate(url, migrationsPath, relativeN); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

View File

@ -12,8 +12,8 @@ import (
"strings"
)
func common(db, migrationsPath string) (driver.Driver, *file.MigrationFiles, uint64, error) {
d, err := driver.New(db)
func common(url, migrationsPath string) (driver.Driver, *file.MigrationFiles, uint64, error) {
d, err := driver.New(url)
if err != nil {
return nil, nil, 0, err
}
@ -28,8 +28,8 @@ func common(db, migrationsPath string) (driver.Driver, *file.MigrationFiles, uin
return d, &files, version, nil
}
func Up(db, migrationsPath string) error {
d, files, version, err := common(db, migrationsPath)
func Up(url, migrationsPath string) error {
d, files, version, err := common(url, migrationsPath)
if err != nil {
return err
}
@ -44,8 +44,8 @@ func Up(db, migrationsPath string) error {
return errors.New("No migrations to apply.")
}
func Down(db, migrationsPath string) error {
d, files, version, err := common(db, migrationsPath)
func Down(url, migrationsPath string) error {
d, files, version, err := common(url, migrationsPath)
if err != nil {
return err
}
@ -60,8 +60,8 @@ func Down(db, migrationsPath string) error {
return errors.New("No migrations to apply.")
}
func Redo(db, migrationsPath string) error {
d, files, version, err := common(db, migrationsPath)
func Redo(url, migrationsPath string) error {
d, files, version, err := common(url, migrationsPath)
if err != nil {
return err
}
@ -84,8 +84,8 @@ func Redo(db, migrationsPath string) error {
return errors.New("No migrations to apply.")
}
func Reset(db, migrationsPath string) error {
d, files, version, err := common(db, migrationsPath)
func Reset(url, migrationsPath string) error {
d, files, version, err := common(url, migrationsPath)
if err != nil {
return err
}
@ -108,8 +108,8 @@ func Reset(db, migrationsPath string) error {
return errors.New("No migrations to apply.")
}
func Migrate(db, migrationsPath string, relativeN int) error {
d, files, version, err := common(db, migrationsPath)
func Migrate(url, migrationsPath string, relativeN int) error {
d, files, version, err := common(url, migrationsPath)
if err != nil {
return err
}
@ -130,16 +130,16 @@ func Migrate(db, migrationsPath string, relativeN int) error {
return errors.New("No migrations to apply.")
}
func Version(db, migrationsPath string) (version uint64, err error) {
d, err := driver.New(db)
func Version(url, migrationsPath string) (version uint64, err error) {
d, err := driver.New(url)
if err != nil {
return 0, err
}
return d.Version()
}
func Create(db, migrationsPath, name string) (*file.MigrationFile, error) {
d, err := driver.New(db)
func Create(url, migrationsPath, name string) (*file.MigrationFile, error) {
d, err := driver.New(url)
if err != nil {
return nil, err
}