ipld-eth-server/pkg/plugin/manager/manager.go

136 lines
3.8 KiB
Go
Raw Normal View History

2019-02-02 22:15:09 +00:00
// VulcanizeDB
// Copyright © 2018 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package manager
import (
"errors"
"fmt"
"github.com/vulcanize/vulcanizedb/pkg/config"
"github.com/vulcanize/vulcanizedb/pkg/plugin/helpers"
2019-02-02 22:15:09 +00:00
"io/ioutil"
"os"
"os/exec"
"path/filepath"
)
// Interface for managing the db migrations for plugin transformers
2019-02-02 22:15:09 +00:00
type MigrationManager interface {
RunMigrations() error
}
type manager struct {
GenConfig config.Plugin
DBConfig config.Database
tmpMigDir string
}
// Manager requires both filled in generator and database configs
2019-02-02 22:15:09 +00:00
func NewMigrationManager(gc config.Plugin, dbc config.Database) *manager {
return &manager{
GenConfig: gc,
DBConfig: dbc,
}
}
func (m *manager) RunMigrations() error {
// Get paths to db migrations from the plugin config
2019-02-02 22:15:09 +00:00
paths, err := m.GenConfig.GetMigrationsPaths()
if err != nil {
return err
}
if len(paths) < 1 {
return nil
}
// Init directory for temporary copies of migrations
2019-02-02 22:15:09 +00:00
err = m.setupMigrationEnv()
if err != nil {
return err
}
defer m.cleanUp()
// Creates copies of migrations for all the plugin's transformers in a tmp dir
2019-02-02 22:15:09 +00:00
err = m.createMigrationCopies(paths)
if err != nil {
return err
}
// 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 failed: %s", err.Error()))
}
// Run the copied migrations with goose
2019-02-02 22:15:09 +00:00
pgStr := fmt.Sprintf("postgres://%s:%d/%s?sslmode=disable", m.DBConfig.Hostname, m.DBConfig.Port, m.DBConfig.Name)
cmd = exec.Command("goose", "postgres", pgStr, "up")
cmd.Dir = m.tmpMigDir
err = cmd.Run()
2019-02-02 22:15:09 +00:00
if err != nil {
return errors.New(fmt.Sprintf("db migrations for plugin transformers failed: %s", err.Error()))
}
return nil
}
// Setup a temporary directory to hold transformer db migrations
2019-02-02 22:15:09 +00:00
func (m *manager) setupMigrationEnv() error {
var err error
m.tmpMigDir, err = helpers.CleanPath("$GOPATH/src/github.com/vulcanize/vulcanizedb/db/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))
}
err = os.Mkdir(m.tmpMigDir, os.FileMode(os.ModePerm))
2019-02-02 22:15:09 +00:00
if err != nil {
return errors.New(fmt.Sprintf("unable to create temporary migration directory %s", m.tmpMigDir))
}
return nil
}
// Create copies of db migrations from vendored libs
func (m *manager) createMigrationCopies(paths map[string]bool) error {
// Iterate through migration paths to find migration directory
for path := range paths {
2019-02-02 22:15:09 +00:00
dir, err := ioutil.ReadDir(path)
if err != nil {
return err
}
// For each file in the directory check if it is a migration
2019-02-02 22:15:09 +00:00
for _, file := range dir {
if file.IsDir() || filepath.Ext(file.Name()) != ".sql" {
2019-02-02 22:15:09 +00:00
continue
}
src := filepath.Join(path, file.Name())
dst := filepath.Join(m.tmpMigDir, file.Name())
// and if it is make a copy of it to our tmp migration directory
2019-02-02 22:15:09 +00:00
err = helpers.CopyFile(src, dst)
if err != nil {
return err
}
}
}
return nil
}
func (m *manager) cleanUp() error {
return os.RemoveAll(m.tmpMigDir)
}