2017-02-08 06:01:29 +00:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
nurl "net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
2017-03-03 20:02:32 +00:00
|
|
|
"path/filepath"
|
2017-02-08 06:01:29 +00:00
|
|
|
|
2018-01-19 18:56:55 +00:00
|
|
|
"github.com/golang-migrate/migrate/source"
|
2017-02-08 06:01:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
source.Register("file", &File{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type File struct {
|
|
|
|
url string
|
|
|
|
path string
|
|
|
|
migrations *source.Migrations
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Open(url string) (source.Driver, error) {
|
|
|
|
u, err := nurl.Parse(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:02:32 +00:00
|
|
|
// concat host and path to restore full path
|
|
|
|
// host might be `.`
|
|
|
|
p := u.Host + u.Path
|
|
|
|
|
|
|
|
if len(p) == 0 {
|
|
|
|
// default to current directory if no path
|
2017-02-08 06:01:29 +00:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-03 20:02:32 +00:00
|
|
|
p = wd
|
|
|
|
|
|
|
|
} else if p[0:1] == "." || p[0:1] != "/" {
|
|
|
|
// make path absolute if relative
|
|
|
|
abs, err := filepath.Abs(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p = abs
|
2017-02-08 06:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// scan directory
|
2017-03-03 20:02:32 +00:00
|
|
|
files, err := ioutil.ReadDir(p)
|
2017-02-08 06:01:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nf := &File{
|
|
|
|
url: url,
|
2017-03-03 20:02:32 +00:00
|
|
|
path: p,
|
2017-02-08 06:01:29 +00:00
|
|
|
migrations: source.NewMigrations(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fi := range files {
|
|
|
|
if !fi.IsDir() {
|
|
|
|
m, err := source.DefaultParse(fi.Name())
|
|
|
|
if err != nil {
|
|
|
|
continue // ignore files that we can't parse
|
|
|
|
}
|
|
|
|
if !nf.migrations.Append(m) {
|
|
|
|
return nil, fmt.Errorf("unable to parse file %v", fi.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Close() error {
|
|
|
|
// nothing do to here
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) First() (version uint, err error) {
|
|
|
|
if v, ok := f.migrations.First(); !ok {
|
2018-05-31 06:40:15 +00:00
|
|
|
return 0, &os.PathError{Op: "first", Path: f.path, Err: os.ErrNotExist}
|
2017-02-08 06:01:29 +00:00
|
|
|
} else {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Prev(version uint) (prevVersion uint, err error) {
|
|
|
|
if v, ok := f.migrations.Prev(version); !ok {
|
2018-05-31 06:40:15 +00:00
|
|
|
return 0, &os.PathError{Op: fmt.Sprintf("prev for version %v", version), Path: f.path, Err: os.ErrNotExist}
|
2017-02-08 06:01:29 +00:00
|
|
|
} else {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Next(version uint) (nextVersion uint, err error) {
|
|
|
|
if v, ok := f.migrations.Next(version); !ok {
|
2018-05-31 06:40:15 +00:00
|
|
|
return 0, &os.PathError{Op: fmt.Sprintf("next for version %v", version), Path: f.path, Err: os.ErrNotExist}
|
2017-02-08 06:01:29 +00:00
|
|
|
} else {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) ReadUp(version uint) (r io.ReadCloser, identifier string, err error) {
|
|
|
|
if m, ok := f.migrations.Up(version); ok {
|
|
|
|
r, err := os.Open(path.Join(f.path, m.Raw))
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
return r, m.Identifier, nil
|
|
|
|
}
|
2018-05-31 06:40:15 +00:00
|
|
|
return nil, "", &os.PathError{Op: fmt.Sprintf("read version %v", version), Path: f.path, Err: os.ErrNotExist}
|
2017-02-08 06:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) ReadDown(version uint) (r io.ReadCloser, identifier string, err error) {
|
|
|
|
if m, ok := f.migrations.Down(version); ok {
|
|
|
|
r, err := os.Open(path.Join(f.path, m.Raw))
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
return r, m.Identifier, nil
|
|
|
|
}
|
2018-05-31 06:40:15 +00:00
|
|
|
return nil, "", &os.PathError{Op: fmt.Sprintf("read version %v", version), Path: f.path, Err: os.ErrNotExist}
|
2017-02-08 06:01:29 +00:00
|
|
|
}
|