-
Notifications
You must be signed in to change notification settings - Fork 15
/
migrations.go
154 lines (151 loc) · 4.53 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"database/sql"
)
var migrations = []func(w *worker){
func(w *worker) {
w.mustExec(`
create table if not exists signals (
chat_id integer,
model_id text,
endpoint text not null default '',
primary key (chat_id, model_id, endpoint));`)
w.mustExec(`
create table if not exists status_changes (
model_id text,
status integer not null default 0,
timestamp integer not null default 0);`)
w.mustExec(`
create table if not exists last_status_changes (
model_id text primary key,
status integer not null default 0,
timestamp integer not null default 0);`)
w.mustExec(`
create table if not exists models (
model_id text primary key,
status integer not null default 0,
referred_users integer not null default 0);`)
w.mustExec(`
create table if not exists feedback (
chat_id integer,
text text,
endpoint text not null default '');`)
w.mustExec(`
create table if not exists block (
chat_id integer,
block integer not null default 0,
endpoint text not null default '',
primary key(chat_id, endpoint));`)
w.mustExec(`
create table if not exists users (
chat_id integer primary key,
max_models integer not null default 0,
reports integer not null default 0);`)
w.mustExec(`
create table if not exists emails (
chat_id integer,
endpoint text not null default '',
email text not null default '',
primary key(chat_id, endpoint));`)
w.mustExec(`
create table if not exists transactions (
local_id text primary key,
kind text,
chat_id integer,
remote_id text,
timeout integer,
amount text,
address string,
status_url string,
checkout_url string,
dest_tag string,
status integer,
timestamp integer,
model_number integer,
currency text,
endpoint text not null default '');`)
w.mustExec(`
create table if not exists referrals (
chat_id integer primary key,
referral_id text not null default '',
referred_users integer not null default 0);`)
},
func(w *worker) {
w.mustExec("create index ix_status_changes_model_id on status_changes(model_id);")
},
func(w *worker) {
w.mustExec("alter table users add blacklist integer not null default 0;")
},
func(w *worker) {
w.mustExec("alter table users add show_images integer not null default 1;")
},
func(w *worker) {
w.mustExec("alter table users add offline_notifications integer not null default 1;")
},
func(w *worker) {
w.mustExec(`
create table interactions (
priority integer not null,
timestamp integer not null,
endpoint text not null,
chat_id integer not null,
result integer not null,
delay integer not null);`)
},
func(w *worker) {
w.mustExec("alter table models add special integer not null default 0;")
},
func(w *worker) {
w.mustExec("create index ix_status_changes_timestamp on status_changes(timestamp);")
},
func(w *worker) {
w.mustExec("alter table signals add confirmed integer not null default 1;")
w.mustExec("create index ix_signals_confirmed on signals(confirmed);")
},
func(w *worker) {
w.mustExec("update models set status = 1 << (status - 1);")
w.mustExec("update status_changes set status = 1 << (status - 1);")
w.mustExec("update last_status_changes set status = 1 << (status - 1);")
},
func(w *worker) {
w.mustExec(`
create table notification_queue (
id integer primary key autoincrement,
endpoint text not null,
chat_id integer not null,
model_id text not null,
status integer not null,
time_diff integer,
image_url text,
social integer not null default 0,
priority integer not null default 0,
sound integer not null default 0,
sending integer not null default 0);`)
},
func(w *worker) {
w.mustExec("create index ix_interactions_endpoint on interactions(endpoint);")
w.mustExec("create index ix_interactions_timestamp on interactions(timestamp);")
},
func(w *worker) {
w.mustExec("alter table interactions add kind integer not null default 0;")
w.mustExec("alter table notification_queue add kind 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)
}
linf("no more migrations")
}