Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ebc7d37

Browse files
committed
update files to not use searchpath anymore
1 parent 13acfe3 commit ebc7d37

File tree

4 files changed

+35
-74
lines changed

4 files changed

+35
-74
lines changed

README.md

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,45 +20,35 @@ Need another driver? Just implement the [Driver interface](http://godoc.org/gith
2020
go get github.com/mattes/migrate
2121

2222
# create new migration
23-
migrate -url="postgres://user@host:port/database" create
23+
migrate -url="postgres://user@host:port/database" -path=./db/migrations create
2424

2525
# apply all *up* migrations
26-
migrate -url="postgres://user@host:port/database" up
26+
migrate -url="postgres://user@host:port/database" -path=./db/migrations up
2727

2828
# apply all *down* migrations
29-
migrate -url="postgres://user@host:port/database" down
29+
migrate -url="postgres://user@host:port/database" -path=./db/migrations down
3030

3131
# roll back the most recently applied migration, then run it again.
32-
migrate -url="postgres://user@host:port/database" redo
32+
migrate -url="postgres://user@host:port/database" -path=./db/migrations redo
3333

3434
# down and up again
35-
migrate -url="postgres://user@host:port/database" reset
35+
migrate -url="postgres://user@host:port/database" -path=./db/migrations reset
3636

3737
# show current migration version
38-
migrate -url="postgres://user@host:port/database" version
38+
migrate -url="postgres://user@host:port/database" -path=./db/migrations version
3939

4040
# apply the next n migrations
41-
migrate -url="postgres://user@host:port/database" migrate +1
42-
migrate -url="postgres://user@host:port/database" migrate +2
43-
migrate -url="postgres://user@host:port/database" migrate +n
41+
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate +1
42+
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate +2
43+
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate +n
4444

4545
# apply the *down* migration of the current version
4646
# and the previous n-1 migrations
47-
migrate -url="postgres://user@host:port/database" migrate -1
48-
migrate -url="postgres://user@host:port/database" migrate -2
49-
migrate -url="postgres://user@host:port/database" migrate -n
47+
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate -1
48+
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate -2
49+
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate -n
5050
```
5151

52-
``migrate`` looks for migration files in the following directories:
53-
54-
```
55-
./db/migrations
56-
./migrations
57-
./db
58-
```
59-
60-
You can explicitly set the search path with ``-path``.
61-
6252

6353
## Usage from within Go
6454

@@ -67,10 +57,8 @@ See http://godoc.org/github.com/mattes/migrate/migrate
6757
```golang
6858
import "github.com/mattes/migrate/migrate"
6959

70-
// optionally set search path
71-
// migrate.SetSearchPath("./location1", "./location2")
7260

73-
migrate.Up("postgres://user@host:port/database")
61+
migrate.Up("postgres://user@host:port/database", "./db/migrations")
7462
// ...
7563
// ...
7664
```

main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ func main() {
2323
switch command {
2424
case "create":
2525
if *path != "" {
26-
migrate.SetSearchPath(*path)
26+
fmt.Println("Please specify path")
27+
os.Exit(1)
2728
}
28-
files, err := migrate.Create(*db, "blablabla")
29+
files, err := migrate.Create(*db, *path, "blablabla")
2930
if err != nil {
3031
fmt.Println(err)
3132
os.Exit(1)

migrate/migrate.go

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,18 @@ import (
66
"github.com/mattes/migrate/driver"
77
"github.com/mattes/migrate/file"
88
"github.com/mattes/migrate/migrate/direction"
9-
"github.com/mattes/migrate/searchpath"
109
"io/ioutil"
1110
"path"
1211
"strconv"
1312
"strings"
1413
)
1514

16-
func init() {
17-
SetSearchPath("./db/migrations", "./migrations", "./db")
18-
}
19-
20-
// Convenience func for searchpath.SetSearchPath(), so users
21-
// don't have to import searchpath
22-
func SetSearchPath(paths ...string) {
23-
searchpath.SetSearchPath(paths...)
24-
}
25-
26-
func common(db string) (driver.Driver, *file.MigrationFiles, uint64, error) {
15+
func common(db, migrationsPath string) (driver.Driver, *file.MigrationFiles, uint64, error) {
2716
d, err := driver.New(db)
2817
if err != nil {
2918
return nil, nil, 0, err
3019
}
31-
32-
p, err := searchpath.FindPath(file.FilenameRegex(d.FilenameExtension()))
33-
if err != nil {
34-
return nil, nil, 0, err
35-
}
36-
files, err := file.ReadMigrationFiles(p, file.FilenameRegex(d.FilenameExtension()))
20+
files, err := file.ReadMigrationFiles(migrationsPath, file.FilenameRegex(d.FilenameExtension()))
3721
if err != nil {
3822
return nil, nil, 0, err
3923
}
@@ -44,8 +28,8 @@ func common(db string) (driver.Driver, *file.MigrationFiles, uint64, error) {
4428
return d, &files, version, nil
4529
}
4630

47-
func Up(db string) error {
48-
d, files, version, err := common(db)
31+
func Up(db, migrationsPath string) error {
32+
d, files, version, err := common(db, migrationsPath)
4933
if err != nil {
5034
return err
5135
}
@@ -60,8 +44,8 @@ func Up(db string) error {
6044
return errors.New("No migrations to apply.")
6145
}
6246

63-
func Down(db string) error {
64-
d, files, version, err := common(db)
47+
func Down(db, migrationsPath string) error {
48+
d, files, version, err := common(db, migrationsPath)
6549
if err != nil {
6650
return err
6751
}
@@ -76,8 +60,8 @@ func Down(db string) error {
7660
return errors.New("No migrations to apply.")
7761
}
7862

79-
func Redo(db string) error {
80-
d, files, version, err := common(db)
63+
func Redo(db, migrationsPath string) error {
64+
d, files, version, err := common(db, migrationsPath)
8165
if err != nil {
8266
return err
8367
}
@@ -100,8 +84,8 @@ func Redo(db string) error {
10084
return errors.New("No migrations to apply.")
10185
}
10286

103-
func Reset(db string) error {
104-
d, files, version, err := common(db)
87+
func Reset(db, migrationsPath string) error {
88+
d, files, version, err := common(db, migrationsPath)
10589
if err != nil {
10690
return err
10791
}
@@ -124,8 +108,8 @@ func Reset(db string) error {
124108
return errors.New("No migrations to apply.")
125109
}
126110

127-
func Migrate(db string, relativeN int) error {
128-
d, files, version, err := common(db)
111+
func Migrate(db, migrationsPath string, relativeN int) error {
112+
d, files, version, err := common(db, migrationsPath)
129113
if err != nil {
130114
return err
131115
}
@@ -146,30 +130,20 @@ func Migrate(db string, relativeN int) error {
146130
return errors.New("No migrations to apply.")
147131
}
148132

149-
func Version(db string) (version uint64, err error) {
133+
func Version(db, migrationsPath string) (version uint64, err error) {
150134
d, err := driver.New(db)
151135
if err != nil {
152136
return 0, err
153137
}
154138
return d.Version()
155139
}
156140

157-
func Create(db, name string) (*file.MigrationFile, error) {
141+
func Create(db, migrationsPath, name string) (*file.MigrationFile, error) {
158142
d, err := driver.New(db)
159143
if err != nil {
160144
return nil, err
161145
}
162-
p, _ := searchpath.FindPath(file.FilenameRegex(d.FilenameExtension()))
163-
if p == "" {
164-
paths := searchpath.GetSearchPath()
165-
if len(paths) > 0 {
166-
p = paths[0]
167-
} else {
168-
return nil, errors.New("Please specify at least one search path.")
169-
}
170-
}
171-
172-
files, err := file.ReadMigrationFiles(p, file.FilenameRegex(d.FilenameExtension()))
146+
files, err := file.ReadMigrationFiles(migrationsPath, file.FilenameRegex(d.FilenameExtension()))
173147
if err != nil {
174148
return nil, err
175149
}
@@ -193,14 +167,14 @@ func Create(db, name string) (*file.MigrationFile, error) {
193167
mfile := &file.MigrationFile{
194168
Version: version,
195169
UpFile: &file.File{
196-
Path: p,
170+
Path: migrationsPath,
197171
FileName: fmt.Sprintf(filenamef, versionStr, name, "up", d.FilenameExtension()),
198172
Name: name,
199173
Content: []byte(""),
200174
Direction: direction.Up,
201175
},
202176
DownFile: &file.File{
203-
Path: p,
177+
Path: migrationsPath,
204178
FileName: fmt.Sprintf(filenamef, versionStr, name, "down", d.FilenameExtension()),
205179
Name: name,
206180
Content: []byte(""),

migrate/migrate_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package migrate
22

33
import (
4-
"github.com/mattes/migrate/searchpath"
54
"io/ioutil"
65
"testing"
76
)
@@ -11,11 +10,10 @@ func TestCreate(t *testing.T) {
1110
if err != nil {
1211
t.Fatal(err)
1312
}
14-
searchpath.SetSearchPath(tmpdir)
15-
if _, err := Create("postgres://localhost/migratetest?sslmode=disable", "test_migration"); err != nil {
13+
if _, err := Create("postgres://localhost/migratetest?sslmode=disable", tmpdir, "test_migration"); err != nil {
1614
t.Fatal(err)
1715
}
18-
if _, err := Create("postgres://localhost/migratetest?sslmode=disable", "another migration"); err != nil {
16+
if _, err := Create("postgres://localhost/migratetest?sslmode=disable", tmpdir, "another migration"); err != nil {
1917
t.Fatal(err)
2018
}
2119

0 commit comments

Comments
 (0)