diff --git a/source/file/file.go b/source/file/file.go index 82408b99a..ee10d08d1 100644 --- a/source/file/file.go +++ b/source/file/file.go @@ -1,22 +1,31 @@ package file import ( + "net/http" nurl "net/url" "os" "path/filepath" "github.com/golang-migrate/migrate/v4/source" + "github.com/golang-migrate/migrate/v4/source/httpfs" ) func init() { source.Register("file", &File{}) } -func parseURL(url string) (string, error) { +type File struct { + httpfs.PartialDriver + url string + path string +} + +func (f *File) Open(url string) (source.Driver, error) { u, err := nurl.Parse(url) if err != nil { - return "", err + return nil, err } + // concat host and path to restore full path // host might be `.` p := u.Opaque @@ -28,7 +37,7 @@ func parseURL(url string) (string, error) { // default to current directory if no path wd, err := os.Getwd() if err != nil { - return "", err + return nil, err } p = wd @@ -36,9 +45,17 @@ func parseURL(url string) (string, error) { // make path absolute if relative abs, err := filepath.Abs(p) if err != nil { - return "", err + return nil, err } p = abs } - return p, nil + + nf := &File{ + url: url, + path: p, + } + if err := nf.Init(http.Dir(p), ""); err != nil { + return nil, err + } + return nf, nil } diff --git a/source/file/file_go115.go b/source/file/file_go115.go deleted file mode 100644 index e2857b364..000000000 --- a/source/file/file_go115.go +++ /dev/null @@ -1,32 +0,0 @@ -// +build !go1.16 - -package file - -import ( - "net/http" - - "github.com/golang-migrate/migrate/v4/source" - "github.com/golang-migrate/migrate/v4/source/httpfs" -) - -type File struct { - httpfs.PartialDriver - url string - path string -} - -func (f *File) Open(url string) (source.Driver, error) { - p, err := parseURL(url) - if err != nil { - return nil, err - } - - nf := &File{ - url: url, - path: p, - } - if err := nf.Init(http.Dir(p), ""); err != nil { - return nil, err - } - return nf, nil -} diff --git a/source/file/file_go116.go b/source/file/file_go116.go deleted file mode 100644 index 5203b4bdb..000000000 --- a/source/file/file_go116.go +++ /dev/null @@ -1,31 +0,0 @@ -// +build go1.16 - -package file - -import ( - "os" - - "github.com/golang-migrate/migrate/v4/source" - "github.com/golang-migrate/migrate/v4/source/iofs" -) - -type File struct { - iofs.PartialDriver - url string - path string -} - -func (f *File) Open(url string) (source.Driver, error) { - p, err := parseURL(url) - if err != nil { - return nil, err - } - nf := &File{ - url: url, - path: p, - } - if err := nf.Init(os.DirFS(p), "."); err != nil { - return nil, err - } - return nf, nil -} diff --git a/source/iofs/README.md b/source/iofs/README.md deleted file mode 100644 index d75b328b9..000000000 --- a/source/iofs/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# iofs - -https://pkg.go.dev/github.com/golang-migrate/migrate/v4/source/iofs diff --git a/source/iofs/doc.go b/source/iofs/doc.go deleted file mode 100644 index 6b2c862e0..000000000 --- a/source/iofs/doc.go +++ /dev/null @@ -1,10 +0,0 @@ -/* -Package iofs provides the Go 1.16+ io/fs#FS driver. - -It can accept various file systems (like embed.FS, archive/zip#Reader) implementing io/fs#FS. - -This driver cannot be used with Go versions 1.15 and below. - -Also, Opening with a URL scheme is not supported. -*/ -package iofs diff --git a/source/iofs/example_test.go b/source/iofs/example_test.go deleted file mode 100644 index 87d4e568b..000000000 --- a/source/iofs/example_test.go +++ /dev/null @@ -1,31 +0,0 @@ -// +build go1.16 - -package iofs_test - -import ( - "embed" - "log" - - "github.com/golang-migrate/migrate/v4" - _ "github.com/golang-migrate/migrate/v4/database/postgres" - "github.com/golang-migrate/migrate/v4/source/iofs" -) - -//go:embed testdata/migrations/*.sql -var fs embed.FS - -func Example() { - d, err := iofs.New(fs, "testdata/migrations") - if err != nil { - log.Fatal(err) - } - m, err := migrate.NewWithSourceInstance("iofs", d, "postgres://postgres@localhost/postgres?sslmode=disable") - if err != nil { - log.Fatal(err) - } - err = m.Up() - if err != nil { - // ... - } - // ... -} diff --git a/source/iofs/iofs.go b/source/iofs/iofs.go deleted file mode 100644 index f76e44387..000000000 --- a/source/iofs/iofs.go +++ /dev/null @@ -1,175 +0,0 @@ -// +build go1.16 - -package iofs - -import ( - "errors" - "fmt" - "io" - "io/fs" - "path" - "strconv" - - "github.com/golang-migrate/migrate/v4/source" -) - -type driver struct { - PartialDriver -} - -// New returns a new Driver from io/fs#FS and a relative path. -func New(fsys fs.FS, path string) (source.Driver, error) { - var i driver - if err := i.Init(fsys, path); err != nil { - return nil, fmt.Errorf("failed to init driver with path %s: %w", path, err) - } - return &i, nil -} - -// Open is part of source.Driver interface implementation. -// Open cannot be called on the iofs passthrough driver. -func (d *driver) Open(url string) (source.Driver, error) { - return nil, errors.New("Open() cannot be called on the iofs passthrough driver") -} - -// PartialDriver is a helper service for creating new source drivers working with -// io/fs.FS instances. It implements all source.Driver interface methods -// except for Open(). New driver could embed this struct and add missing Open() -// method. -// -// To prepare PartialDriver for use Init() function. -type PartialDriver struct { - migrations *source.Migrations - fsys fs.FS - path string -} - -// Init prepares not initialized IoFS instance to read migrations from a -// io/fs#FS instance and a relative path. -func (d *PartialDriver) Init(fsys fs.FS, path string) error { - entries, err := fs.ReadDir(fsys, path) - if err != nil { - return err - } - - ms := source.NewMigrations() - for _, e := range entries { - if e.IsDir() { - continue - } - m, err := source.DefaultParse(e.Name()) - if err != nil { - continue - } - file, err := e.Info() - if err != nil { - return err - } - if !ms.Append(m) { - return source.ErrDuplicateMigration{ - Migration: *m, - FileInfo: file, - } - } - } - - d.fsys = fsys - d.path = path - d.migrations = ms - return nil -} - -// Close is part of source.Driver interface implementation. -// Closes the file system if possible. -func (d *PartialDriver) Close() error { - c, ok := d.fsys.(io.Closer) - if !ok { - return nil - } - return c.Close() -} - -// First is part of source.Driver interface implementation. -func (d *PartialDriver) First() (version uint, err error) { - if version, ok := d.migrations.First(); ok { - return version, nil - } - return 0, &fs.PathError{ - Op: "first", - Path: d.path, - Err: fs.ErrNotExist, - } -} - -// Prev is part of source.Driver interface implementation. -func (d *PartialDriver) Prev(version uint) (prevVersion uint, err error) { - if version, ok := d.migrations.Prev(version); ok { - return version, nil - } - return 0, &fs.PathError{ - Op: "prev for version " + strconv.FormatUint(uint64(version), 10), - Path: d.path, - Err: fs.ErrNotExist, - } -} - -// Next is part of source.Driver interface implementation. -func (d *PartialDriver) Next(version uint) (nextVersion uint, err error) { - if version, ok := d.migrations.Next(version); ok { - return version, nil - } - return 0, &fs.PathError{ - Op: "next for version " + strconv.FormatUint(uint64(version), 10), - Path: d.path, - Err: fs.ErrNotExist, - } -} - -// ReadUp is part of source.Driver interface implementation. -func (d *PartialDriver) ReadUp(version uint) (r io.ReadCloser, identifier string, err error) { - if m, ok := d.migrations.Up(version); ok { - body, err := d.open(path.Join(d.path, m.Raw)) - if err != nil { - return nil, "", err - } - return body, m.Identifier, nil - } - return nil, "", &fs.PathError{ - Op: "read up for version " + strconv.FormatUint(uint64(version), 10), - Path: d.path, - Err: fs.ErrNotExist, - } -} - -// ReadDown is part of source.Driver interface implementation. -func (d *PartialDriver) ReadDown(version uint) (r io.ReadCloser, identifier string, err error) { - if m, ok := d.migrations.Down(version); ok { - body, err := d.open(path.Join(d.path, m.Raw)) - if err != nil { - return nil, "", err - } - return body, m.Identifier, nil - } - return nil, "", &fs.PathError{ - Op: "read down for version " + strconv.FormatUint(uint64(version), 10), - Path: d.path, - Err: fs.ErrNotExist, - } -} - -func (d *PartialDriver) open(path string) (fs.File, error) { - f, err := d.fsys.Open(path) - if err == nil { - return f, nil - } - // Some non-standard file systems may return errors that don't include the path, that - // makes debugging harder. - if !errors.As(err, new(*fs.PathError)) { - err = &fs.PathError{ - Op: "open", - Path: path, - Err: err, - } - } - return nil, err -} diff --git a/source/iofs/iofs_test.go b/source/iofs/iofs_test.go deleted file mode 100644 index 2e0def2c7..000000000 --- a/source/iofs/iofs_test.go +++ /dev/null @@ -1,22 +0,0 @@ -// +build go1.16 - -package iofs_test - -import ( - "embed" - "testing" - - "github.com/golang-migrate/migrate/v4/source/iofs" - st "github.com/golang-migrate/migrate/v4/source/testing" -) - -func Test(t *testing.T) { - //go:embed testdata/migrations/*.sql - var fs embed.FS - d, err := iofs.New(fs, "testdata/migrations") - if err != nil { - t.Fatal(err) - } - - st.Test(t, d) -} diff --git a/source/iofs/testdata/migrations/1_foobar.down.sql b/source/iofs/testdata/migrations/1_foobar.down.sql deleted file mode 100644 index 4267951a5..000000000 --- a/source/iofs/testdata/migrations/1_foobar.down.sql +++ /dev/null @@ -1 +0,0 @@ -1 down diff --git a/source/iofs/testdata/migrations/1_foobar.up.sql b/source/iofs/testdata/migrations/1_foobar.up.sql deleted file mode 100644 index 046fd5a5d..000000000 --- a/source/iofs/testdata/migrations/1_foobar.up.sql +++ /dev/null @@ -1 +0,0 @@ -1 up diff --git a/source/iofs/testdata/migrations/3_foobar.up.sql b/source/iofs/testdata/migrations/3_foobar.up.sql deleted file mode 100644 index 77c1b77dc..000000000 --- a/source/iofs/testdata/migrations/3_foobar.up.sql +++ /dev/null @@ -1 +0,0 @@ -3 up diff --git a/source/iofs/testdata/migrations/4_foobar.down.sql b/source/iofs/testdata/migrations/4_foobar.down.sql deleted file mode 100644 index b405d8bd0..000000000 --- a/source/iofs/testdata/migrations/4_foobar.down.sql +++ /dev/null @@ -1 +0,0 @@ -4 down diff --git a/source/iofs/testdata/migrations/4_foobar.up.sql b/source/iofs/testdata/migrations/4_foobar.up.sql deleted file mode 100644 index eba61bb94..000000000 --- a/source/iofs/testdata/migrations/4_foobar.up.sql +++ /dev/null @@ -1 +0,0 @@ -4 up diff --git a/source/iofs/testdata/migrations/5_foobar.down.sql b/source/iofs/testdata/migrations/5_foobar.down.sql deleted file mode 100644 index 6dc96e206..000000000 --- a/source/iofs/testdata/migrations/5_foobar.down.sql +++ /dev/null @@ -1 +0,0 @@ -5 down diff --git a/source/iofs/testdata/migrations/7_foobar.down.sql b/source/iofs/testdata/migrations/7_foobar.down.sql deleted file mode 100644 index 46636016b..000000000 --- a/source/iofs/testdata/migrations/7_foobar.down.sql +++ /dev/null @@ -1 +0,0 @@ -7 down diff --git a/source/iofs/testdata/migrations/7_foobar.up.sql b/source/iofs/testdata/migrations/7_foobar.up.sql deleted file mode 100644 index cdbc410ee..000000000 --- a/source/iofs/testdata/migrations/7_foobar.up.sql +++ /dev/null @@ -1 +0,0 @@ -7 up