Этот коммит содержится в:
Ibrahim Serdar Acikgoz
2022-04-04 14:03:39 +03:00
коммит произвёл GitHub
родитель b1b9774efa
Коммит 97d3368a98
16 изменённых файлов: 173 добавлений и 188 удалений

73
vendor/github.com/mattermost/morph/sources/embedded/README.md сгенерированный поставляемый Обычный файл
Просмотреть файл

@@ -0,0 +1,73 @@
# embedded source
This source reads migrations from embedded files, for example using
[go-bindata](github.com/go-bindata/go-bindata) or go embed feature.
## go embed usage
To read the embedded data, create a migration source through the
`WithInstance` method and then instantiate `morph`:
```go
import (
"embed"
"path/filepath"
"github.com/mattermost/morph"
"github.com/mattermost/morph/sources/embedded"
)
//go:embed testfiles
var assets embed.FS
func main() {
dirEntries, err := assets.ReadDir("testfiles")
if err != nil {
panic(err)
}
assetNames := make([]string, len(dirEntries))
for i, dirEntry := range dirEntries {
assetNames[i] = dirEntry.Name()
}
res := embedded.Resource(assetNames, func(name string) ([]byte, error) {
return assets.ReadFile(filepath.Join("testfiles", name))
})
src, err := embedded.WithInstance(res)
if err != nil {
panic(err)
}
// create the morph instance from the source and driver
m := morph.NewFromConnURL("postgres://...", src, opts)
}
```
## go-bindata usage
To read the embedded data, create a migration source through the
`WithInstance` method and then instantiate `morph`:
```go
import (
"github.com/mattermost/morph"
"github.com/mattermost/morph/sources/embedded"
"github.com/mattermost/morph/sources/embedded/testdata"
)
func main() {
res := embedded.Resource(testdata.AssetNames(), func(name string) ([]byte, error) {
return testdata.Asset(name)
})
src, err := embedded.WithInstance(res)
if err != nil {
panic(err)
}
// create the morph instance from the source and driver
m := morph.NewFromConnURL("postgres://...", src, opts)
}
```

56
vendor/github.com/mattermost/morph/sources/embedded/embedded.go сгенерированный поставляемый Обычный файл
Просмотреть файл

@@ -0,0 +1,56 @@
package embedded
import (
"bytes"
"fmt"
"io/ioutil"
"github.com/mattermost/morph/models"
"github.com/mattermost/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
}
type Embedded struct {
assetSource *AssetSource
migrations []*models.Migration
}
func WithInstance(assetSource *AssetSource) (sources.Source, error) {
b := &Embedded{
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 *Embedded) Migrations() []*models.Migration {
return b.migrations
}