Skip to content
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

Merged
merged 15 commits into from
Nov 18, 2020
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
38 changes: 38 additions & 0 deletions source/iofs/README.md
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.
Copy link
Member

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


## 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
Copy link

Choose a reason for hiding this comment

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

Suggested change
//go:embed migrations
//go:embed migrations/*.sql

May be useful to describe this to avoid people from accidentally embedding their own README.md, .gitignore, .DS_Store, etc.

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()
// ...
}
```
36 changes: 36 additions & 0 deletions source/iofs/iofs.go
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 {
Copy link
Member

Choose a reason for hiding this comment

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

Rename to IoFS since abbreviations should be in all caps.

httpfs.PartialDriver
Copy link
Member

Choose a reason for hiding this comment

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

A refactor may be in-order.
It makes sense to go the other direction. e.g. add an iofs.PartialDriver and have httpfs embed that, since io/fs.File interface is a subset of the net/http.File interface

}

// 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 {
Copy link
Member

Choose a reason for hiding this comment

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

Converting from fs.FS to http.FileSystem may fail later if the underlying struct doesn't implement io.Seeker. Seek() isn't used in the httpfs source driver right now, but if that changes, this would break.

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
}
31 changes: 31 additions & 0 deletions source/iofs/iofs_test.go
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")
}
}
1 change: 1 addition & 0 deletions source/iofs/testdata/1_foobar.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 down
1 change: 1 addition & 0 deletions source/iofs/testdata/1_foobar.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 up
1 change: 1 addition & 0 deletions source/iofs/testdata/3_foobar.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3 up
1 change: 1 addition & 0 deletions source/iofs/testdata/4_foobar.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4 down
1 change: 1 addition & 0 deletions source/iofs/testdata/4_foobar.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4 up
1 change: 1 addition & 0 deletions source/iofs/testdata/5_foobar.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5 down
1 change: 1 addition & 0 deletions source/iofs/testdata/7_foobar.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7 down
1 change: 1 addition & 0 deletions source/iofs/testdata/7_foobar.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7 up