-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 2 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 |
---|---|---|
@@ -1,15 +1,18 @@ | ||
package source | ||
|
||
import "os" | ||
|
||
// ErrDuplicateMigration is an error type for reporting duplicate migration | ||
// files. | ||
type ErrDuplicateMigration struct { | ||
Migration | ||
os.FileInfo | ||
FileInfo | ||
} | ||
|
||
// Error implements error interface. | ||
func (e ErrDuplicateMigration) Error() string { | ||
return "duplicate migration file: " + e.Name() | ||
} | ||
|
||
// FileInfo is the interface that extracts the minimum required function from os.FileInfo by ErrDuplicateMigration. | ||
type FileInfo interface { | ||
Name() string | ||
} |
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. | ||||||
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. 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 :) |
||||||
|
||||||
## 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,160 @@ | ||
// +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 { | ||
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. Rename to |
||
migrations *source.Migrations | ||
fsys fs.ReadDirFS | ||
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.ReadDirFS, 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.ReadDirFS instance and a relative path. | ||
func (p *Iofs) Init(fsys fs.ReadDirFS, 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. Why use Does this optimization reduce compatibility with different filesystems? We're probably better off using fs.ReadDir |
||
entries, err := fsys.ReadDir(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 | ||
} | ||
if !ms.Append(m) { | ||
return source.ErrDuplicateMigration{ | ||
Migration: *m, | ||
FileInfo: e, | ||
} | ||
} | ||
} | ||
|
||
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.
Why not use
os.FileInfo
or fs.FileInfo?You can use fs.DirEntry.Info() to get the
fs.FileInfo