snapshot of https://github.com/golang-migrate/migrate.git (master, 2026-09-15; blobs > 131072B omitted)
This commit is contained in:
56
source/godoc_vfs/vfs.go
Normal file
56
source/godoc_vfs/vfs.go
Normal file
@@ -0,0 +1,56 @@
|
||||
// Package godoc_vfs contains a driver that reads migrations from a virtual file
|
||||
// system.
|
||||
//
|
||||
// Implementations of the filesystem interface that read from zip files and
|
||||
// maps, as well as the definition of the filesystem interface can be found in
|
||||
// the golang.org/x/tools/godoc/vfs package.
|
||||
package godoc_vfs
|
||||
|
||||
import (
|
||||
"github.com/golang-migrate/migrate/v4/source"
|
||||
"github.com/golang-migrate/migrate/v4/source/httpfs"
|
||||
|
||||
"golang.org/x/tools/godoc/vfs"
|
||||
vfs_httpfs "golang.org/x/tools/godoc/vfs/httpfs"
|
||||
)
|
||||
|
||||
func init() {
|
||||
source.Register("godoc-vfs", &VFS{})
|
||||
}
|
||||
|
||||
// VFS is an implementation of driver that returns migrations from a virtual
|
||||
// file system.
|
||||
type VFS struct {
|
||||
httpfs.PartialDriver
|
||||
fs vfs.FileSystem
|
||||
path string
|
||||
}
|
||||
|
||||
// Open implements the source.Driver interface for VFS.
|
||||
//
|
||||
// Calling this function panics, instead use the WithInstance function.
|
||||
// See the package level documentation for an example.
|
||||
func (b *VFS) Open(url string) (source.Driver, error) {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
// WithInstance creates a new driver from a virtual file system.
|
||||
// If a tree named searchPath exists in the virtual filesystem, WithInstance
|
||||
// searches for migration files there.
|
||||
// It defaults to "/".
|
||||
func WithInstance(fs vfs.FileSystem, searchPath string) (source.Driver, error) {
|
||||
if searchPath == "" {
|
||||
searchPath = "/"
|
||||
}
|
||||
|
||||
bn := &VFS{
|
||||
fs: fs,
|
||||
path: searchPath,
|
||||
}
|
||||
|
||||
if err := bn.Init(vfs_httpfs.New(fs), searchPath); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return bn, nil
|
||||
}
|
||||
33
source/godoc_vfs/vfs_example_test.go
Normal file
33
source/godoc_vfs/vfs_example_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package godoc_vfs_test
|
||||
|
||||
import (
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
"github.com/golang-migrate/migrate/v4/source/godoc_vfs"
|
||||
"golang.org/x/tools/godoc/vfs/mapfs"
|
||||
)
|
||||
|
||||
func Example_mapfs() {
|
||||
fs := mapfs.New(map[string]string{
|
||||
"1_foobar.up.sql": "1 up",
|
||||
"1_foobar.down.sql": "1 down",
|
||||
"3_foobar.up.sql": "3 up",
|
||||
"4_foobar.up.sql": "4 up",
|
||||
"4_foobar.down.sql": "4 down",
|
||||
"5_foobar.down.sql": "5 down",
|
||||
"7_foobar.up.sql": "7 up",
|
||||
"7_foobar.down.sql": "7 down",
|
||||
})
|
||||
|
||||
d, err := godoc_vfs.WithInstance(fs, "")
|
||||
if err != nil {
|
||||
panic("bad migrations found!")
|
||||
}
|
||||
m, err := migrate.NewWithSourceInstance("godoc-vfs", d, "database://foobar")
|
||||
if err != nil {
|
||||
panic("error creating the migrations")
|
||||
}
|
||||
err = m.Up()
|
||||
if err != nil {
|
||||
panic("up failed")
|
||||
}
|
||||
}
|
||||
40
source/godoc_vfs/vfs_test.go
Normal file
40
source/godoc_vfs/vfs_test.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package godoc_vfs_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4/source/godoc_vfs"
|
||||
st "github.com/golang-migrate/migrate/v4/source/testing"
|
||||
"golang.org/x/tools/godoc/vfs/mapfs"
|
||||
)
|
||||
|
||||
func TestVFS(t *testing.T) {
|
||||
fs := mapfs.New(map[string]string{
|
||||
"1_foobar.up.sql": "1 up",
|
||||
"1_foobar.down.sql": "1 down",
|
||||
"3_foobar.up.sql": "3 up",
|
||||
"4_foobar.up.sql": "4 up",
|
||||
"4_foobar.down.sql": "4 down",
|
||||
"5_foobar.down.sql": "5 down",
|
||||
"7_foobar.up.sql": "7 up",
|
||||
"7_foobar.down.sql": "7 down",
|
||||
})
|
||||
|
||||
d, err := godoc_vfs.WithInstance(fs, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
st.Test(t, d)
|
||||
}
|
||||
|
||||
func TestOpen(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Error("Expected Open to panic")
|
||||
}
|
||||
}()
|
||||
b := &godoc_vfs.VFS{}
|
||||
if _, err := b.Open(""); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user