Semaphore/db/migrations.go

132 lines
2.7 KiB
Go
Raw Normal View History

2016-05-24 11:55:48 +02:00
package db
2016-01-05 00:32:53 +01:00
import (
"fmt"
"time"
2016-03-16 22:49:43 +01:00
2020-02-09 14:48:24 +01:00
log "github.com/Sirupsen/logrus"
2016-03-16 22:49:43 +01:00
"github.com/go-sql-driver/mysql"
"github.com/gobuffalo/packr"
2016-01-05 00:32:53 +01:00
)
var dbAssets = packr.NewBox("./migrations")
// CheckExists queries the database to see if a migration table with this version id exists already
func (version *Version) CheckExists() (bool, error) {
2016-05-24 11:55:48 +02:00
exists, err := Mysql.SelectInt("select count(1) as ex from migrations where version=?", version.VersionString())
2016-01-05 00:32:53 +01:00
if err != nil {
2020-02-09 14:48:24 +01:00
//nolint: gosimple
2016-01-05 00:32:53 +01:00
switch err.(type) {
case *mysql.MySQLError:
// 1146 is mysql table does not exist
if err.(*mysql.MySQLError).Number != 1146 {
return false, err
}
fmt.Println("Creating migrations table")
if _, err = Mysql.Exec(initialSQL); err != nil {
2016-01-05 00:32:53 +01:00
panic(err)
}
return version.CheckExists()
default:
return false, err
}
}
return exists > 0, nil
}
// Run executes a database migration
func (version *Version) Run() error {
2016-01-05 00:32:53 +01:00
fmt.Printf("Executing migration %s (at %v)...\n", version.HumanoidVersion(), time.Now())
2016-05-24 11:55:48 +02:00
tx, err := Mysql.Begin()
2016-01-05 00:32:53 +01:00
if err != nil {
return err
}
sql := version.GetSQL(version.GetPath())
for i, query := range sql {
fmt.Printf("\r [%d/%d]", i+1, len(sql))
if len(query) == 0 {
continue
}
if _, err := tx.Exec(query); err != nil {
handleRollbackError(tx.Rollback())
log.Warnf("\n ERR! Query: %v\n\n", query)
2016-01-05 00:32:53 +01:00
return err
}
}
if _, err := tx.Exec("insert into migrations set version=?, upgraded_date=?", version.VersionString(), time.Now()); err != nil {
handleRollbackError(tx.Rollback())
2016-01-05 00:32:53 +01:00
return err
}
fmt.Println()
return tx.Commit()
}
2020-02-09 14:48:24 +01:00
func handleRollbackError(err error) {
if err != nil {
log.Warn(err.Error())
}
}
// TryRollback attempts to rollback the database to an earlier version if a rollback exists
func (version *Version) TryRollback() {
2016-01-05 00:32:53 +01:00
fmt.Printf("Rolling back %s (time: %v)...\n", version.HumanoidVersion(), time.Now())
data := dbAssets.Bytes(version.GetErrPath())
if len(data) == 0 {
2017-05-20 15:21:13 +02:00
fmt.Println("Rollback SQL does not exist.")
2016-01-05 00:32:53 +01:00
fmt.Println()
return
}
sql := version.GetSQL(version.GetErrPath())
for _, query := range sql {
fmt.Printf(" [ROLLBACK] > %v\n", query)
2016-05-24 11:55:48 +02:00
if _, err := Mysql.Exec(query); err != nil {
2016-01-05 00:32:53 +01:00
fmt.Println(" [ROLLBACK] - Stopping")
return
}
}
}
// MigrateAll checks for db migrations and executes them
2016-01-05 00:32:53 +01:00
func MigrateAll() error {
2017-05-20 15:21:13 +02:00
fmt.Println("Checking DB migrations")
didRun := false
2016-01-05 00:32:53 +01:00
// go from beginning to the end
for _, version := range Versions {
if exists, err := version.CheckExists(); err != nil || exists {
if exists {
2016-01-05 00:32:53 +01:00
continue
}
return err
}
2017-05-20 15:21:13 +02:00
didRun = true
2016-01-05 00:32:53 +01:00
if err := version.Run(); err != nil {
version.TryRollback()
return err
}
}
2017-05-20 15:21:13 +02:00
if didRun {
fmt.Println("Migrations Finished")
}
2016-01-05 00:32:53 +01:00
return nil
}