2021-06-04 20:57:45 -04:00
|
|
|
package migrate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const table = "dbversion"
|
|
|
|
|
|
|
|
type Error struct {
|
|
|
|
description string
|
|
|
|
wrapped error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Error) Error() string {
|
|
|
|
return fmt.Sprintf("%s: %v", e.description, e.wrapped)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Error) Unwrap() error {
|
|
|
|
return e.wrapped
|
|
|
|
}
|
|
|
|
|
|
|
|
type Record struct {
|
|
|
|
Description string
|
|
|
|
F func(ctx Context) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type Context interface {
|
|
|
|
Exec(query string, args ...interface{}) (sql.Result, error)
|
|
|
|
Query(query string, args ...interface{}) (*sql.Rows, error)
|
|
|
|
}
|
|
|
|
|
2021-07-10 16:09:26 -04:00
|
|
|
func Apply(ctx Context, d Dialect, migrations []Record) (err error) {
|
|
|
|
if err = initialize(ctx, d); err == nil {
|
2021-06-04 20:57:45 -04:00
|
|
|
var currentVersion int64
|
2021-07-10 16:09:26 -04:00
|
|
|
if currentVersion, err = dbVersion(ctx, d); err == nil {
|
2021-06-04 20:57:45 -04:00
|
|
|
migrations = migrations[currentVersion:] // only apply what hasnt been been applied already
|
|
|
|
for i, m := range migrations {
|
2021-07-10 16:09:26 -04:00
|
|
|
if err = apply(ctx, d, m); err != nil {
|
2021-06-04 20:57:45 -04:00
|
|
|
err = Error{
|
|
|
|
description: fmt.Sprintf("error performing migration \"%s\"", migrations[i].Description),
|
|
|
|
wrapped: err,
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-10 16:09:26 -04:00
|
|
|
func initialize(ctx Context, d Dialect) (err error) {
|
|
|
|
if noVersionTable(ctx, d) {
|
|
|
|
return createVersionTable(ctx, d)
|
2021-06-04 20:57:45 -04:00
|
|
|
}
|
2021-07-10 16:09:26 -04:00
|
|
|
return
|
2021-06-04 20:57:45 -04:00
|
|
|
}
|
|
|
|
|
2021-07-10 16:09:26 -04:00
|
|
|
func noVersionTable(ctx Context, d Dialect) bool {
|
|
|
|
rows, table_check := ctx.Query(d.TableExists(table))
|
2021-06-04 20:57:45 -04:00
|
|
|
if rows != nil {
|
|
|
|
defer rows.Close()
|
|
|
|
}
|
|
|
|
return table_check != nil
|
|
|
|
}
|
|
|
|
|
2021-07-10 16:09:26 -04:00
|
|
|
func apply(ctx Context, d Dialect, r Record) (err error) {
|
2021-06-04 20:57:45 -04:00
|
|
|
if err = r.F(ctx); err == nil {
|
2021-07-10 16:09:26 -04:00
|
|
|
err = incrementVersion(ctx, d, r.Description)
|
2021-06-04 20:57:45 -04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-10 16:09:26 -04:00
|
|
|
func createVersionTable(ctx Context, d Dialect) (err error) {
|
|
|
|
_, err = ctx.Exec(d.CreateTable(table))
|
2021-06-04 20:57:45 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-10 16:09:26 -04:00
|
|
|
func incrementVersion(ctx Context, d Dialect, description string) (err error) {
|
|
|
|
_, err = ctx.Exec(d.InsertVersion(table), description, time.Now())
|
2021-06-04 20:57:45 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-10 16:09:26 -04:00
|
|
|
func dbVersion(ctx Context, d Dialect) (id int64, err error) {
|
|
|
|
row, err := ctx.Query(d.CheckVersion(table))
|
2021-06-04 20:57:45 -04:00
|
|
|
if row.Next() {
|
|
|
|
err = row.Scan(&id)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|