embedded

package
v0.0.0-...-b96d2fb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2022 License: MIT Imports: 5 Imported by: 0

README

embedded source

This source reads migrations from embedded files, for example using 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:

import (
	"embed"
	"path/filepath"

    "github.com/isacikgoz/morph"
    "github.com/isacikgoz/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:

import (
    "github.com/isacikgoz/morph"
    "github.com/isacikgoz/morph/sources/embedded"
    "github.com/isacikgoz/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)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithInstance

func WithInstance(assetSource *AssetSource) (sources.Source, error)

Types

type AssetFunc

type AssetFunc func(name string) ([]byte, error)

type AssetSource

type AssetSource struct {
	Names     []string
	AssetFunc AssetFunc
}

func Resource

func Resource(names []string, fn AssetFunc) *AssetSource

type Embedded

type Embedded struct {
	// contains filtered or unexported fields
}

func (*Embedded) Migrations

func (b *Embedded) Migrations() []*models.Migration

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL