2022-01-23 14:03:23 +01:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Migration represents sql schema version
|
|
|
|
type Migration struct {
|
|
|
|
Version string `db:"version" json:"version"`
|
|
|
|
UpgradedDate *time.Time `db:"upgraded_date" json:"upgraded_date"`
|
|
|
|
Notes *string `db:"notes" json:"notes"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// HumanoidVersion adds a v to the VersionString
|
|
|
|
func (m Migration) HumanoidVersion() string {
|
|
|
|
return "v" + m.Version
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetMigrations() []Migration {
|
|
|
|
return []Migration{
|
2022-01-23 17:34:42 +01:00
|
|
|
{Version: "0.0.0"},
|
2022-01-23 14:03:23 +01:00
|
|
|
{Version: "1.0.0"},
|
|
|
|
{Version: "1.2.0"},
|
|
|
|
{Version: "1.3.0"},
|
|
|
|
{Version: "1.4.0"},
|
|
|
|
{Version: "1.5.0"},
|
|
|
|
{Version: "1.6.0"},
|
|
|
|
{Version: "1.7.0"},
|
|
|
|
{Version: "1.8.0"},
|
|
|
|
{Version: "1.9.0"},
|
|
|
|
{Version: "2.2.1"},
|
|
|
|
{Version: "2.3.0"},
|
|
|
|
{Version: "2.3.1"},
|
|
|
|
{Version: "2.3.2"},
|
|
|
|
{Version: "2.4.0"},
|
|
|
|
{Version: "2.5.0"},
|
|
|
|
{Version: "2.5.2"},
|
|
|
|
{Version: "2.7.1"},
|
|
|
|
{Version: "2.7.4"},
|
|
|
|
{Version: "2.7.6"},
|
|
|
|
{Version: "2.7.8"},
|
|
|
|
{Version: "2.7.9"},
|
|
|
|
{Version: "2.7.10"},
|
|
|
|
{Version: "2.7.12"},
|
|
|
|
{Version: "2.7.13"},
|
|
|
|
{Version: "2.8.0"},
|
|
|
|
{Version: "2.8.1"},
|
|
|
|
{Version: "2.8.7"},
|
|
|
|
{Version: "2.8.8"},
|
|
|
|
{Version: "2.8.20"},
|
|
|
|
{Version: "2.8.25"},
|
|
|
|
{Version: "2.8.26"},
|
2022-01-26 20:51:20 +01:00
|
|
|
{Version: "2.8.36"},
|
2022-01-31 22:30:36 +01:00
|
|
|
{Version: "2.8.38"},
|
2022-01-31 23:35:13 +01:00
|
|
|
{Version: "2.8.39"},
|
2022-02-03 08:05:13 +01:00
|
|
|
{Version: "2.8.40"},
|
2022-02-04 18:35:08 +01:00
|
|
|
{Version: "2.8.42"},
|
2022-01-23 14:03:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Migrate(d Store) error {
|
|
|
|
didRun := false
|
|
|
|
|
|
|
|
for _, version := range GetMigrations() {
|
|
|
|
if exists, err := d.IsMigrationApplied(version); err != nil || exists {
|
|
|
|
if exists {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
didRun = true
|
|
|
|
fmt.Printf("Executing migration %s (at %v)...\n", version.HumanoidVersion(), time.Now())
|
|
|
|
if err := d.ApplyMigration(version); err != nil {
|
|
|
|
fmt.Printf("Rolling back %s (time: %v)...\n", version.HumanoidVersion(), time.Now())
|
|
|
|
d.TryRollbackMigration(version)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if didRun {
|
|
|
|
fmt.Println("Migrations Finished")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|