Skip to content

Commit

Permalink
Move create migrations files helper to utils pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
chapsuk committed Apr 30, 2018
1 parent d90e9fd commit eab953a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
24 changes: 3 additions & 21 deletions driver/migrate/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package migrate
import (
"database/sql"
"fmt"
"os"
"strconv"
"time"

"github.com/chapsuk/miga/utils"

"github.com/chapsuk/miga/logger"
orig "github.com/mattes/migrate"
Expand Down Expand Up @@ -47,16 +47,7 @@ func New(dialect, dsn, tableName, dir string) (*Migrator, error) {
}

func (m *Migrator) Create(name, ext string) error {
timestamp := time.Now().Unix()
base := fmt.Sprintf("%v/%v_%v.", m.dir, timestamp, name)
os.MkdirAll(m.dir, os.ModePerm)

err := createFile(base + "up." + ext)
if err != nil {
return err
}

return createFile(base + "down." + ext)
return utils.CreateMigrationsFiles(m.dir, name, ext)
}

func (m *Migrator) Down() error {
Expand Down Expand Up @@ -142,15 +133,6 @@ func versionToUint(version string) (uint, error) {
return uint(v), nil
}

func createFile(fname string) error {
_, err := os.Create(fname)
if err != nil {
return err
}
logger.G().Infof("Create migrations file: %s", fname)
return nil
}

type migrateLogger struct{}

func (l *migrateLogger) Printf(format string, v ...interface{}) {
Expand Down
2 changes: 1 addition & 1 deletion miga.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
driver: migrate
driver: goose

postgres:
dsn: "host=127.0.0.1 user=postgres password=postgres port=5432 sslmode=disable database=miga"
Expand Down
31 changes: 31 additions & 0 deletions utils/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package utils

import (
"fmt"
"os"
"time"

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

func CreateMigrationsFiles(dir, name, ext string) error {
timestamp := time.Now().Unix()
base := fmt.Sprintf("%v/%v_%v.", dir, timestamp, name)
os.MkdirAll(dir, os.ModePerm)

err := createFile(base + "up." + ext)
if err != nil {
return err
}

return createFile(base + "down." + ext)
}

func createFile(fname string) error {
_, err := os.Create(fname)
if err != nil {
return err
}
logger.G().Infof("Create migrations file: %s", fname)
return nil
}

0 comments on commit eab953a

Please sign in to comment.