-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add io/fs#FS Driver #471 #472
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
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
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,3 @@ | ||
# 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 | ||
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() | ||
// ... | ||
} | ||
``` | ||
https://pkg.go.dev/github.com/golang-migrate/migrate/v4/source/iofs |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// +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.WithInstance(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() | ||
// ... | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,10 @@ import ( | |
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") | ||
//go:embed testdata/migrations/*.sql | ||
var fs embed.FS | ||
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. I'm just curious--how did this even work? Per the golang docs:
To me this looks like a local variable, not a global variable at package scope... What am I missing? 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. Local variables were still supported when implementing this PR. 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. Ah, thank you! |
||
d, err := iofs.WithInstance(fs, "testdata/migrations") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
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.
Thanks for using executable examples!