Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
Merge pull request #113 from vulcanize/plugin-updates
Browse files Browse the repository at this point in the history
Plugin updates
  • Loading branch information
rmulhol authored Jun 27, 2019
2 parents 7092d33 + 0f95267 commit af6190e
Show file tree
Hide file tree
Showing 54 changed files with 4,669 additions and 167 deletions.
168 changes: 29 additions & 139 deletions Gopkg.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ func initConfig() {
log.Printf("Using config file: %s\n\n", viper.ConfigFileUsed())
} else {
invalidConfigError := "Couldn't read config file"
fmt.Println("Error: ", invalidConfigError)
log.Fatal(invalidConfigError)
formattedError := fmt.Sprintf("%s: %s", invalidConfigError, err.Error())
log.Fatal(formattedError)
}
}

Expand Down
68 changes: 42 additions & 26 deletions pkg/plugin/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
package manager

import (
"database/sql"
"errors"
"fmt"
"github.com/lib/pq"
"github.com/pressly/goose"
"github.com/vulcanize/vulcanizedb/pkg/config"
"github.com/vulcanize/vulcanizedb/pkg/plugin/helpers"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
)

Expand All @@ -36,6 +38,7 @@ type manager struct {
GenConfig config.Plugin
DBConfig config.Database
tmpMigDir string
db *sql.DB
}

// Manager requires both filled in generator and database configs
Expand All @@ -46,6 +49,22 @@ func NewMigrationManager(gc config.Plugin, dbc config.Database) *manager {
}
}

func (m *manager) setDB() error {
var pgStr string
if len(m.DBConfig.User) > 0 && len(m.DBConfig.Password) > 0 {
pgStr = fmt.Sprintf("postgresql://%s:%s@%s:%d/%s?sslmode=disable",
m.DBConfig.User, m.DBConfig.Password, m.DBConfig.Hostname, m.DBConfig.Port, m.DBConfig.Name)
} else {
pgStr = fmt.Sprintf("postgres://%s:%d/%s?sslmode=disable", m.DBConfig.Hostname, m.DBConfig.Port, m.DBConfig.Name)
}
dbConnector, err := pq.NewConnector(pgStr)
if err != nil {
return errors.New(fmt.Sprintf("can't connect to db: %s", err.Error()))
}
m.db = sql.OpenDB(dbConnector)
return nil
}

func (m *manager) RunMigrations() error {
// Get paths to db migrations from the plugin config
paths, err := m.GenConfig.GetMigrationsPaths()
Expand Down Expand Up @@ -73,17 +92,19 @@ func (m *manager) RunMigrations() error {
// Setup a temporary directory to hold transformer db migrations
func (m *manager) setupMigrationEnv() error {
var err error
m.tmpMigDir, err = helpers.CleanPath(filepath.Join("$GOPATH/src", m.GenConfig.Home+".plugin_migrations"))
m.tmpMigDir, err = helpers.CleanPath(filepath.Join("$GOPATH/src", m.GenConfig.Home, ".plugin_migrations"))
if err != nil {
return err
}
err = os.RemoveAll(m.tmpMigDir)
if err != nil {
return errors.New(fmt.Sprintf("unable to remove file found at %s where tmp directory needs to be written", m.tmpMigDir))
removeErr := os.RemoveAll(m.tmpMigDir)
if removeErr != nil {
removeErrString := "unable to remove file found at %s where tmp directory needs to be written: %s"
return errors.New(fmt.Sprintf(removeErrString, m.tmpMigDir, removeErr.Error()))
}
err = os.Mkdir(m.tmpMigDir, os.FileMode(os.ModePerm))
if err != nil {
return errors.New(fmt.Sprintf("unable to create temporary migration directory %s", m.tmpMigDir))
mkdirErr := os.Mkdir(m.tmpMigDir, os.FileMode(os.ModePerm))
if mkdirErr != nil {
mkdirErrString := "unable to create temporary migration directory %s: %s"
return errors.New(fmt.Sprintf(mkdirErrString, m.tmpMigDir, mkdirErr.Error()))
}

return nil
Expand Down Expand Up @@ -120,28 +141,23 @@ func (m *manager) createMigrationCopies(paths []string) error {
}

func (m *manager) fixAndRun(path string) error {
// Setup DB if not set
if m.db == nil {
setErr := m.setDB()
if setErr != nil {
return errors.New(fmt.Sprintf("could not open db: %s", setErr.Error()))
}
}
// Fix the migrations
cmd := exec.Command("goose", "fix")
cmd.Dir = m.tmpMigDir
err := cmd.Run()
if err != nil {
return errors.New(fmt.Sprintf("version fixing for plugin migrations at %s failed: %s", path, err.Error()))
fixErr := goose.Fix(m.tmpMigDir)
if fixErr != nil {
return errors.New(fmt.Sprintf("version fixing for plugin migrations at %s failed: %s", path, fixErr.Error()))
}
// Run the copied migrations with goose
var pgStr string
if len(m.DBConfig.User) > 0 && len(m.DBConfig.Password) > 0 {
pgStr = fmt.Sprintf("postgresql://%s:%s@%s:%d/%s?sslmode=disable",
m.DBConfig.User, m.DBConfig.Password, m.DBConfig.Hostname, m.DBConfig.Port, m.DBConfig.Name)
} else {
pgStr = fmt.Sprintf("postgres://%s:%d/%s?sslmode=disable", m.DBConfig.Hostname, m.DBConfig.Port, m.DBConfig.Name)
upErr := goose.Up(m.db, m.tmpMigDir)
if upErr != nil {
return errors.New(fmt.Sprintf("db migrations for plugin transformers at %s failed: %s", path, upErr.Error()))
}
cmd = exec.Command("goose", "postgres", pgStr, "up")
cmd.Dir = m.tmpMigDir
err = cmd.Run()
if err != nil {
return errors.New(fmt.Sprintf("db migrations for plugin transformers at %s failed: %s", path, err.Error()))
}

return nil
}

Expand Down
24 changes: 24 additions & 0 deletions vendor/github.com/pkg/errors/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions vendor/github.com/pkg/errors/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions vendor/github.com/pkg/errors/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions vendor/github.com/pkg/errors/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions vendor/github.com/pkg/errors/appveyor.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions vendor/github.com/pkg/errors/bench_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit af6190e

Please sign in to comment.