use filepath rather than path to ensure we're dealing with filenames in an OS compatible way. fixes #2.

pull/2/head
Liam Staskawicz 2013-02-24 23:05:53 -08:00
parent 003f86c223
commit 3c6b6c35c4
5 changed files with 14 additions and 16 deletions

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"path"
"path/filepath" "path/filepath"
"text/template" "text/template"
"time" "time"
@ -42,7 +41,7 @@ func createRun(cmd *Command, args ...string) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
fpath := path.Join(conf.MigrationsDir, filename) fpath := filepath.Join(conf.MigrationsDir, filename)
var tmpl *template.Template var tmpl *template.Template
if migrationType == "sql" { if migrationType == "sql" {

View File

@ -4,7 +4,7 @@ import (
"database/sql" "database/sql"
"fmt" "fmt"
"log" "log"
"path" "path/filepath"
"time" "time"
) )
@ -50,7 +50,7 @@ func statusRun(cmd *Command, args ...string) {
fmt.Println(" Applied At Migration") fmt.Println(" Applied At Migration")
fmt.Println(" =======================================") fmt.Println(" =======================================")
for _, m := range mm.Migrations { for _, m := range mm.Migrations {
printMigrationStatus(db, m.Version, path.Base(m.Source)) printMigrationStatus(db, m.Version, filepath.Base(m.Source))
} }
} }

View File

@ -6,7 +6,7 @@ import (
"github.com/bmizerany/pq" "github.com/bmizerany/pq"
"github.com/kylelemons/go-gypsy/yaml" "github.com/kylelemons/go-gypsy/yaml"
"os" "os"
"path" "path/filepath"
) )
// global options. available to any subcommands. // global options. available to any subcommands.
@ -23,7 +23,7 @@ type DBConf struct {
// extract configuration details from the given file // extract configuration details from the given file
func MakeDBConf() (*DBConf, error) { func MakeDBConf() (*DBConf, error) {
cfgFile := path.Join(*dbPath, "dbconf.yml") cfgFile := filepath.Join(*dbPath, "dbconf.yml")
f, err := yaml.ReadFile(cfgFile) f, err := yaml.ReadFile(cfgFile)
if err != nil { if err != nil {
@ -51,7 +51,7 @@ func MakeDBConf() (*DBConf, error) {
} }
return &DBConf{ return &DBConf{
MigrationsDir: path.Join(*dbPath, "migrations"), MigrationsDir: filepath.Join(*dbPath, "migrations"),
Env: *dbEnv, Env: *dbEnv,
Driver: drv, Driver: drv,
OpenStr: open, OpenStr: open,

View File

@ -8,7 +8,6 @@ import (
_ "github.com/ziutek/mymysql/godrv" _ "github.com/ziutek/mymysql/godrv"
"log" "log"
"os" "os"
"path"
"path/filepath" "path/filepath"
"sort" "sort"
"strconv" "strconv"
@ -73,7 +72,7 @@ func runMigrations(conf *DBConf, migrationsDir string, target int64) {
var e error var e error
switch path.Ext(m.Source) { switch filepath.Ext(m.Source) {
case ".go": case ".go":
e = runGoMigration(conf, m.Source, m.Version, mm.Direction) e = runGoMigration(conf, m.Source, m.Version, mm.Direction)
case ".sql": case ".sql":
@ -84,7 +83,7 @@ func runMigrations(conf *DBConf, migrationsDir string, target int64) {
log.Fatalf("FAIL %v, quitting migration", e) log.Fatalf("FAIL %v, quitting migration", e)
} }
fmt.Println("OK ", path.Base(m.Source)) fmt.Println("OK ", filepath.Base(m.Source))
} }
} }
@ -104,7 +103,7 @@ func collectMigrations(dirpath string, current, target int64) (mm *MigrationMap,
for _, m := range mm.Migrations { for _, m := range mm.Migrations {
if v == m.Version { if v == m.Version {
log.Fatalf("more than one file specifies the migration for version %d (%s and %s)", log.Fatalf("more than one file specifies the migration for version %d (%s and %s)",
v, m.Source, path.Join(dirpath, name)) v, m.Source, filepath.Join(dirpath, name))
} }
} }
@ -170,9 +169,9 @@ func (mm *MigrationMap) Sort(direction bool) {
// and ext specifies the type of migration // and ext specifies the type of migration
func numericComponent(name string) (int64, error) { func numericComponent(name string) (int64, error) {
base := path.Base(name) base := filepath.Base(name)
if ext := path.Ext(base); ext != ".go" && ext != ".sql" { if ext := filepath.Ext(base); ext != ".go" && ext != ".sql" {
return 0, errors.New("not a recognized migration file type") return 0, errors.New("not a recognized migration file type")
} }

View File

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"path" "path/filepath"
"strings" "strings"
) )
@ -53,13 +53,13 @@ func runSQLMigration(db *sql.DB, script string, v int64, direction bool) error {
if _, err = txn.Exec(query); err != nil { if _, err = txn.Exec(query); err != nil {
txn.Rollback() txn.Rollback()
log.Fatalf("FAIL %s (%v), quitting migration.", path.Base(script), err) log.Fatalf("FAIL %s (%v), quitting migration.", filepath.Base(script), err)
return err return err
} }
} }
if err = finalizeMigration(txn, direction, v); err != nil { if err = finalizeMigration(txn, direction, v); err != nil {
log.Fatalf("error finalizing migration %s, quitting. (%v)", path.Base(script), err) log.Fatalf("error finalizing migration %s, quitting. (%v)", filepath.Base(script), err)
} }
return nil return nil