-
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 1 commit
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,38 @@ | ||||||
# iofs | ||||||
|
||||||
Driver with file system interface (`io/fs#FS`) supported from Go 1.16. | ||||||
|
||||||
This Driver cannot be used with versions below Go 1.15. | ||||||
|
||||||
## 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,36 @@ | ||
// +build go1.16 | ||
|
||
package iofs | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/fs" | ||
"net/http" | ||
|
||
"github.com/golang-migrate/migrate/v4/source" | ||
"github.com/golang-migrate/migrate/v4/source/httpfs" | ||
) | ||
|
||
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 |
||
httpfs.PartialDriver | ||
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. |
||
} | ||
|
||
// 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(http.FS(fsys), path); err != nil { | ||
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. Converting from See: https://tip.golang.org/src/net/http/fs.go func (f ioFile) Seek(offset int64, whence int) (int64, error) {
s, ok := f.file.(io.Seeker)
if !ok {
return 0, errMissingSeek
}
return s.Seek(offset, whence)
} |
||
return nil, fmt.Errorf("failed to init driver with path %s: %w", path, err) | ||
} | ||
return &i, nil | ||
} |
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.
Update to:
This Driver cannot be used with Go versions 1.15 and below