mirror of
https://github.com/status-im/migrate.git
synced 2025-02-23 16:28:08 +00:00
update files to not use searchpath anymore
This commit is contained in:
parent
13acfe3e2e
commit
ebc7d37dc5
38
README.md
38
README.md
@ -20,45 +20,35 @@ Need another driver? Just implement the [Driver interface](http://godoc.org/gith
|
|||||||
go get github.com/mattes/migrate
|
go get github.com/mattes/migrate
|
||||||
|
|
||||||
# create new migration
|
# 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
|
# 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
|
# 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.
|
# 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
|
# 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
|
# 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
|
# apply the next n migrations
|
||||||
migrate -url="postgres://user@host:port/database" migrate +1
|
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate +1
|
||||||
migrate -url="postgres://user@host:port/database" migrate +2
|
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate +2
|
||||||
migrate -url="postgres://user@host:port/database" migrate +n
|
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate +n
|
||||||
|
|
||||||
# apply the *down* migration of the current version
|
# apply the *down* migration of the current version
|
||||||
# and the previous n-1 migrations
|
# and the previous n-1 migrations
|
||||||
migrate -url="postgres://user@host:port/database" migrate -1
|
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate -1
|
||||||
migrate -url="postgres://user@host:port/database" migrate -2
|
migrate -url="postgres://user@host:port/database" -path=./db/migrations migrate -2
|
||||||
migrate -url="postgres://user@host:port/database" migrate -n
|
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
|
## Usage from within Go
|
||||||
|
|
||||||
@ -67,10 +57,8 @@ See http://godoc.org/github.com/mattes/migrate/migrate
|
|||||||
```golang
|
```golang
|
||||||
import "github.com/mattes/migrate/migrate"
|
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")
|
||||||
// ...
|
// ...
|
||||||
// ...
|
// ...
|
||||||
```
|
```
|
||||||
|
5
main.go
5
main.go
@ -23,9 +23,10 @@ func main() {
|
|||||||
switch command {
|
switch command {
|
||||||
case "create":
|
case "create":
|
||||||
if *path != "" {
|
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 {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -6,34 +6,18 @@ import (
|
|||||||
"github.com/mattes/migrate/driver"
|
"github.com/mattes/migrate/driver"
|
||||||
"github.com/mattes/migrate/file"
|
"github.com/mattes/migrate/file"
|
||||||
"github.com/mattes/migrate/migrate/direction"
|
"github.com/mattes/migrate/migrate/direction"
|
||||||
"github.com/mattes/migrate/searchpath"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func common(db, migrationsPath string) (driver.Driver, *file.MigrationFiles, uint64, error) {
|
||||||
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) {
|
|
||||||
d, err := driver.New(db)
|
d, err := driver.New(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, 0, err
|
return nil, nil, 0, err
|
||||||
}
|
}
|
||||||
|
files, err := file.ReadMigrationFiles(migrationsPath, file.FilenameRegex(d.FilenameExtension()))
|
||||||
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()))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, 0, err
|
return nil, nil, 0, err
|
||||||
}
|
}
|
||||||
@ -44,8 +28,8 @@ func common(db string) (driver.Driver, *file.MigrationFiles, uint64, error) {
|
|||||||
return d, &files, version, nil
|
return d, &files, version, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Up(db string) error {
|
func Up(db, migrationsPath string) error {
|
||||||
d, files, version, err := common(db)
|
d, files, version, err := common(db, migrationsPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -60,8 +44,8 @@ func Up(db string) error {
|
|||||||
return errors.New("No migrations to apply.")
|
return errors.New("No migrations to apply.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Down(db string) error {
|
func Down(db, migrationsPath string) error {
|
||||||
d, files, version, err := common(db)
|
d, files, version, err := common(db, migrationsPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -76,8 +60,8 @@ func Down(db string) error {
|
|||||||
return errors.New("No migrations to apply.")
|
return errors.New("No migrations to apply.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Redo(db string) error {
|
func Redo(db, migrationsPath string) error {
|
||||||
d, files, version, err := common(db)
|
d, files, version, err := common(db, migrationsPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -100,8 +84,8 @@ func Redo(db string) error {
|
|||||||
return errors.New("No migrations to apply.")
|
return errors.New("No migrations to apply.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Reset(db string) error {
|
func Reset(db, migrationsPath string) error {
|
||||||
d, files, version, err := common(db)
|
d, files, version, err := common(db, migrationsPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -124,8 +108,8 @@ func Reset(db string) error {
|
|||||||
return errors.New("No migrations to apply.")
|
return errors.New("No migrations to apply.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Migrate(db string, relativeN int) error {
|
func Migrate(db, migrationsPath string, relativeN int) error {
|
||||||
d, files, version, err := common(db)
|
d, files, version, err := common(db, migrationsPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -146,7 +130,7 @@ func Migrate(db string, relativeN int) error {
|
|||||||
return errors.New("No migrations to apply.")
|
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)
|
d, err := driver.New(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@ -154,22 +138,12 @@ func Version(db string) (version uint64, err error) {
|
|||||||
return d.Version()
|
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)
|
d, err := driver.New(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
p, _ := searchpath.FindPath(file.FilenameRegex(d.FilenameExtension()))
|
files, err := file.ReadMigrationFiles(migrationsPath, 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()))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -193,14 +167,14 @@ func Create(db, name string) (*file.MigrationFile, error) {
|
|||||||
mfile := &file.MigrationFile{
|
mfile := &file.MigrationFile{
|
||||||
Version: version,
|
Version: version,
|
||||||
UpFile: &file.File{
|
UpFile: &file.File{
|
||||||
Path: p,
|
Path: migrationsPath,
|
||||||
FileName: fmt.Sprintf(filenamef, versionStr, name, "up", d.FilenameExtension()),
|
FileName: fmt.Sprintf(filenamef, versionStr, name, "up", d.FilenameExtension()),
|
||||||
Name: name,
|
Name: name,
|
||||||
Content: []byte(""),
|
Content: []byte(""),
|
||||||
Direction: direction.Up,
|
Direction: direction.Up,
|
||||||
},
|
},
|
||||||
DownFile: &file.File{
|
DownFile: &file.File{
|
||||||
Path: p,
|
Path: migrationsPath,
|
||||||
FileName: fmt.Sprintf(filenamef, versionStr, name, "down", d.FilenameExtension()),
|
FileName: fmt.Sprintf(filenamef, versionStr, name, "down", d.FilenameExtension()),
|
||||||
Name: name,
|
Name: name,
|
||||||
Content: []byte(""),
|
Content: []byte(""),
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package migrate
|
package migrate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/mattes/migrate/searchpath"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -11,11 +10,10 @@ func TestCreate(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
searchpath.SetSearchPath(tmpdir)
|
if _, err := Create("postgres://localhost/migratetest?sslmode=disable", tmpdir, "test_migration"); err != nil {
|
||||||
if _, err := Create("postgres://localhost/migratetest?sslmode=disable", "test_migration"); err != nil {
|
|
||||||
t.Fatal(err)
|
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)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user