Semaphore/db/version.go
2018-10-16 00:48:19 +09:00

88 lines
2.0 KiB
Go

package db
import (
"fmt"
"strings"
"time"
)
// Version represents an sql schema version
type Version struct {
Major int
Minor int
Patch int
Build string
UpgradedDate *time.Time
Notes *string
}
// Versions holds all sql schema version references
var Versions []*Version
var initialSQL = `
create table ` + "`migrations`" + ` (
` + "`version`" + ` varchar(255) not null primary key,
` + "`upgraded_date`" + ` datetime null,
` + "`notes`" + ` text null
) engine=innodb charset=utf8;
`
// VersionString returns a well formatted string of the current Version
func (version *Version) VersionString() string {
s := fmt.Sprintf("%d.%d.%d", version.Major, version.Minor, version.Patch)
if len(version.Build) == 0 {
return s
}
return fmt.Sprintf("%s-%s", s, version.Build)
}
// HumanoidVersion adds a v to the VersionString
func (version *Version) HumanoidVersion() string {
return "v" + version.VersionString()
}
// GetPath is the humanoid version with the file format appended
func (version *Version) GetPath() string {
return version.HumanoidVersion() + ".sql"
}
//GetErrPath is the humanoid version with '.err' and file format appended
func (version *Version) GetErrPath() string {
return version.HumanoidVersion() + ".err.sql"
}
// GetSQL takes a path to an SQL file and returns it from packr as a slice of strings separated by newlines
func (version *Version) GetSQL(path string) []string {
sql, err := dbAssets.MustString(path)
if err != nil {
panic(err)
}
return strings.Split(sql, ";\n")
}
func init() {
Versions = []*Version{
{},
{Major: 1},
{Major: 1, Minor: 1},
{Major: 1, Minor: 2},
{Major: 1, Minor: 3},
{Major: 1, Minor: 4},
{Major: 1, Minor: 5},
{Minor: 1},
{Major: 1, Minor: 6},
{Major: 1, Minor: 7},
{Major: 1, Minor: 8},
{Major: 1, Minor: 9},
{Major: 2, Minor: 2, Patch: 1},
{Major: 2, Minor: 3},
{Major: 2, Minor: 3, Patch: 1},
{Major: 2, Minor: 3, Patch: 2},
{Major: 2, Minor: 4},
{Major: 2, Minor: 5},
{Major: 2, Minor: 5, Patch: 2},
}
}