-
Notifications
You must be signed in to change notification settings - Fork 13
/
migrations.go
executable file
·59 lines (54 loc) · 1.21 KB
/
migrations.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
package main
import (
"database/sql"
"log"
"time"
m "github.com/remind101/migrate"
"github.com/tectiv3/standardfile/db"
)
//Migrate performs migration
func Migrate() {
db.Init(cfg.DB)
migrations := getMigrations()
err := m.Exec(db.DB(), m.Up, migrations...)
if err != nil {
log.Fatal(err)
}
log.Println("Done")
}
func getMigrations() []m.Migration {
migrations := []m.Migration{
{
ID: 1,
Up: m.Queries([]string{
"ALTER TABLE users ADD COLUMN pw_auth varchar(255);",
"ALTER TABLE users ADD COLUMN pw_salt varchar(255);",
}),
Down: func(tx *sql.Tx) error {
// It's not possible to remove a column with sqlite.
return nil
},
},
{
ID: 2,
Up: func(tx *sql.Tx) error {
users := []User{}
db.Select("SELECT * FROM `users`", &users)
log.Println("Got", len(users), "users to update")
for _, u := range users {
if u.Email == "" || u.PwNonce == "" {
continue
}
if _, err := tx.Exec("UPDATE `users` SET `pw_salt`=?, `updated_at`=? WHERE `uuid`=?", getSalt(u.Email, u.PwNonce), time.Now(), u.UUID); err != nil {
log.Println(err)
}
}
return nil
},
Down: func(tx *sql.Tx) error {
return nil
},
},
}
return migrations
}