-
Notifications
You must be signed in to change notification settings - Fork 1
/
migrations.go
52 lines (48 loc) · 1.3 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
package main
import "database/sql"
var migrations = []func(w *worker){
func(w *worker) {
w.mustExec(`
create table if not exists feedback (
chat_id integer,
text text);`)
w.mustExec(`
create table if not exists users (
chat_id integer primary key,
external_id text not null default '');`)
w.mustExec(`
create table if not exists addresses (
chat_id integer,
username text not null default '',
muted integer not null default 0);`)
w.mustExec(`
create table if not exists delivered_ids (
chat_id integer,
message_id text not null default '')`)
},
func(w *worker) {
w.mustExec("alter table addresses add next_delivery integer not null default 0")
},
}
func (w *worker) applyMigrations() {
row := w.db.QueryRow("select version from schema_version")
var version int
err := row.Scan(&version)
if err == sql.ErrNoRows {
version = -1
w.mustExec("insert into schema_version(version) values (0)")
} else {
checkErr(err)
}
for i, m := range migrations[version+1:] {
n := i + version + 1
linf("applying migration %d", n)
m(w)
w.mustExec("update schema_version set version=?", n)
}
}
func (w *worker) createDatabase() {
linf("creating database if needed...")
w.mustExec(`create table if not exists schema_version (version integer);`)
w.applyMigrations()
}