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-02-12 13:15:15 +01:00
|
|
|
{Version: "2.8.51"},
|
2022-06-23 17:37:00 +02:00
|
|
|
{Version: "2.8.57"},
|
2022-10-28 16:59:50 +02:00
|
|
|
{Version: "2.8.58"},
|
2023-07-07 23:15:30 +02:00
|
|
|
{Version: "2.8.91"},
|
2023-09-11 01:02:10 +02:00
|
|
|
{Version: "2.9.6"},
|
2024-01-30 10:26:11 +01:00
|
|
|
{Version: "2.9.46"},
|
2024-02-11 21:24:53 +01:00
|
|
|
{Version: "2.9.60"},
|
2024-03-06 22:17:34 +01:00
|
|
|
{Version: "2.9.61"},
|
2024-03-07 19:50:48 +01:00
|
|
|
{Version: "2.9.62"},
|
2024-04-19 18:47:08 +02:00
|
|
|
{Version: "2.9.70"},
|
2024-05-21 16:05:38 +02:00
|
|
|
{Version: "2.9.97"},
|
2024-05-22 19:00:28 +02:00
|
|
|
{Version: "2.9.100"},
|
2024-06-30 21:57:03 +02:00
|
|
|
{Version: "2.10.12"},
|
2024-07-02 20:45:59 +02:00
|
|
|
{Version: "2.10.15"},
|
2024-07-09 21:26:12 +02:00
|
|
|
{Version: "2.10.16"},
|
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
|
|
|
|
}
|