/
db.go
256 lines (219 loc) · 6.15 KB
/
db.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package main
import (
"context"
"database/sql"
"fmt"
"os"
"github.com/luna-duclos/instrumentedsql"
"github.com/mattn/go-sqlite3"
homedir "github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
)
var Conn *sql.DB
func dbMigrate() error {
row := Conn.QueryRow(`select name from sqlite_master where type='table' and name='migration_info'`)
var blackhole string
err := row.Scan(&blackhole)
if err == sql.ErrNoRows {
_, err := Conn.Exec(`create table migration_info (
id integer not null primary key
)`)
if err != nil {
return errors.WithStack(err)
}
} else if err != nil {
return errors.WithStack(err)
}
err = doMigration(1, `
create table accounts (
id integer not null primary key,
displayName string not null,
urlBase string not null,
apiUrlBase string not null,
accessToken string not null
);
create table channels (
id integer not null primary key,
displayName string not null,
system string,
queries string not null,
accountID integer not null,
FOREIGN KEY(accountID) REFERENCES accounts(id)
);
create table github_users (
id integer not null primary key,
login string not null,
avatarURL string not null
);
create table issues (
id integer not null primary key,
number integer not null,
title string not null,
userID integer not null,
repoOwner string not null,
repoName string not null,
state string not null,
locked bool not null,
comments integer not null,
createdAt string not null,
updatedAt string not null,
closedAt string,
isPullRequest boolean not null,
body string not null,
alreadyRead boolean not null,
milestoneID integer,
FOREIGN KEY(userID) REFERENCES github_users(id)
FOREIGN KEY(milestoneID) REFERENCES milestones(id)
);
create table labels (
id integer not null primary key,
name string not null,
color string not null,
'default' boolean not null
);
create table milestones (
id integer not null primary key,
number integer not null,
title string not null,
description string not null,
state string not null,
createdAt string not null,
updatedAt string not null,
closedAt string
);
create table assigned_labels_to_issue (
id integer not null primary key,
issueID integer not null,
labelID integer not null,
FOREIGN KEY(issueID) REFERENCES issues(id)
FOREIGN KEY(labelID) REFERENCES labels(id)
);
create unique index uniq_issue_label on assigned_labels_to_issue(issueID, labelID);
create table assigned_users_to_issue (
id integer not null primary key,
issueID integer not null,
userID integer not null,
FOREIGN KEY(issueID) REFERENCES issues(id)
FOREIGN KEY(userID) REFERENCES github_users(id)
);
create unique index uniq_assigned_user_to_issue on assigned_users_to_issue(issueID, userID);
`)
if err != nil {
return errors.WithStack(err)
}
err = doMigration(2, `
create table queries (
id integer not null primary key,
query string not null
);
create table channel_issues (
id integer not null primary key,
issueID integer not null,
channelID integer not null,
queryID integer not null,
FOREIGN KEY(issueID) REFERENCES issues(id)
FOREIGN KEY(channelID) REFERENCES channels(id)
FOREIGN KEY(queryID) REFERENCES queries(id)
);
create unique index uniq_channel_issue on channel_issues(issueID, channelID, queryID);
`)
if err != nil {
return errors.WithStack(err)
}
err = doMigration(3, `
create index fk_channel_account_id on channels(accountID);
create index fk_issue_user_id on issues(userID);
create index fk_issue_milestone_id on issues(milestoneID);
`)
if err != nil {
return errors.WithStack(err)
}
// Change order of index
err = doMigration(4, `
drop index uniq_channel_issue;
create unique index uniq_channel_issue on channel_issues(channelID, issueID, queryID);
`)
if err != nil {
return errors.WithStack(err)
}
err = doMigration(5, `
alter table issues add column merged boolean;
`)
if err != nil {
return errors.WithStack(err)
}
return nil
}
func doMigration(id int, query string) error {
ctx := context.Background()
return tx(func(tx *sql.Tx) error {
exist, err := rowExist(ctx, "migration_info", id, tx)
if err != nil {
return err
}
if exist {
return nil
}
_, err = tx.Exec(query)
if err != nil {
return errors.WithStack(err)
}
_, err = tx.Exec(`insert into migration_info(id) values(?)`, id)
if err != nil {
return errors.WithStack(err)
}
return nil
})
}
func tx(f func(*sql.Tx) error) error {
tx, err := Conn.Begin()
if err != nil {
return errors.WithStack(err)
}
err = f(tx)
if err != nil {
tx.Rollback()
return errors.Wrap(err, "Transaction Rollbacked")
}
tx.Commit()
return nil
}
func init() {
logger := instrumentedsql.LoggerFunc(func(ctx context.Context, msg string, keyvals ...interface{}) {
// log.Printf("%s %v", msg, keyvals)
})
sql.Register("instrumented-sqlite", instrumentedsql.WrapDriver(&sqlite3.SQLiteDriver{}, instrumentedsql.WithLogger(logger)))
fname, err := homedir.Expand("~/.cache/korat/development.sqlite3")
if err != nil {
panic(err)
}
db, err := sql.Open("instrumented-sqlite", fname)
if err != nil {
panic(err)
}
Conn = db
_, err = Conn.Exec("PRAGMA foreign_keys = ON;")
if err != nil {
panic(err)
}
err = dbMigrate()
if err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(1)
}
}
type sqlConn interface {
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
}
func rowExist(ctx context.Context, tbl string, id int, conn sqlConn) (bool, error) {
row := conn.QueryRowContext(ctx, fmt.Sprintf(`select 1 from %s where id = ?`, tbl), id)
var blackhole int
err := row.Scan(&blackhole)
if err == sql.ErrNoRows {
return false, nil
} else if err != nil {
return false, errors.WithStack(err)
}
return true, nil
}