-
Notifications
You must be signed in to change notification settings - Fork 1
/
migrate.go
91 lines (76 loc) · 1.56 KB
/
migrate.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"database/sql"
"log"
"strings"
)
// don't change these!
var defaultColors = []string{
"ff4d40",
"ffa540",
"40ff73",
"4071ff",
"ff4086",
"40ccff",
"5940ff",
"ff40f5",
"a940ff",
"e6ab68",
"4d4d4d",
}
func handleMigrateError(err error) {
log.Fatalln(err)
}
func migrateClasses(tx *sql.Tx) error {
classCountByUser := map[int]int{}
classes, err := DB.Query("SELECT id, name, COALESCE(teacher, ''), COALESCE(color, ''), userId FROM classes")
if err != nil {
return err
}
for classes.Next() {
id, name, teacher, color, userID := -1, "", "", "", -1
err = classes.Scan(&id, &name, &teacher, &color, &userID)
if err != nil {
return err
}
sortIndex, ok := classCountByUser[userID]
if !ok {
classCountByUser[userID] = 0
}
classCountByUser[userID]++
if color == "" {
// set an explicit color instead of inferring it
color = defaultColors[id%len(defaultColors)]
}
// update teacher, color, and sort index
_, err = tx.Exec(
"UPDATE classes SET teacher = ?, color = ?, sortIndex = ? WHERE id = ?",
teacher, strings.ToLower(color), sortIndex, id,
)
if err != nil {
return err
}
}
return nil
}
func migrate(name string) {
log.Printf("Starting migration '%s'...", name)
if name == "classes" {
tx, err := DB.Begin()
if err != nil {
handleMigrateError(err)
}
err = migrateClasses(tx)
if err != nil {
tx.Rollback()
handleMigrateError(err)
}
err = tx.Commit()
if err != nil {
handleMigrateError(err)
}
} else {
log.Fatalf("Unknown migration name!")
}
log.Println("Done!")
}