update files to not use searchpath anymore

This commit is contained in:
mattes 2014-08-11 03:54:07 +02:00
parent 13acfe3e2e
commit ebc7d37dc5
4 changed files with 35 additions and 74 deletions

View File

@ -20,45 +20,35 @@ Need another driver? Just implement the [Driver interface](http://godoc.org/gith
go get github.com/mattes/migrate
# create new migration
migrate -url="postgres://user@host:port/database" create
migrate -url="postgres://user@host:port/database" -path=./db/migrations create
# apply all *up* migrations
migrate -url="postgres://user@host:port/database" up
migrate -url="postgres://user@host:port/database" -path=./db/migrations up
# apply all *down* migrations
migrate -url="postgres://user@host:port/database" down
migrate -url="postgres://user@host:port/database" -path=./db/migrations down
# roll back the most recently applied migration, then run it again.
migrate -url="postgres://user@host:port/database" redo
migrate -url="postgres://user@host:port/database" -path=./db/migrations redo
# down and up again
migrate -url="postgres://user@host:port/database" reset
migrate -url="postgres://user@host:port/database" -path=./db/migrations reset
# show current migration version
migrate -url="postgres://user@host:port/database" version
migrate -url="postgres://user@host:port/database" -path=./db/migrations version
# apply the next n migrations
migrate -url="postgres://user@host:port/database" migrate +1
migrate -url="postgres://user@host:port/database" migrate +2
migrate -url="postgres://user@host:port/database" migrate +n
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate +1
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate +2
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate +n
# apply the *down* migration of the current version
# and the previous n-1 migrations
migrate -url="postgres://user@host:port/database" migrate -1
migrate -url="postgres://user@host:port/database" migrate -2
migrate -url="postgres://user@host:port/database" migrate -n
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate -1
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate -2
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate -n
```
``migrate`` looks for migration files in the following directories:
```
./db/migrations
./migrations
./db
```
You can explicitly set the search path with ``-path``.
## Usage from within Go
@ -67,10 +57,8 @@ See http://godoc.org/github.com/mattes/migrate/migrate
```golang
import "github.com/mattes/migrate/migrate"
// optionally set search path
// migrate.SetSearchPath("./location1", "./location2")
migrate.Up("postgres://user@host:port/database")
migrate.Up("postgres://user@host:port/database", "./db/migrations")
// ...
// ...
```

View File

@ -23,9 +23,10 @@ func main() {
switch command {
case "create":
if *path != "" {
migrate.SetSearchPath(*path)
fmt.Println("Please specify path")
os.Exit(1)
}
files, err := migrate.Create(*db, "blablabla")
files, err := migrate.Create(*db, *path, "blablabla")
if err != nil {
fmt.Println(err)
os.Exit(1)

View File

@ -6,34 +6,18 @@ import (
"github.com/mattes/migrate/driver"
"github.com/mattes/migrate/file"
"github.com/mattes/migrate/migrate/direction"
"github.com/mattes/migrate/searchpath"
"io/ioutil"
"path"
"strconv"
"strings"
)
func init() {
SetSearchPath("./db/migrations", "./migrations", "./db")
}
// Convenience func for searchpath.SetSearchPath(), so users
// don't have to import searchpath
func SetSearchPath(paths ...string) {
searchpath.SetSearchPath(paths...)
}
func common(db string) (driver.Driver, *file.MigrationFiles, uint64, error) {
func common(db, migrationsPath string) (driver.Driver, *file.MigrationFiles, uint64, error) {
d, err := driver.New(db)
if err != nil {
return nil, nil, 0, err
}
p, err := searchpath.FindPath(file.FilenameRegex(d.FilenameExtension()))
if err != nil {
return nil, nil, 0, err
}
files, err := file.ReadMigrationFiles(p, file.FilenameRegex(d.FilenameExtension()))
files, err := file.ReadMigrationFiles(migrationsPath, file.FilenameRegex(d.FilenameExtension()))
if err != nil {
return nil, nil, 0, err
}
@ -44,8 +28,8 @@ func common(db string) (driver.Driver, *file.MigrationFiles, uint64, error) {
return d, &files, version, nil
}
func Up(db string) error {
d, files, version, err := common(db)
func Up(db, migrationsPath string) error {
d, files, version, err := common(db, migrationsPath)
if err != nil {
return err
}
@ -60,8 +44,8 @@ func Up(db string) error {
return errors.New("No migrations to apply.")
}
func Down(db string) error {
d, files, version, err := common(db)
func Down(db, migrationsPath string) error {
d, files, version, err := common(db, migrationsPath)
if err != nil {
return err
}
@ -76,8 +60,8 @@ func Down(db string) error {
return errors.New("No migrations to apply.")
}
func Redo(db string) error {
d, files, version, err := common(db)
func Redo(db, migrationsPath string) error {
d, files, version, err := common(db, migrationsPath)
if err != nil {
return err
}
@ -100,8 +84,8 @@ func Redo(db string) error {
return errors.New("No migrations to apply.")
}
func Reset(db string) error {
d, files, version, err := common(db)
func Reset(db, migrationsPath string) error {
d, files, version, err := common(db, migrationsPath)
if err != nil {
return err
}
@ -124,8 +108,8 @@ func Reset(db string) error {
return errors.New("No migrations to apply.")
}
func Migrate(db string, relativeN int) error {
d, files, version, err := common(db)
func Migrate(db, migrationsPath string, relativeN int) error {
d, files, version, err := common(db, migrationsPath)
if err != nil {
return err
}
@ -146,7 +130,7 @@ func Migrate(db string, relativeN int) error {
return errors.New("No migrations to apply.")
}
func Version(db string) (version uint64, err error) {
func Version(db, migrationsPath string) (version uint64, err error) {
d, err := driver.New(db)
if err != nil {
return 0, err
@ -154,22 +138,12 @@ func Version(db string) (version uint64, err error) {
return d.Version()
}
func Create(db, name string) (*file.MigrationFile, error) {
func Create(db, migrationsPath, name string) (*file.MigrationFile, error) {
d, err := driver.New(db)
if err != nil {
return nil, err
}
p, _ := searchpath.FindPath(file.FilenameRegex(d.FilenameExtension()))
if p == "" {
paths := searchpath.GetSearchPath()
if len(paths) > 0 {
p = paths[0]
} else {
return nil, errors.New("Please specify at least one search path.")
}
}
files, err := file.ReadMigrationFiles(p, file.FilenameRegex(d.FilenameExtension()))
files, err := file.ReadMigrationFiles(migrationsPath, file.FilenameRegex(d.FilenameExtension()))
if err != nil {
return nil, err
}
@ -193,14 +167,14 @@ func Create(db, name string) (*file.MigrationFile, error) {
mfile := &file.MigrationFile{
Version: version,
UpFile: &file.File{
Path: p,
Path: migrationsPath,
FileName: fmt.Sprintf(filenamef, versionStr, name, "up", d.FilenameExtension()),
Name: name,
Content: []byte(""),
Direction: direction.Up,
},
DownFile: &file.File{
Path: p,
Path: migrationsPath,
FileName: fmt.Sprintf(filenamef, versionStr, name, "down", d.FilenameExtension()),
Name: name,
Content: []byte(""),

View File

@ -1,7 +1,6 @@
package migrate
import (
"github.com/mattes/migrate/searchpath"
"io/ioutil"
"testing"
)
@ -11,11 +10,10 @@ func TestCreate(t *testing.T) {
if err != nil {
t.Fatal(err)
}
searchpath.SetSearchPath(tmpdir)
if _, err := Create("postgres://localhost/migratetest?sslmode=disable", "test_migration"); err != nil {
if _, err := Create("postgres://localhost/migratetest?sslmode=disable", tmpdir, "test_migration"); err != nil {
t.Fatal(err)
}
if _, err := Create("postgres://localhost/migratetest?sslmode=disable", "another migration"); err != nil {
if _, err := Create("postgres://localhost/migratetest?sslmode=disable", tmpdir, "another migration"); err != nil {
t.Fatal(err)
}