forked from TF2Stadium/Helen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchanges.go
65 lines (53 loc) · 1.46 KB
/
changes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (C) 2015 TF2Stadium
// Use of this source code is governed by the GPLv3
// that can be found in the COPYING file.
package migrations
import (
"github.com/Sirupsen/logrus"
db "github.com/TF2Stadium/Helen/database"
"github.com/blang/semver"
)
//follows semantic versioning scheme
var schemaVersion = semver.Version{
Major: 11,
Minor: 0,
Patch: 0,
}
type Constant struct {
SchemaVersion string
}
func getCurrConstants() *Constant {
constant := &Constant{}
db.DB.Table("constants").Last(constant)
return constant
}
func writeConstants() {
db.DB.Exec("UPDATE constants SET schema_version = ?", schemaVersion.String())
logrus.Info("Current Schema Version: ", getCurrConstants().SchemaVersion)
}
func checkSchema() {
var count int
defer writeConstants()
db.DB.Table("constants").Where("schema_version = ?", schemaVersion.String()).Count(&count)
if count == 1 {
return
}
currStr := getCurrConstants().SchemaVersion
if currStr == "" {
db.DB.Save(&Constant{
schemaVersion.String(),
})
//Initial database migration
whitelist_id_string()
//Write current schema version
return
}
if v, _ := semver.Parse(currStr); v.Major < schemaVersion.Major {
logrus.Warning("Incompatible schema change detected (", currStr, ") attempting to migrate to (", schemaVersion.String(), ")")
for i := v.Major + 1; i <= schemaVersion.Major; i++ {
logrus.Debug("Calling migration routine for ", schemaVersion.String())
f := migrationRoutines[i]
f()
}
}
}