-
Notifications
You must be signed in to change notification settings - Fork 75
/
cockroachdb.go
398 lines (344 loc) · 10.3 KB
/
cockroachdb.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright (c) 2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package cockroachdb
import (
"fmt"
"net/url"
"path/filepath"
"sync"
"time"
"github.com/decred/politeia/politeiawww/legacy/codetracker/github/database"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
const (
cacheID = "ghtracker"
// Database table names
tableNameVersions = "versions"
tableNamePullRequest = "pullrequests"
tableNameCommits = "commits"
tableNameReviews = "reviews"
userPoliteiawww = "politeiawww" // cmsdb user (read/write access)
)
// cockroachdb implements the cache interface.
type cockroachdb struct {
sync.RWMutex
shutdown bool // Backend is shutdown
recordsdb *gorm.DB // Database context
}
// NewPullRequest creates a new pull request row in the db.
func (c *cockroachdb) NewPullRequest(dbPullRequest *database.PullRequest) error {
pr := EncodePullRequest(dbPullRequest)
log.Debugf("NewPullRequest: %v", pr.URL)
return c.recordsdb.Create(&pr).Error
}
// UpdatePullRequest updates an existing pull request row in the db.
func (c *cockroachdb) UpdatePullRequest(dbPullRequest *database.PullRequest) error {
pr := EncodePullRequest(dbPullRequest)
log.Debugf("UpdatePullRequest: %v", pr.URL)
return c.recordsdb.Save(&pr).Error
}
// PullRequestByURL return a PullRequest by its URL.
func (c *cockroachdb) PullRequestByID(id string) (*database.PullRequest, error) {
log.Debugf("PullRequestByID: %v", id)
pr := PullRequest{
ID: id,
}
err := c.recordsdb.
Find(&pr).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
err = database.ErrNoPullRequestFound
}
return nil, err
}
return DecodePullRequest(&pr), nil
}
// MergedPullRequestsByUserDates takes a username, start and end date and
// returns merged pull requests that match those criteria.
func (c *cockroachdb) MergedPullRequestsByUserDates(username string, start, end int64) ([]*database.PullRequest, error) {
log.Debugf("MergedPullRequestsByUserDates: %v %v", time.Unix(start, 0),
time.Unix(end, 0))
// Get all PRs from a user between the given dates.
prs := make([]PullRequest, 0, 1024) // PNOOMA
err := c.recordsdb.
Where("author = ? AND "+
"merged_at BETWEEN ? AND ?",
username,
start,
end).
Find(&prs).
Error
if err != nil {
return nil, err
}
dbPRs := make([]*database.PullRequest, 0, len(prs))
for _, vv := range prs {
dbPRs = append(dbPRs, DecodePullRequest(&vv))
}
return dbPRs, nil
}
// UpdatedPullRequestsByUserDates takes a username, start and end date and
// returns updated pull requests that match those criteria.
func (c *cockroachdb) UpdatedPullRequestsByUserDates(username string, start, end int64) ([]*database.PullRequest, error) {
log.Debugf("UpdatedPullRequestsByUserDates: %v %v", time.Unix(start, 0),
time.Unix(end, 0))
// Select the most recent pullrequests (by url) that match author and are
// between start and end.
query := `
SELECT * FROM pullrequests
WHERE author = $1 AND updated_at IN
(SELECT
MAX(updated_at)
FROM pullrequests
WHERE updated_at BETWEEN $2 AND $3
GROUP BY url
)`
rows, err := c.recordsdb.Raw(query, username, start, end).Rows()
if err != nil {
return nil, err
}
defer rows.Close()
prs := make([]PullRequest, 0, 1024) // PNOOMA
for rows.Next() {
var pr PullRequest
err := c.recordsdb.ScanRows(rows, &pr)
if err != nil {
return nil, err
}
prs = append(prs, pr)
}
if err = rows.Err(); err != nil {
return nil, err
}
dbPRs := make([]*database.PullRequest, 0, len(prs))
for _, pr := range prs {
dbPRs = append(dbPRs, DecodePullRequest(&pr))
}
return dbPRs, nil
}
// PullRequestByURL takes a url and returns all PRs that may match.
func (c *cockroachdb) PullRequestsByURL(url string) ([]*database.PullRequest, error) {
urlPRs := make([]PullRequest, 0, 1024) // PNOOMA
err := c.recordsdb.
Where("url = ?",
url).
Find(&urlPRs).
Error
if err != nil {
return nil, err
}
dbPRs := make([]*database.PullRequest, 0, len(urlPRs))
for _, pr := range urlPRs {
dbPRs = append(dbPRs, DecodePullRequest(&pr))
}
return dbPRs, nil
}
// MatchingReviews is a struct that will return results from a review
// request.
type MatchingReviews struct {
PullRequestURL string
ID int64
Author string
State string
SubmittedAt int64
CommitID string
Repo string
Number int
Additions int
Deletions int
}
// ReviewsByUserDates takes username, start and end date and returns all reviews
// that match the provided criteria.
func (c *cockroachdb) ReviewsByUserDates(username string, start, end int64) ([]database.PullRequestReview, error) {
log.Debugf("ReviewsByUserDates: %v %v", time.Unix(start, 0),
time.Unix(end, 0))
// Get all Reviews from a user between the given dates.
query := `
SELECT
reviews.pull_request_url,
reviews.id,
reviews.author,
reviews.state,
reviews.submitted_at,
reviews.commit_id,
reviews.repo,
reviews.number,
pullrequests.additions,
pullrequests.deletions
FROM reviews
INNER JOIN pullrequests
ON pullrequests.url = reviews.pull_request_url
WHERE reviews.author = $1 AND reviews.state = $2 AND
reviews.submitted_at BETWEEN $3 AND $4`
rows, err := c.recordsdb.Raw(query, username, "APPROVED", start, end).Rows()
if err != nil {
return nil, err
}
defer rows.Close()
matching := make([]MatchingReviews, 0, 1024)
for rows.Next() {
var i MatchingReviews
err := c.recordsdb.ScanRows(rows, &i)
if err != nil {
return nil, err
}
matching = append(matching, i)
}
if err = rows.Err(); err != nil {
return nil, err
}
return convertMatchingReviewsToDatabaseReviews(matching), nil
}
// NewPullRequestReview creates a new pull request review row.
func (c *cockroachdb) NewPullRequestReview(dbPullRequestReview *database.PullRequestReview) error {
pr := EncodePullRequestReview(dbPullRequestReview)
log.Debugf("NewPullRequestReview: %v", pr.CommitID)
return c.recordsdb.Create(&pr).Error
}
// UpdatePullRequestReview updates an existing pull request review row.
func (c *cockroachdb) UpdatePullRequestReview(dbPullRequestReview *database.PullRequestReview) error {
pr := EncodePullRequestReview(dbPullRequestReview)
log.Debugf("UpdatePullRequestReview: %v", pr.CommitID)
return c.recordsdb.Save(&pr).Error
}
// This function must be called within a transaction.
func createGHTables(tx *gorm.DB) error {
log.Infof("createGHTables")
// Create cms tables
if !tx.HasTable(tableNamePullRequest) {
err := tx.CreateTable(&PullRequest{}).Error
if err != nil {
return err
}
}
if !tx.HasTable(tableNameReviews) {
err := tx.CreateTable(&PullRequestReview{}).Error
if err != nil {
return err
}
}
if !tx.HasTable(tableNameCommits) {
err := tx.CreateTable(&Commit{}).Error
if err != nil {
return err
}
}
return nil
}
// ReviewByID returns a review given the provided id.
func (c *cockroachdb) ReviewByID(id int64) (*database.PullRequestReview, error) {
log.Debugf("ReviewByID: %v", id)
review := PullRequestReview{
ID: id,
}
err := c.recordsdb.
Find(&review).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
err = database.ErrNoPullRequestReviewFound
}
return nil, err
}
return DecodePullRequestReview(&review), nil
}
// NewCommit creates a new commit row.
func (c *cockroachdb) NewCommit(dbCommit *database.Commit) error {
commit := encodeCommit(dbCommit)
log.Debugf("NewCommit: %v", commit.SHA)
return c.recordsdb.Create(&commit).Error
}
// CommitBySHA returns a commit given the provided hash.
func (c *cockroachdb) CommitBySHA(sha string) (*database.Commit, error) {
log.Debugf("CommitBySHA: %v", sha)
commit := Commit{
SHA: sha,
}
err := c.recordsdb.
Find(&commit).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
err = database.ErrNoCommitFound
}
return nil, err
}
return decodeCommit(&commit), nil
}
// CommitsByUserDates takes a username, start and end date and
// returns commits that match those criteria.
func (c *cockroachdb) CommitsByUserDates(username string, start, end int64) ([]database.Commit, error) {
log.Debugf("CommitsByUserDates: %v %v", time.Unix(start, 0),
time.Unix(end, 0))
// Get all commits from a user between the given dates.
commits := make([]Commit, 0, 1024) // PNOOMA
err := c.recordsdb.
Where("author = ? AND "+
"date BETWEEN ? AND ?",
username,
start,
end).
Find(&commits).
Error
if err != nil {
return nil, err
}
dbCommits := make([]database.Commit, 0, len(commits))
for _, vv := range commits {
dbCommits = append(dbCommits, *decodeCommit(&vv))
}
return dbCommits, nil
}
// Setup calls the tables creation function to ensure the database is prepared
// for use.
func (c *cockroachdb) Setup() error {
tx := c.recordsdb.Begin()
err := createGHTables(tx)
if err != nil {
tx.Rollback()
return err
}
return tx.Commit().Error
}
func buildQueryString(user, rootCert, cert, key string) string {
v := url.Values{}
v.Set("sslmode", "require")
v.Set("sslrootcert", filepath.Clean(rootCert))
v.Set("sslcert", filepath.Join(cert))
v.Set("sslkey", filepath.Join(key))
return v.Encode()
}
// New returns a new cockroachdb context that contains a connection to the
// specified database that was made using the politeiawww user and the passed
// in certificates.
func New(host, rootCert, cert, key string) (*cockroachdb, error) {
log.Tracef("New: %v %v %v %v %v", host, rootCert, cert, key)
// Connect to database
dbName := cacheID
h := "postgresql://" + userPoliteiawww + "@" + host + "/" + dbName
u, err := url.Parse(h)
if err != nil {
return nil, fmt.Errorf("parse url '%v': %v", h, err)
}
qs := buildQueryString(u.User.String(), rootCert, cert, key)
addr := u.String() + "?" + qs
db, err := gorm.Open("postgres", addr)
if err != nil {
return nil, fmt.Errorf("connect to database '%v': %v", addr, err)
}
// Create context
c := &cockroachdb{
recordsdb: db,
}
// Disable gorm logging. This prevents duplicate errors from
// being printed since we handle errors manually.
c.recordsdb.LogMode(false)
// Disable automatic table name pluralization. We set table
// names manually.
c.recordsdb.SingularTable(true)
return c, err
}
// Close satisfies the database interface.
func (c *cockroachdb) Close() error {
return c.recordsdb.Close()
}