Skip to content

Commit

Permalink
Check mysql, set logger for goose, only sql format
Browse files Browse the repository at this point in the history
  • Loading branch information
chapsuk committed Apr 30, 2018
1 parent eab953a commit 3446394
Show file tree
Hide file tree
Showing 18 changed files with 107 additions and 48 deletions.
20 changes: 18 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
.PHONY: postgres_up
postgres_up:
docker run -d \
-p 5432:5432 \
-e POSTGRES_USER=postgres \
-e POSTGRES_DB=miga \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_USER=user \
-e POSTGRES_PASSWORD=password \
--name=miga-pg postgres:9.6.5-alpine

.PHONY: postgres_down
postgres_down:
docker rm -f miga-pg

.PHONY: mysql_up
mysql_up:
docker run -d \
-p 3306:3306 \
-e MYSQL_DATABASE=miga \
-e MYSQL_USER=user \
-e MYSQL_PASSWORD=password \
-e MYSQL_ROOT_PASSWORD=mysql \
--name=miga-mysql mysql:5.7

.PHONY: mysql_down
mysql_down:
docker rm -f miga-mysql
17 changes: 9 additions & 8 deletions commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ import (
"gopkg.in/urfave/cli.v2"
)

// Create migrations files with given name and extension
func Create(ctx *cli.Context, d driver.Interface) error {
name := ctx.Args().Get(0)
if len(name) == 0 {
return errors.New("NAME required")
}

ext := ctx.Args().Get(1)
switch ext {
case "sql":
case "go":
default:
ext = "sql"
}
// ext := ctx.Args().Get(1)
// switch ext {
// case "sql":
// case "go":
// default:
// ext = "sql"
// }

return d.Create(name, ext)
return d.Create(name, "sql")
}
2 changes: 2 additions & 0 deletions commands/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"gopkg.in/urfave/cli.v2"
)

// Down rollback last migration
func Down(ctx *cli.Context, d driver.Interface) error {
return d.Down()
}

// DownTo rollback migrations one by one from current until version from command args
func DownTo(ctx *cli.Context, d driver.Interface) error {
version, err := parseVersion(ctx)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions commands/migrate/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ import (

var migrator driver.Interface

// Command returns migration CLI command
func Command() *cli.Command {
return &cli.Command{
Name: "migrate",
Aliases: []string{"m"},
Usage: "migrate command",
Usage: "Migrations root command",
Before: func(ctx *cli.Context) (err error) {
migrator, err = driver.New(config.MigrateDriverConfig())
return
},
Subcommands: []*cli.Command{
&cli.Command{
Name: "create",
Usage: "Creates new migration file with next version",
ArgsUsage: "NAME [sql|go]",
Usage: "Creates new migration sql file",
ArgsUsage: "NAME",
Action: func(ctx *cli.Context) error {
return commands.Create(ctx, migrator)
},
Expand Down
1 change: 1 addition & 0 deletions commands/redo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"gopkg.in/urfave/cli.v2"
)

// Redo rollback and rerun last migration
func Redo(ctx *cli.Context, d driver.Interface) error {
return d.Redo()
}
1 change: 1 addition & 0 deletions commands/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"gopkg.in/urfave/cli.v2"
)

// Reset rollback all migrations
func Reset(ctx *cli.Context, d driver.Interface) error {
if !ctx.Bool("force") {
logger.G().Info("Rollback all migrations! Are you sure? (yes/no):")
Expand Down
7 changes: 4 additions & 3 deletions commands/seed/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ import (

var seeder driver.Interface

// Command returns seed CLI command
func Command() *cli.Command {
return &cli.Command{
Name: "seed",
Aliases: []string{"s"},
Usage: "seed command",
Usage: "Seeding root command, see",
Before: func(ctx *cli.Context) (err error) {
seeder, err = driver.New(config.SeedDriverConfig())
return
},
Subcommands: []*cli.Command{
&cli.Command{
Name: "create",
Usage: "Creates new seed file with next version",
ArgsUsage: "NAME [sql|go]",
Usage: "Creates new seed sql file with next version",
ArgsUsage: "NAME",
Action: func(ctx *cli.Context) error {
return commands.Create(ctx, seeder)
},
Expand Down
1 change: 1 addition & 0 deletions commands/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"gopkg.in/urfave/cli.v2"
)

// Status print current migrations state
func Status(ctx *cli.Context, d driver.Interface) error {
return d.Status()
}
2 changes: 2 additions & 0 deletions commands/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"gopkg.in/urfave/cli.v2"
)

// Up to latest available migration
func Up(ctx *cli.Context, d driver.Interface) error {
return d.Up()
}

// UpTo up to version from command args
func UpTo(ctx *cli.Context, d driver.Interface) error {
version, err := parseVersion(ctx)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"gopkg.in/urfave/cli.v2"
)

// Version print current db version
func Version(ctx *cli.Context, d driver.Interface) error {
return d.Version()
}
1 change: 1 addition & 0 deletions config/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

var migrateConfig, seedConfig *driver.Config

// Init configuration with viper
func Init(appName, cfg string) error {
viper.SetConfigFile(cfg)
viper.SetEnvPrefix(appName)
Expand Down
2 changes: 2 additions & 0 deletions driver/goose/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package goose
import (
"database/sql"

"github.com/chapsuk/miga/utils"
orig "github.com/pressly/goose"
)

Expand All @@ -18,6 +19,7 @@ func New(dialect, dsn, tableName, dir string) (*Goose, error) {
}

orig.SetDBVersionTableName(tableName)
orig.SetLogger(&utils.StdLogger{})

db, err := sql.Open(dialect, dsn)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion driver/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ func New(cfg *Config) (Interface, error) {
cfg.Dir,
)
default:
return nil, errors.New("unsupported driver")
return nil, errors.New("unsupported migrations driver")
}
}
35 changes: 19 additions & 16 deletions driver/migrate/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package migrate

import (
"database/sql"
"errors"
"fmt"
"strconv"

"github.com/chapsuk/miga/utils"

"github.com/chapsuk/miga/logger"
"github.com/chapsuk/miga/utils"
orig "github.com/mattes/migrate"
"github.com/mattes/migrate/database"
"github.com/mattes/migrate/database/mysql"
"github.com/mattes/migrate/database/postgres"

_ "github.com/mattes/migrate/source/file"
Expand All @@ -26,9 +28,20 @@ func New(dialect, dsn, tableName, dir string) (*Migrator, error) {
return nil, err
}

driver, err := postgres.WithInstance(db, &postgres.Config{
MigrationsTable: tableName,
})
var driver database.Driver
switch dialect {
case "postgres":
driver, err = postgres.WithInstance(db, &postgres.Config{
MigrationsTable: tableName,
})
case "mysql":
driver, err = mysql.WithInstance(db, &mysql.Config{
MigrationsTable: tableName,
})
default:
return nil, errors.New("Unsupported dialect")
}

if err != nil {
return nil, err
}
Expand All @@ -37,7 +50,7 @@ func New(dialect, dsn, tableName, dir string) (*Migrator, error) {
if err != nil {
return nil, err
}
m.Log = &migrateLogger{}
m.Log = &utils.StdLogger{}

return &Migrator{
backend: m,
Expand Down Expand Up @@ -132,13 +145,3 @@ func versionToUint(version string) (uint, error) {
}
return uint(v), nil
}

type migrateLogger struct{}

func (l *migrateLogger) Printf(format string, v ...interface{}) {
logger.G().Infof(format, v...)
}

func (l *migrateLogger) Verbose() bool {
return true
}
4 changes: 2 additions & 2 deletions logger/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func Init(appName, appVersion, level, format string) error {
}

logger = l.Sugar().With(
"app", appName,
"version", appVersion,
"app_name", appName,
"app_version", appVersion,
)

return nil
Expand Down
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ func main() {

func initGlobalsFunc() func(*cli.Context) error {
return func(ctx *cli.Context) error {
err := config.Init(ctx.App.Name, ctx.String("config"))
if err != nil {
return err
}

return logger.Init(
err := logger.Init(
ctx.App.Name,
ctx.App.Version,
ctx.String("log.level"),
ctx.String("log.format"),
)
if err != nil {
return err
}

return config.Init(ctx.App.Name, ctx.String("config"))
}
}
14 changes: 7 additions & 7 deletions miga.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
driver: goose

postgres:
dsn: "host=127.0.0.1 user=postgres password=postgres port=5432 sslmode=disable database=miga"
# postgres:
# dsn: "host=127.0.0.1 user=user password=password port=5432 sslmode=disable database=miga"

mysql:
dsn: ""
dsn: "user:password@tcp(127.0.0.1:3306)/miga"

migrate:
path: ./migrations
table_name: app_db_version
path: ./migrations/goose
table_name: db_version

seed:
path: ./seeds
table_name: app_seed_version
path: ./seeds/goose
table_name: seed_version
26 changes: 26 additions & 0 deletions utils/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package utils

import "github.com/chapsuk/miga/logger"

type StdLogger struct{}

func (l *StdLogger) Printf(format string, v ...interface{}) {
logger.G().Infof(format, v...)
}

func (l *StdLogger) Verbose() bool {
return true
}

func (l *StdLogger) Fatal(v ...interface{}) {
logger.G().Fatal(v)
}
func (l *StdLogger) Fatalf(format string, v ...interface{}) {
logger.G().Fatalf(format, v...)
}
func (l *StdLogger) Print(v ...interface{}) {
logger.G().Info(v...)
}
func (l *StdLogger) Println(v ...interface{}) {
logger.G().Info(v...)
}

0 comments on commit 3446394

Please sign in to comment.