Skip to content

Commit 615fda0

Browse files
author
Miguel Molina
authored
Merge pull request #180 from erizocosmico/fix/migrations-dir
fix migration command directory in windows
2 parents 9605c1b + 67669c5 commit 615fda0

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

generator/cli/kallax/cmd/migrate.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"path/filepath"
6+
"strings"
67

78
"github.com/mattes/migrate"
89
_ "github.com/mattes/migrate/database/postgres"
@@ -146,7 +147,7 @@ func runMigrationAction(fn runMigrationFunc) cli.ActionFunc {
146147
return fmt.Errorf("kallax: cannot get absolute path of `dir`: %s", err)
147148
}
148149

149-
m, err := migrate.New(fmt.Sprintf("file://%s", dir), fmt.Sprintf("postgres://%s", dsn))
150+
m, err := migrate.New(pathToFileURL(dir), fmt.Sprintf("postgres://%s", dsn))
150151
if err != nil {
151152
return fmt.Errorf("kallax: unable to open a connection with the database: %s", err)
152153
}
@@ -155,6 +156,14 @@ func runMigrationAction(fn runMigrationFunc) cli.ActionFunc {
155156
}
156157
}
157158

159+
func pathToFileURL(path string) string {
160+
path = strings.Replace(path, "\\", "/", -1)
161+
if !strings.HasPrefix(path, "/") {
162+
path = "/" + path
163+
}
164+
return fmt.Sprintf("file://%s", path)
165+
}
166+
158167
func migrateAction(c *cli.Context) error {
159168
dirs := c.StringSlice("input")
160169
dir := c.String("out")
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cmd
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestPathToFileURL(t *testing.T) {
10+
cases := []struct {
11+
input string
12+
expected string
13+
}{
14+
{`c:\foo\bar\baz`, "file:///c:/foo/bar/baz"},
15+
{"/foo/bar/baz", "file:///foo/bar/baz"},
16+
}
17+
18+
for _, tt := range cases {
19+
require.Equal(t, tt.expected, pathToFileURL(tt.input), tt.input)
20+
}
21+
}

0 commit comments

Comments
 (0)