plugin migration order specified in config

This commit is contained in:
Ian Norden 2019-03-07 11:02:52 -06:00
parent 2ac682afa8
commit 84aa0a7eba
3 changed files with 46 additions and 20 deletions

View File

@ -18,6 +18,7 @@ package cmd
import ( import (
"errors" "errors"
"fmt" "fmt"
"strconv"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -132,32 +133,41 @@ func prepConfig() {
transformers := make(map[string]config.Transformer) transformers := make(map[string]config.Transformer)
for _, name := range names { for _, name := range names {
transformer := viper.GetStringMapString("exporter." + name) transformer := viper.GetStringMapString("exporter." + name)
p, ok := transformer["path"] p, pOK := transformer["path"]
if !ok || p == "" { if !pOK || p == "" {
log.Fatal(fmt.Sprintf("%s transformer config is missing `path` value", name)) log.Fatal(name, "transformer config is missing `path` value")
} }
r, ok := transformer["repository"] r, rOK := transformer["repository"]
if !ok || r == "" { if !rOK || r == "" {
log.Fatal(fmt.Sprintf("%s transformer config is missing `repository` value", name)) log.Fatal(name, "transformer config is missing `repository` value")
} }
m, ok := transformer["migrations"] m, mOK := transformer["migrations"]
if !ok || m == "" { if !mOK || m == "" {
log.Fatal(fmt.Sprintf("%s transformer config is missing `migrations` value", name)) log.Fatal(name, "transformer config is missing `migrations` value")
} }
t, ok := transformer["type"] mr, mrOK := transformer["rank"]
if !ok { if !mrOK || mr == "" {
log.Fatal(fmt.Sprintf("%s transformer config is missing `type` value", name)) 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) transformerType := config.GetTransformerType(t)
if transformerType == config.UnknownTransformerType { if transformerType == config.UnknownTransformerType {
log.Fatal(errors.New(`unknown transformer type in exporter config accepted types are "eth_event", "eth_storage"`)) 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{ transformers[name] = config.Transformer{
Path: p, Path: p,
Type: transformerType, Type: transformerType,
RepositoryPath: r, RepositoryPath: r,
MigrationPath: m, MigrationPath: m,
MigrationRank: rank,
} }
} }

View File

@ -17,6 +17,8 @@
package config package config
import ( import (
"errors"
"fmt"
"path/filepath" "path/filepath"
"strings" "strings"
@ -35,6 +37,7 @@ type Transformer struct {
Path string Path string
Type TransformerType Type TransformerType
MigrationPath string MigrationPath string
MigrationRank int
RepositoryPath string RepositoryPath string
} }
@ -51,10 +54,10 @@ func (c *Plugin) GetPluginPaths() (string, string, error) {
return goFile, soFile, nil return goFile, soFile, nil
} }
// Removes duplicate migration paths before returning them // Removes duplicate migration paths and returns them in ranked order
func (c *Plugin) GetMigrationsPaths() (map[string]bool, error) { func (c *Plugin) GetMigrationsPaths() ([]string, error) {
paths := make(map[string]bool) paths := make(map[int]string)
for _, transformer := range c.Transformers { for name, transformer := range c.Transformers {
repo := transformer.RepositoryPath repo := transformer.RepositoryPath
mig := transformer.MigrationPath mig := transformer.MigrationPath
path := filepath.Join("$GOPATH/src", c.Home, "vendor", repo, mig) 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 { if err != nil {
return nil, err 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 // Removes duplicate repo paths before returning them

View File

@ -105,9 +105,9 @@ func (m *manager) setupMigrationEnv() error {
} }
// Create copies of db migrations from vendored libs // 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 // Iterate through migration paths to find migration directory
for path := range paths { for _, path := range paths {
dir, err := ioutil.ReadDir(path) dir, err := ioutil.ReadDir(path)
if err != nil { if err != nil {
return err return err