-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathdatabase.go
More file actions
243 lines (209 loc) · 6.72 KB
/
database.go
File metadata and controls
243 lines (209 loc) · 6.72 KB
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
package githubanalysis
import (
"database/sql"
"fmt"
"time"
"github.com/benfred/github-analysis/config"
"github.com/google/go-github/github"
"github.com/lib/pq"
)
// Database connection to the github postgres database
type Database struct {
*sql.DB
}
// Connect to the database
func Connect(cfg config.Config) (*Database, error) {
connStr := fmt.Sprintf("user='%s' password='%s' dbname='%s' host='%s' sslmode=disable",
cfg.Database.Username, cfg.Database.Password, cfg.Database.DBName, cfg.Database.Host)
db, err := sql.Open("postgres", connStr)
if err != nil {
return nil, err
}
return &Database{DB: db}, nil
}
// InsertUserStatus updates the statuscode/fetchtime associated with a user in the case that it
// can't be fetched
func (conn *Database) InsertUserStatus(id int64, login string, statuscode int, fetchtime time.Time) error {
sql := `INSERT INTO users (id, login, fetched, statuscode) VALUES ($1, $2, $3, $4)
ON CONFLICT(id) DO UPDATE SET fetched=$3, statuscode=$4`
// TODO: don't use prepare?
stmt, err := conn.Prepare(sql)
if err != nil {
fmt.Printf("Failed to prepare: %s\n", err.Error())
return err
}
_, err = stmt.Exec(id, login, fetchtime, statuscode)
if err != nil {
fmt.Printf("Failed to exec: %s\n", err.Error())
return err
}
return err
}
// InsertUser inserts a github.User object into the database
func (conn *Database) InsertUser(statuscode *int, fetchtime *time.Time, user *github.User, upsert bool) error {
sql := `INSERT INTO users (id, login, name, company, location, bio, email, type, followers, following,
created, modified, fetched, statuscode, blog)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) `
if upsert {
sql += `ON CONFLICT(id) DO UPDATE SET login=$2, name=$3, company=$4, location=$5, bio=$6, email=$7, type=$8,
followers=$9, following=$10, created=$11, modified=$12, fetched=$13, statuscode=$14, blog=$15`
} else {
sql += `ON CONFLICT(id) DO NOTHING`
}
var modified *time.Time
if user.UpdatedAt != nil {
modified = &user.UpdatedAt.Time
}
var created *time.Time
if user.CreatedAt != nil {
created = &user.CreatedAt.Time
}
_, err := conn.Exec(sql, user.GetID(),
user.GetLogin(),
user.Name,
user.Company,
user.Location,
user.Bio,
user.Email,
user.Type,
user.Followers,
user.Following,
created,
modified,
fetchtime,
statuscode,
user.Blog)
return err
}
// InsertRepoStatus updates the statuscode/fetchtime associated with a repo in the case that it
// can't be fetched
func (conn *Database) InsertRepoStatus(repoid int64, reponame string, statuscode int, fetchtime time.Time, upsert bool) error {
sql := `INSERT INTO repos (id, name, fetched, statuscode) VALUES ($1, $2, $3, $4)`
if upsert {
sql += ` ON CONFLICT(id) DO UPDATE SET name=$2, fetched=$3, statuscode=$4`
} else {
sql += ` ON CONFLICT(id) DO NOTHING`
}
stmt, err := conn.Prepare(sql)
if err != nil {
fmt.Printf("Failed to prepare: %s\n", err.Error())
return err
}
_, err = stmt.Exec(repoid, reponame, fetchtime, statuscode)
return err
}
// InsertRepo inserts a github.Repository object into the database
func (conn *Database) InsertRepo(statuscode *int, fetchtime *time.Time, repo *github.Repository, upsert bool) error {
sql := `INSERT INTO repos (id, name, language, description, size, stars, forks, topics, parentid,
ownerid, created, modified, fetched, statuscode, license, homepage)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)`
if upsert {
sql += ` ON CONFLICT(id) DO UPDATE SET name=$2, language=$3, description=$4, size=$5, stars=$6,
forks=$7, topics=$8, parentid=$9, ownerid=$10, created=$11, modified=$12, fetched=$13,
statuscode=$14, license=$15, homepage=$16`
} else {
sql += ` ON CONFLICT(id) DO NOTHING`
}
owner := repo.GetOwner()
var ownerid *int
if owner != nil {
ownerid = owner.ID
err := conn.InsertUser(nil, nil, owner, false)
if err != nil {
return err
}
}
var parentid *int
parent := repo.GetParent()
if parent != nil {
// If we have a parent, insert the parent if its missing
err := conn.InsertRepo(nil, nil, parent, false)
if err != nil {
return err
}
parentid = parent.ID
}
var modified *time.Time
if repo.PushedAt != nil {
modified = &repo.PushedAt.Time
}
var created *time.Time
if repo.CreatedAt != nil {
created = &repo.CreatedAt.Time
}
_, err := conn.Exec(sql, repo.GetID(),
repo.GetFullName(),
repo.Language,
repo.Description,
repo.GetSize(),
repo.GetStargazersCount(),
repo.GetForksCount(),
pq.Array(repo.Topics),
parentid,
ownerid,
created,
modified,
fetchtime,
statuscode,
repo.GetLicense().GetKey(),
repo.GetHomepage())
if err != nil {
fmt.Printf("Failed to insert github repo: %s", err.Error())
return err
}
return nil
}
// HasRepo returns whether the repo has been fetched
func (conn *Database) HasRepo(repoid int64) (bool, error) {
// TODO: this doesn't seem all that good
rows, err := conn.Query("SELECT fetched from repos where id=$1 and fetched is not null", repoid)
if err != nil {
return false, err
}
defer rows.Close()
for rows.Next() {
var fetched time.Time
if err := rows.Scan(&fetched); err != nil {
return false, err
}
return true, nil
}
return false, nil
}
// HasUser returns whether the user has been fetched
func (conn *Database) HasUser(userid int64) (bool, error) {
// TODO: this doesn't seem all that good
rows, err := conn.Query("SELECT fetched from users where id=$1 and fetched is not null", userid)
if err != nil {
return false, err
}
defer rows.Close()
for rows.Next() {
var fetched time.Time
if err := rows.Scan(&fetched); err != nil {
return false, err
}
return true, nil
}
return false, nil
}
// InsertOrganizationMembers inserts or updates the list of public organization members in the database
func (conn *Database) InsertOrganizationMembers(orgid int64, orgname string, members []*github.User, statuscode int, fetchtime time.Time, upsert bool) error {
// Insert a stub user if not already fetched for each user in the organization
// Note purposefully setting statuscode/fetched time to nil here to mark as not fetched
// and setting upsert flag to false to prevent overwriting good data
var memberids []int
for _, user := range members {
memberids = append(memberids, *user.ID)
conn.InsertUser(nil, nil, user, false)
}
fmt.Printf("Writing: %s - %d members\n", orgname, len(memberids))
sql := `INSERT INTO organization_members (organization, members, fetched, statuscode) VALUES ($1, $2, $3, $4)`
if upsert {
sql += ` ON CONFLICT(organization) DO UPDATE SET members=$2, fetched=$3, statuscode=$4`
} else {
sql += ` ON CONFLICT(id) DO NOTHING`
}
_, err := conn.Exec(sql, orgid, pq.Array(memberids), fetchtime, statuscode)
return err
}