-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add io/fs#FS Driver #471 #472
Changes from 3 commits
489e6d3
65cbb0f
e18e378
e9ae08d
416f878
45ec3dc
3af8acf
0e9937b
7fb518d
3c0290b
2cd12a6
ca6628d
58e9837
321662e
02da3b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// +build go1.16 | ||
// +build iofs | ||
|
||
package cli | ||
|
||
import ( | ||
_ "github.com/golang-migrate/migrate/v4/source/iofs" | ||
) |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,38 @@ | ||||||
# iofs | ||||||
|
||||||
Driver with file system interface (`io/fs#FS`) supported from Go 1.16. | ||||||
|
||||||
This Driver cannot be used with Go versions 1.15 and below. | ||||||
|
||||||
## Usage | ||||||
|
||||||
Directory embedding example | ||||||
|
||||||
```go | ||||||
package main | ||||||
|
||||||
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 migrations | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
May be useful to describe this to avoid people from accidentally embedding their own |
||||||
var fs embed.FS | ||||||
|
||||||
func main() { | ||||||
d, err := iofs.WithInstance(fs, "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() | ||||||
// ... | ||||||
} | ||||||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
// +build go1.16 | ||
|
||
package iofs | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"io/fs" | ||
"path" | ||
"strconv" | ||
|
||
"github.com/golang-migrate/migrate/v4/source" | ||
) | ||
|
||
func init() { | ||
source.Register("iofs", &IoFS{}) | ||
} | ||
|
||
// IoFS is a source driver for io/fs#FS. | ||
type IoFS struct { | ||
migrations *source.Migrations | ||
fsys fs.FS | ||
path string | ||
} | ||
|
||
// Open by url does not supported with IoFS. | ||
func (i *IoFS) Open(url string) (source.Driver, error) { | ||
return nil, errors.New("iofs driver does not support open by url") | ||
} | ||
|
||
// WithInstance wraps io/fs#FS as source.Driver. | ||
func WithInstance(fsys fs.FS, path string) (source.Driver, error) { | ||
var i IoFS | ||
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 | ||
} | ||
|
||
// Init prepares not initialized IoFS instance to read migrations from a | ||
// fs.FS instance and a relative path. | ||
func (p *IoFS) Init(fsys fs.FS, path string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are your thoughts about moving most of this implementation to a |
||
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 { | ||
continue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is continue OK like any other error case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to https://tip.golang.org/pkg/io/fs/#DirEntry an error in this case may mean:
But I think it can also just represent any other IO error, in e.g. the case the FS interface is backed by a real FS and not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed that errors should be returned and not ignored. Ignored errors will make bugs harder to debug. |
||
} | ||
if !ms.Append(m) { | ||
return source.ErrDuplicateMigration{ | ||
Migration: *m, | ||
FileInfo: file, | ||
} | ||
} | ||
} | ||
|
||
p.fsys = fsys | ||
p.path = path | ||
p.migrations = ms | ||
return nil | ||
} | ||
|
||
// Close is part of source.Driver interface implementation. This is a no-op. | ||
func (p *IoFS) Close() error { | ||
return nil | ||
} | ||
|
||
// First is part of source.Driver interface implementation. | ||
func (p *IoFS) First() (version uint, err error) { | ||
if version, ok := p.migrations.First(); ok { | ||
return version, nil | ||
} | ||
return 0, &fs.PathError{ | ||
Op: "first", | ||
Path: p.path, | ||
Err: fs.ErrNotExist, | ||
} | ||
} | ||
|
||
// Prev is part of source.Driver interface implementation. | ||
func (p *IoFS) Prev(version uint) (prevVersion uint, err error) { | ||
if version, ok := p.migrations.Prev(version); ok { | ||
return version, nil | ||
} | ||
return 0, &fs.PathError{ | ||
Op: "prev for version " + strconv.FormatUint(uint64(version), 10), | ||
Path: p.path, | ||
Err: fs.ErrNotExist, | ||
} | ||
} | ||
|
||
// Next is part of source.Driver interface implementation. | ||
func (p *IoFS) Next(version uint) (nextVersion uint, err error) { | ||
if version, ok := p.migrations.Next(version); ok { | ||
return version, nil | ||
} | ||
return 0, &fs.PathError{ | ||
Op: "next for version " + strconv.FormatUint(uint64(version), 10), | ||
Path: p.path, | ||
Err: fs.ErrNotExist, | ||
} | ||
} | ||
|
||
// ReadUp is part of source.Driver interface implementation. | ||
func (p *IoFS) ReadUp(version uint) (r io.ReadCloser, identifier string, err error) { | ||
if m, ok := p.migrations.Up(version); ok { | ||
body, err := p.open(path.Join(p.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: p.path, | ||
Err: fs.ErrNotExist, | ||
} | ||
} | ||
|
||
// ReadDown is part of source.Driver interface implementation. | ||
func (p *IoFS) ReadDown(version uint) (r io.ReadCloser, identifier string, err error) { | ||
if m, ok := p.migrations.Down(version); ok { | ||
body, err := p.open(path.Join(p.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: p.path, | ||
Err: fs.ErrNotExist, | ||
} | ||
} | ||
|
||
func (p *IoFS) open(path string) (fs.File, error) { | ||
f, err := p.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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// +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" | ||
) | ||
|
||
//go:embed testdata | ||
var fs embed.FS | ||
|
||
func Test(t *testing.T) { | ||
d, err := iofs.WithInstance(fs, "testdata") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
st.Test(t, d) | ||
} | ||
|
||
func TestOpen(t *testing.T) { | ||
i := new(iofs.IoFS) | ||
_, err := i.Open("") | ||
if err == nil { | ||
t.Fatal("iofs driver does not support open by url") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 down |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 up |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3 up |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
4 down |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
4 up |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
5 down |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
7 down |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
7 up |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Reword this to something like just:
Side note: I think all the documentation in this file would be better placed into a package-level godoc documentation comment, so that on https://pkg.go.dev this package shows up with package docs and usage examples similar to this and this
Feel free to ignore if this is not helpful :)