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

167 lines
4.7 KiB
Go
Raw Normal View History

2019-02-02 22:15:09 +00:00
// VulcanizeDB
// Copyright © 2019 Vulcanize
2019-02-02 22:15:09 +00:00
// 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 (
"database/sql"
2019-02-02 22:15:09 +00:00
"errors"
"fmt"
"github.com/lib/pq"
"github.com/pressly/goose"
"github.com/vulcanize/vulcanizedb/pkg/config"
"github.com/vulcanize/vulcanizedb/pkg/plugin/helpers"
2019-02-02 22:15:09 +00:00
"io/ioutil"
"os"
"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
db *sql.DB
2019-02-02 22:15:09 +00:00
}
// 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) 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
}
2019-02-02 22:15:09 +00:00
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
}
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(filepath.Join("$GOPATH/src", m.GenConfig.Home, ".plugin_migrations"))
2019-02-02 22:15:09 +00:00
if err != nil {
return err
}
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()))
2019-02-02 22:15:09 +00:00
}
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()))
2019-02-02 22:15:09 +00:00
}
return nil
}
// Create copies of db migrations from vendored libs
func (m *manager) createMigrationCopies(paths []string) 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
}
}
err = m.fixAndRun(path)
if err != nil {
return err
}
}
return nil
}
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
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
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()))
2019-04-10 21:56:31 +00:00
}
2019-02-02 22:15:09 +00:00
return nil
}
func (m *manager) cleanUp() error {
return os.RemoveAll(m.tmpMigDir)
}