Replace go-migrate with morph (#19116)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
c8198fbefe
Коммит
6595b31f23
105
vendor/github.com/go-morph/morph/sources/file/file.go
сгенерированный
поставляемый
Обычный файл
105
vendor/github.com/go-morph/morph/sources/file/file.go
сгенерированный
поставляемый
Обычный файл
@@ -0,0 +1,105 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/go-morph/morph/models"
|
||||
"github.com/go-morph/morph/sources"
|
||||
)
|
||||
|
||||
func init() {
|
||||
sources.Register("file", &File{})
|
||||
}
|
||||
|
||||
type File struct {
|
||||
url string
|
||||
path string
|
||||
migrations []*models.Migration
|
||||
}
|
||||
|
||||
func (f *File) Open(sourceURL string) (sources.Source, error) {
|
||||
uri, err := url.Parse(sourceURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// host might be "." for relative URLs like file://./migrations
|
||||
p := uri.Opaque
|
||||
if len(p) == 0 {
|
||||
p = uri.Host + uri.Path
|
||||
}
|
||||
|
||||
// if no path provided, default to current directory
|
||||
if len(p) == 0 {
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p = wd
|
||||
} else if p[0:1] != "/" {
|
||||
// make path absolute if required
|
||||
abs, err := filepath.Abs(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p = abs
|
||||
}
|
||||
|
||||
nf := &File{
|
||||
url: sourceURL,
|
||||
path: p,
|
||||
}
|
||||
|
||||
if err := nf.readMigrations(); err != nil {
|
||||
return nil, fmt.Errorf("cannot read migrations in path %q: %w", p, err)
|
||||
}
|
||||
|
||||
return nf, nil
|
||||
}
|
||||
|
||||
func (f *File) readMigrations() error {
|
||||
info, err := os.Stat(f.path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() {
|
||||
return fmt.Errorf("file %q is not a directory", info.Name())
|
||||
}
|
||||
|
||||
migrations := []*models.Migration{}
|
||||
walkerr := filepath.Walk(f.path, func(path string, info os.FileInfo, err error) error {
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m, err := models.NewMigration(file, filepath.Base(path))
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not create migration: %w", err)
|
||||
}
|
||||
|
||||
migrations = append(migrations, m)
|
||||
return nil
|
||||
})
|
||||
if walkerr != nil {
|
||||
return walkerr
|
||||
}
|
||||
|
||||
f.migrations = migrations
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *File) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *File) Migrations() []*models.Migration {
|
||||
return f.migrations
|
||||
}
|
||||
31
vendor/github.com/go-morph/morph/sources/go_bindata/README.md
сгенерированный
поставляемый
Обычный файл
31
vendor/github.com/go-morph/morph/sources/go_bindata/README.md
сгенерированный
поставляемый
Обычный файл
@@ -0,0 +1,31 @@
|
||||
# go-bindata source
|
||||
|
||||
This source reads migrations from a
|
||||
[go-bindata](github.com/go-bindata/go-bindata) embedded binary file.
|
||||
|
||||
## Usage
|
||||
|
||||
To read the embedded data, create a migration source through the
|
||||
`WithInstance` method and then instantiate `morph`:
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/go-morph/morph"
|
||||
"github.com/go-morph/morph/sources/go_bindata"
|
||||
"github.com/go-morph/morph/sources/go_bindata/testdata"
|
||||
)
|
||||
|
||||
func main() {
|
||||
res := bindata.Resource(testdata.AssetNames(), func(name string) ([]byte, error) {
|
||||
return testdata.Asset(name)
|
||||
})
|
||||
|
||||
src, err := bindata.WithInstance(res)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// create the morph instance from the source and driver
|
||||
m := morph.NewFromConnURL("postgres://...", src, opts)
|
||||
}
|
||||
```
|
||||
68
vendor/github.com/go-morph/morph/sources/go_bindata/go-bindata.go
сгенерированный
поставляемый
Обычный файл
68
vendor/github.com/go-morph/morph/sources/go_bindata/go-bindata.go
сгенерированный
поставляемый
Обычный файл
@@ -0,0 +1,68 @@
|
||||
package bindata
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/go-morph/morph/models"
|
||||
"github.com/go-morph/morph/sources"
|
||||
)
|
||||
|
||||
type AssetFunc func(name string) ([]byte, error)
|
||||
|
||||
func Resource(names []string, fn AssetFunc) *AssetSource {
|
||||
return &AssetSource{
|
||||
Names: names,
|
||||
AssetFunc: fn,
|
||||
}
|
||||
}
|
||||
|
||||
type AssetSource struct {
|
||||
Names []string
|
||||
AssetFunc AssetFunc
|
||||
}
|
||||
|
||||
func init() {
|
||||
sources.Register("go-bindata", &Bindata{})
|
||||
}
|
||||
|
||||
type Bindata struct {
|
||||
assetSource *AssetSource
|
||||
migrations []*models.Migration
|
||||
}
|
||||
|
||||
func (b *Bindata) Open(url string) (sources.Source, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func WithInstance(assetSource *AssetSource) (sources.Source, error) {
|
||||
b := &Bindata{
|
||||
assetSource: assetSource,
|
||||
migrations: []*models.Migration{},
|
||||
}
|
||||
|
||||
for _, filename := range assetSource.Names {
|
||||
migrationBytes, err := b.assetSource.AssetFunc(filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot read migration %q: %w", filename, err)
|
||||
}
|
||||
|
||||
m, err := models.NewMigration(ioutil.NopCloser(bytes.NewReader(migrationBytes)), filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not create migration: %w", err)
|
||||
}
|
||||
|
||||
b.migrations = append(b.migrations, m)
|
||||
}
|
||||
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (b *Bindata) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bindata) Migrations() []*models.Migration {
|
||||
return b.migrations
|
||||
}
|
||||
47
vendor/github.com/go-morph/morph/sources/source.go
сгенерированный
поставляемый
Обычный файл
47
vendor/github.com/go-morph/morph/sources/source.go
сгенерированный
поставляемый
Обычный файл
@@ -0,0 +1,47 @@
|
||||
package sources
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/go-morph/morph/models"
|
||||
)
|
||||
|
||||
var sourcesMu sync.RWMutex
|
||||
var registeredSources = make(map[string]Source)
|
||||
|
||||
type Source interface {
|
||||
Open(sourceURL string) (source Source, err error)
|
||||
Close() (err error)
|
||||
Migrations() (migrations []*models.Migration)
|
||||
}
|
||||
|
||||
func Register(name string, source Source) {
|
||||
sourcesMu.Lock()
|
||||
defer sourcesMu.Unlock()
|
||||
|
||||
registeredSources[name] = source
|
||||
}
|
||||
|
||||
func List() []string {
|
||||
sourcesMu.Lock()
|
||||
defer sourcesMu.Unlock()
|
||||
|
||||
sources := make([]string, 0, len(registeredSources))
|
||||
for source := range registeredSources {
|
||||
sources = append(sources, source)
|
||||
}
|
||||
|
||||
return sources
|
||||
}
|
||||
|
||||
func Open(sourceName, sourceURL string) (Source, error) {
|
||||
sourcesMu.RLock()
|
||||
source, ok := registeredSources[sourceName]
|
||||
sourcesMu.RUnlock()
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unsupported source %q found", sourceName)
|
||||
}
|
||||
|
||||
return source.Open(sourceURL)
|
||||
}
|
||||
Ссылка в новой задаче
Block a user