2019-02-02 22:15:09 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// 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 (
|
2019-06-27 16:31:18 +00:00
|
|
|
"database/sql"
|
2019-02-02 22:15:09 +00:00
|
|
|
"fmt"
|
2019-05-17 04:15:54 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2019-06-27 16:31:18 +00:00
|
|
|
"github.com/lib/pq"
|
|
|
|
"github.com/pressly/goose"
|
2019-05-17 04:15:54 +00:00
|
|
|
|
2019-02-11 10:08:24 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/config"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/plugin/helpers"
|
2019-02-02 22:15:09 +00:00
|
|
|
)
|
|
|
|
|
2019-02-11 10:08:24 +00:00
|
|
|
// 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
|
2019-06-27 16:31:18 +00:00
|
|
|
db *sql.DB
|
2019-02-02 22:15:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-11 10:08:24 +00:00
|
|
|
// Manager requires both filled in generator and database configs
|
2019-10-18 17:34:22 +00:00
|
|
|
func NewMigrationManager(gc config.Plugin, dbc config.Database) MigrationManager {
|
2019-02-02 22:15:09 +00:00
|
|
|
return &manager{
|
|
|
|
GenConfig: gc,
|
|
|
|
DBConfig: dbc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 16:31:18 +00:00
|
|
|
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 {
|
2019-10-18 15:20:53 +00:00
|
|
|
return fmt.Errorf("can't connect to db: %s", err.Error())
|
2019-06-27 16:31:18 +00:00
|
|
|
}
|
|
|
|
m.db = sql.OpenDB(dbConnector)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-02 22:15:09 +00:00
|
|
|
func (m *manager) RunMigrations() error {
|
2019-02-11 10:08:24 +00:00
|
|
|
// 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
|
|
|
|
}
|
2019-02-11 10:08:24 +00:00
|
|
|
// 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()
|
2019-02-11 10:08:24 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-02-11 10:08:24 +00:00
|
|
|
// 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
|
2019-06-27 15:12:12 +00:00
|
|
|
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
|
|
|
|
}
|
2019-06-27 14:52:29 +00:00
|
|
|
removeErr := os.RemoveAll(m.tmpMigDir)
|
|
|
|
if removeErr != nil {
|
|
|
|
removeErrString := "unable to remove file found at %s where tmp directory needs to be written: %s"
|
2019-10-18 15:20:53 +00:00
|
|
|
return fmt.Errorf(removeErrString, m.tmpMigDir, removeErr.Error())
|
2019-02-02 22:15:09 +00:00
|
|
|
}
|
2019-06-27 14:52:29 +00:00
|
|
|
mkdirErr := os.Mkdir(m.tmpMigDir, os.FileMode(os.ModePerm))
|
|
|
|
if mkdirErr != nil {
|
|
|
|
mkdirErrString := "unable to create temporary migration directory %s: %s"
|
2019-10-18 15:20:53 +00:00
|
|
|
return fmt.Errorf(mkdirErrString, m.tmpMigDir, mkdirErr.Error())
|
2019-02-02 22:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-11 10:08:24 +00:00
|
|
|
// Create copies of db migrations from vendored libs
|
2019-03-07 17:02:52 +00:00
|
|
|
func (m *manager) createMigrationCopies(paths []string) error {
|
2019-02-11 10:08:24 +00:00
|
|
|
// Iterate through migration paths to find migration directory
|
2019-03-07 17:02:52 +00:00
|
|
|
for _, path := range paths {
|
2019-02-02 22:15:09 +00:00
|
|
|
dir, err := ioutil.ReadDir(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-11 10:08:24 +00:00
|
|
|
// For each file in the directory check if it is a migration
|
2019-02-02 22:15:09 +00:00
|
|
|
for _, file := range dir {
|
2019-02-11 10:08:24 +00:00
|
|
|
if file.IsDir() || filepath.Ext(file.Name()) != ".sql" {
|
2019-02-02 22:15:09 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
src := filepath.Join(path, file.Name())
|
2019-02-11 10:08:24 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2019-03-07 21:02:13 +00:00
|
|
|
err = m.fixAndRun(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *manager) fixAndRun(path string) error {
|
2019-06-27 16:31:18 +00:00
|
|
|
// Setup DB if not set
|
|
|
|
if m.db == nil {
|
|
|
|
setErr := m.setDB()
|
|
|
|
if setErr != nil {
|
2019-10-18 15:20:53 +00:00
|
|
|
return fmt.Errorf("could not open db: %s", setErr.Error())
|
2019-06-27 16:31:18 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-07 21:02:13 +00:00
|
|
|
// Fix the migrations
|
2019-06-27 16:31:18 +00:00
|
|
|
fixErr := goose.Fix(m.tmpMigDir)
|
|
|
|
if fixErr != nil {
|
2019-10-18 15:20:53 +00:00
|
|
|
return fmt.Errorf("version fixing for plugin migrations at %s failed: %s", path, fixErr.Error())
|
2019-03-07 21:02:13 +00:00
|
|
|
}
|
|
|
|
// Run the copied migrations with goose
|
2019-06-27 16:31:18 +00:00
|
|
|
upErr := goose.Up(m.db, m.tmpMigDir)
|
|
|
|
if upErr != nil {
|
2019-10-18 15:20:53 +00:00
|
|
|
return fmt.Errorf("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)
|
|
|
|
}
|