plugin migration order specified in config
This commit is contained in:
parent
2ac682afa8
commit
84aa0a7eba
@ -18,6 +18,7 @@ package cmd
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
@ -132,32 +133,41 @@ func prepConfig() {
|
||||
transformers := make(map[string]config.Transformer)
|
||||
for _, name := range names {
|
||||
transformer := viper.GetStringMapString("exporter." + name)
|
||||
p, ok := transformer["path"]
|
||||
if !ok || p == "" {
|
||||
log.Fatal(fmt.Sprintf("%s transformer config is missing `path` value", name))
|
||||
p, pOK := transformer["path"]
|
||||
if !pOK || p == "" {
|
||||
log.Fatal(name, "transformer config is missing `path` value")
|
||||
}
|
||||
r, ok := transformer["repository"]
|
||||
if !ok || r == "" {
|
||||
log.Fatal(fmt.Sprintf("%s transformer config is missing `repository` value", name))
|
||||
r, rOK := transformer["repository"]
|
||||
if !rOK || r == "" {
|
||||
log.Fatal(name, "transformer config is missing `repository` value")
|
||||
}
|
||||
m, ok := transformer["migrations"]
|
||||
if !ok || m == "" {
|
||||
log.Fatal(fmt.Sprintf("%s transformer config is missing `migrations` value", name))
|
||||
m, mOK := transformer["migrations"]
|
||||
if !mOK || m == "" {
|
||||
log.Fatal(name, "transformer config is missing `migrations` value")
|
||||
}
|
||||
t, ok := transformer["type"]
|
||||
if !ok {
|
||||
log.Fatal(fmt.Sprintf("%s transformer config is missing `type` value", name))
|
||||
mr, mrOK := transformer["rank"]
|
||||
if !mrOK || mr == "" {
|
||||
log.Fatal(name, "transformer config is missing `rank` value")
|
||||
}
|
||||
t, tOK := transformer["type"]
|
||||
if !tOK {
|
||||
log.Fatal(name, "transformer config is missing `type` value")
|
||||
}
|
||||
transformerType := config.GetTransformerType(t)
|
||||
if transformerType == config.UnknownTransformerType {
|
||||
log.Fatal(errors.New(`unknown transformer type in exporter config accepted types are "eth_event", "eth_storage"`))
|
||||
}
|
||||
rank, err := strconv.Atoi(mr)
|
||||
if err != nil {
|
||||
log.Fatal(name, "migration `rank` can't be converted to an integer")
|
||||
}
|
||||
|
||||
transformers[name] = config.Transformer{
|
||||
Path: p,
|
||||
Type: transformerType,
|
||||
RepositoryPath: r,
|
||||
MigrationPath: m,
|
||||
MigrationRank: rank,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -17,6 +17,8 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
@ -35,6 +37,7 @@ type Transformer struct {
|
||||
Path string
|
||||
Type TransformerType
|
||||
MigrationPath string
|
||||
MigrationRank int
|
||||
RepositoryPath string
|
||||
}
|
||||
|
||||
@ -51,10 +54,10 @@ func (c *Plugin) GetPluginPaths() (string, string, error) {
|
||||
return goFile, soFile, nil
|
||||
}
|
||||
|
||||
// Removes duplicate migration paths before returning them
|
||||
func (c *Plugin) GetMigrationsPaths() (map[string]bool, error) {
|
||||
paths := make(map[string]bool)
|
||||
for _, transformer := range c.Transformers {
|
||||
// Removes duplicate migration paths and returns them in ranked order
|
||||
func (c *Plugin) GetMigrationsPaths() ([]string, error) {
|
||||
paths := make(map[int]string)
|
||||
for name, transformer := range c.Transformers {
|
||||
repo := transformer.RepositoryPath
|
||||
mig := transformer.MigrationPath
|
||||
path := filepath.Join("$GOPATH/src", c.Home, "vendor", repo, mig)
|
||||
@ -62,10 +65,23 @@ func (c *Plugin) GetMigrationsPaths() (map[string]bool, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
paths[cleanPath] = true
|
||||
// If there is a different path with the same rank then we have a conflict
|
||||
_, ok := paths[transformer.MigrationRank]
|
||||
if ok {
|
||||
conflictingPath := paths[transformer.MigrationRank]
|
||||
if conflictingPath != cleanPath {
|
||||
return nil, errors.New(fmt.Sprintf("transformer %s has the same migration rank (%d) as another transformer", name, transformer.MigrationRank))
|
||||
}
|
||||
}
|
||||
paths[transformer.MigrationRank] = cleanPath
|
||||
}
|
||||
|
||||
return paths, nil
|
||||
sortedPaths := make([]string, len(paths))
|
||||
for rank, path := range paths {
|
||||
sortedPaths[rank] = path
|
||||
}
|
||||
|
||||
return sortedPaths, nil
|
||||
}
|
||||
|
||||
// Removes duplicate repo paths before returning them
|
||||
|
@ -105,9 +105,9 @@ func (m *manager) setupMigrationEnv() error {
|
||||
}
|
||||
|
||||
// Create copies of db migrations from vendored libs
|
||||
func (m *manager) createMigrationCopies(paths map[string]bool) error {
|
||||
func (m *manager) createMigrationCopies(paths []string) error {
|
||||
// Iterate through migration paths to find migration directory
|
||||
for path := range paths {
|
||||
for _, path := range paths {
|
||||
dir, err := ioutil.ReadDir(path)
|
||||
if err != nil {
|
||||
return err
|
||||
|
Loading…
Reference in New Issue
Block a user