mirror of https://github.com/status-im/migrate.git
refactor and add timer
This commit is contained in:
parent
2301404557
commit
2353a03eb4
|
@ -2,7 +2,6 @@ package postgres
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/mattes/migrate/file"
|
||||
"github.com/mattes/migrate/migrate/direction"
|
||||
|
@ -45,16 +44,8 @@ func (driver *Driver) FilenameExtension() string {
|
|||
|
||||
func (driver *Driver) Migrate(files file.Files, pipe chan interface{}) {
|
||||
defer close(pipe)
|
||||
|
||||
for _, f := range files {
|
||||
|
||||
direc := ""
|
||||
if f.Direction == direction.Up {
|
||||
direc = " →"
|
||||
} else if f.Direction == direction.Down {
|
||||
direc = "← "
|
||||
}
|
||||
pipe <- fmt.Sprintf("%s | %s", direc, f.FileName)
|
||||
pipe <- f
|
||||
|
||||
tx, err := driver.db.Begin()
|
||||
if err != nil {
|
||||
|
@ -80,7 +71,11 @@ func (driver *Driver) Migrate(files file.Files, pipe chan interface{}) {
|
|||
}
|
||||
}
|
||||
|
||||
f.Read()
|
||||
if err := f.ReadContent(); err != nil {
|
||||
pipe <- err
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := tx.Exec(string(f.Content)); err != nil {
|
||||
pipe <- err
|
||||
if err := tx.Rollback(); err != nil {
|
||||
|
@ -94,8 +89,6 @@ func (driver *Driver) Migrate(files file.Files, pipe chan interface{}) {
|
|||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (driver *Driver) Version() (uint64, error) {
|
||||
|
|
|
@ -37,7 +37,7 @@ type MigrationFile struct {
|
|||
type MigrationFiles []MigrationFile
|
||||
|
||||
// Read reads the file contents
|
||||
func (f *File) Read() error {
|
||||
func (f *File) ReadContent() error {
|
||||
content, err := ioutil.ReadFile(path.Join(f.Path, f.FileName))
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
41
main.go
41
main.go
|
@ -3,10 +3,13 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/mattes/migrate/file"
|
||||
"github.com/mattes/migrate/migrate"
|
||||
"github.com/mattes/migrate/migrate/direction"
|
||||
pipep "github.com/mattes/migrate/pipe"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var url = flag.String("url", "", "Driver connection URL, like schema://url")
|
||||
|
@ -19,7 +22,12 @@ func main() {
|
|||
switch command {
|
||||
case "create":
|
||||
verifyMigrationsPath(*migrationsPath)
|
||||
createCmd(*url, *migrationsPath, flag.Arg(1))
|
||||
name := flag.Arg(1)
|
||||
if name == "" {
|
||||
fmt.Println("Please specify name.")
|
||||
os.Exit(1)
|
||||
}
|
||||
createCmd(*url, *migrationsPath, name)
|
||||
|
||||
case "migrate":
|
||||
verifyMigrationsPath(*migrationsPath)
|
||||
|
@ -77,11 +85,20 @@ func writePipe(pipe chan interface{}) {
|
|||
} else {
|
||||
switch item.(type) {
|
||||
case string:
|
||||
fmt.Println(item.(string))
|
||||
fmt.Println(" ", item.(string))
|
||||
case error:
|
||||
fmt.Println(item.(error).Error())
|
||||
fmt.Println(" ", item.(error).Error())
|
||||
case file.File:
|
||||
f := item.(file.File)
|
||||
direc := " "
|
||||
if f.Direction == direction.Up {
|
||||
direc = " →"
|
||||
} else if f.Direction == direction.Down {
|
||||
direc = "← "
|
||||
}
|
||||
fmt.Printf("%s %s\n", direc, f.FileName)
|
||||
default:
|
||||
fmt.Println("%v", item)
|
||||
fmt.Printf(" %v\n", item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -89,6 +106,12 @@ func writePipe(pipe chan interface{}) {
|
|||
}
|
||||
}
|
||||
|
||||
var timerStart time.Time
|
||||
|
||||
func printTimer() {
|
||||
fmt.Printf("\n%.4f seconds\n", time.Now().Sub(timerStart).Seconds())
|
||||
}
|
||||
|
||||
func createCmd(url, migrationsPath, name string) {
|
||||
migrationFile, err := migrate.Create(url, migrationsPath, name)
|
||||
if err != nil {
|
||||
|
@ -102,33 +125,43 @@ func createCmd(url, migrationsPath, name string) {
|
|||
}
|
||||
|
||||
func upCmd(url, migrationsPath string) {
|
||||
timerStart = time.Now()
|
||||
pipe := pipep.New()
|
||||
go migrate.Up(pipe, url, migrationsPath)
|
||||
writePipe(pipe)
|
||||
printTimer()
|
||||
}
|
||||
|
||||
func downCmd(url, migrationsPath string) {
|
||||
timerStart = time.Now()
|
||||
pipe := pipep.New()
|
||||
go migrate.Down(pipe, url, migrationsPath)
|
||||
writePipe(pipe)
|
||||
printTimer()
|
||||
}
|
||||
|
||||
func redoCmd(url, migrationsPath string) {
|
||||
timerStart = time.Now()
|
||||
pipe := pipep.New()
|
||||
go migrate.Redo(pipe, url, migrationsPath)
|
||||
writePipe(pipe)
|
||||
printTimer()
|
||||
}
|
||||
|
||||
func resetCmd(url, migrationsPath string) {
|
||||
timerStart = time.Now()
|
||||
pipe := pipep.New()
|
||||
go migrate.Reset(pipe, url, migrationsPath)
|
||||
writePipe(pipe)
|
||||
printTimer()
|
||||
}
|
||||
|
||||
func migrateCmd(url, migrationsPath string, relativeN int) {
|
||||
timerStart = time.Now()
|
||||
pipe := pipep.New()
|
||||
go migrate.Migrate(pipe, url, migrationsPath, relativeN)
|
||||
writePipe(pipe)
|
||||
printTimer()
|
||||
}
|
||||
|
||||
func versionCmd(url, migrationsPath string) {
|
||||
|
|
Loading…
Reference in New Issue