Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix documents and migration directory for testing
  • Loading branch information
johejo committed Nov 15, 2020
commit e9ae08da0c5abc15d567294012205073e92e35d5
37 changes: 1 addition & 36 deletions source/iofs/README.md
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
10 changes: 10 additions & 0 deletions source/iofs/doc.go
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
28 changes: 28 additions & 0 deletions source/iofs/example_test.go
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() {
Copy link
Member

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!

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()
// ...
}
7 changes: 3 additions & 4 deletions source/iofs/iofs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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:

The //go:embed directive can be used with both exported and unexported variables, depending on whether the package wants to make the data available to other packages. It can only be used with global variables at package scope, not with local variables.

To me this looks like a local variable, not a global variable at package scope... What am I missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Local variables were still supported when implementing this PR.
golang/go#43216
It is a remnant of that.

Copy link
Contributor

Choose a reason for hiding this comment

The 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)
}
Expand Down