forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode_hosts.go
More file actions
347 lines (299 loc) · 9.11 KB
/
code_hosts.go
File metadata and controls
347 lines (299 loc) · 9.11 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
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
package database
import (
"context"
"database/sql"
"fmt"
"github.com/keegancsmith/sqlf"
"github.com/sourcegraph/sourcegraph/internal/database/basestore"
"github.com/sourcegraph/sourcegraph/internal/database/dbutil"
"github.com/sourcegraph/sourcegraph/internal/types"
)
type ListCodeHostsOpts struct {
*LimitOffset
// Only list code hosts with the given ID. This makes if effectively a getByID.
ID int32
// Only list code hosts with the given URL. This makes if effectively a getByURL.
URL string
// Cursor is used for pagination, it's the ID of the next code host to look at.
Cursor int32
// IncludeDeleted causes deleted code hosts to be returned as well. Note:
// For now deletion is a virtual concept where we check if all referencing
// external services are soft or hard deleted.
IncludeDeleted bool
// Search is an optional string to search through kind and URL.
Search string
}
// CodeHostStore provides access to the code_hosts table.
type CodeHostStore interface {
basestore.ShareableStore
With(other basestore.ShareableStore) CodeHostStore
WithTransact(context.Context, func(CodeHostStore) error) error
Count(ctx context.Context, opts ListCodeHostsOpts) (int32, error)
// GetByID gets the code host matching the specified ID.
GetByID(ctx context.Context, id int32) (*types.CodeHost, error)
// GetByURL gets the code host matching the specified url.
GetByURL(ctx context.Context, url string) (*types.CodeHost, error)
// List lists all code hosts matching the specified options.
List(ctx context.Context, opts ListCodeHostsOpts) (chs []*types.CodeHost, next int32, err error)
// Create creates a new code host in the db.
//
// If a code host with the given url already exists, it returns the existing code host.
Create(ctx context.Context, ch *types.CodeHost) error
// Update updates a code host, it uses the id field to match.
Update(ctx context.Context, ch *types.CodeHost) error
// Delete deletes the code host specified by the id.
Delete(ctx context.Context, id int32) error
}
// CodeHostsWith instantiates and returns a new CodeHostStore using the other stores
// handle.
func CodeHostsWith(other basestore.ShareableStore) CodeHostStore {
return &codeHostStore{Store: basestore.NewWithHandle(other.Handle())}
}
type codeHostStore struct {
*basestore.Store
}
func (s *codeHostStore) With(other basestore.ShareableStore) CodeHostStore {
return &codeHostStore{Store: s.Store.With(other)}
}
func (s *codeHostStore) copy() *codeHostStore {
return &codeHostStore{
Store: s.Store,
}
}
func (s *codeHostStore) WithTransact(ctx context.Context, f func(CodeHostStore) error) error {
return s.Store.WithTransact(ctx, func(tx *basestore.Store) error {
c := s.copy()
c.Store = tx
return f(c)
})
}
var codeHostColumnExpressions = []*sqlf.Query{
sqlf.Sprintf("code_hosts.id"),
sqlf.Sprintf("code_hosts.kind"),
sqlf.Sprintf("code_hosts.url"),
sqlf.Sprintf("code_hosts.api_rate_limit_quota"),
sqlf.Sprintf("code_hosts.api_rate_limit_interval_seconds"),
sqlf.Sprintf("code_hosts.git_rate_limit_quota"),
sqlf.Sprintf("code_hosts.git_rate_limit_interval_seconds"),
sqlf.Sprintf("code_hosts.created_at"),
sqlf.Sprintf("code_hosts.updated_at"),
}
type errCodeHostNotFound struct {
id int32
}
func (e errCodeHostNotFound) Error() string {
if e.id != 0 {
return fmt.Sprintf("code host with id %d not found", e.id)
}
return "code host not found"
}
func (errCodeHostNotFound) NotFound() bool {
return true
}
func (s *codeHostStore) GetByID(ctx context.Context, id int32) (*types.CodeHost, error) {
chs, _, err := s.List(ctx, ListCodeHostsOpts{LimitOffset: &LimitOffset{Limit: 1}, ID: id})
if err != nil {
return nil, err
}
if len(chs) != 1 {
return nil, errCodeHostNotFound{}
}
return chs[0], nil
}
func (s *codeHostStore) GetByURL(ctx context.Context, url string) (*types.CodeHost, error) {
// We would normally parse the URL here to verify its valid, but some code hosts have connections that
// have multiple URLs and in the code host table, they are represented by a code_hosts.url of:
// python/gomodules/etc...
chs, _, err := s.List(ctx, ListCodeHostsOpts{LimitOffset: &LimitOffset{Limit: 1}, URL: url})
if err != nil {
return nil, err
}
if len(chs) != 1 {
return nil, errCodeHostNotFound{}
}
return chs[0], nil
}
func (s *codeHostStore) Create(ctx context.Context, ch *types.CodeHost) error {
query := createCodeHostQuery(ch)
row := s.QueryRow(ctx, query)
return scan(row, ch)
}
func createCodeHostQuery(ch *types.CodeHost) *sqlf.Query {
return sqlf.Sprintf(
createCodeHostQueryFmtstr,
ch.Kind,
ch.URL,
ch.APIRateLimitQuota,
ch.APIRateLimitIntervalSeconds,
ch.GitRateLimitQuota,
ch.GitRateLimitIntervalSeconds,
sqlf.Join(codeHostColumnExpressions, ","),
sqlf.Join(codeHostColumnExpressions, ","),
ch.URL,
)
}
const createCodeHostQueryFmtstr = `
WITH inserted AS (
INSERT INTO
code_hosts (kind, url, api_rate_limit_quota, api_rate_limit_interval_seconds, git_rate_limit_quota, git_rate_limit_interval_seconds)
VALUES (%s, %s, %s, %s, %s, %s)
ON CONFLICT(url) DO NOTHING
RETURNING
%s
)
SELECT * FROM inserted
UNION
SELECT
%s
FROM code_hosts
WHERE url = %s
`
func (s *codeHostStore) List(ctx context.Context, opts ListCodeHostsOpts) (chs []*types.CodeHost, next int32, err error) {
query := listCodeHostsQuery(opts)
chs, err = basestore.NewSliceScanner(scanCodeHosts)(s.Query(ctx, query))
if err != nil || chs == nil {
// Return an empty list in case of no results
return []*types.CodeHost{}, 0, err
}
// Set the next value if we were able to fetch Limit + 1
if opts.LimitOffset != nil && opts.Limit > 0 && len(chs) == opts.Limit+1 {
next = chs[len(chs)-1].ID
chs = chs[:len(chs)-1]
}
return
}
func listCodeHostsQuery(opts ListCodeHostsOpts) *sqlf.Query {
// We fetch an extra one so that we have the `next` value
newLimitOffset := opts.LimitOffset
if newLimitOffset != nil && newLimitOffset.Limit > 0 {
newLimitOffset.Limit += 1
}
return sqlf.Sprintf(listCodeHostsQueryFmtstr, sqlf.Join(codeHostColumnExpressions, ","), listCodeHostsWhereQuery(opts), newLimitOffset.SQL())
}
const listCodeHostsQueryFmtstr = `
SELECT
%s
FROM
code_hosts
WHERE
%s
ORDER BY id ASC
%s
`
func (s *codeHostStore) Update(ctx context.Context, ch *types.CodeHost) error {
query := updateCodeHostQuery(ch)
row := s.QueryRow(ctx, query)
err := scan(row, ch)
if err != nil {
if err == sql.ErrNoRows {
return errCodeHostNotFound{ch.ID}
}
return err
}
return nil
}
func updateCodeHostQuery(ch *types.CodeHost) *sqlf.Query {
return sqlf.Sprintf(
updateCodeHostQueryFmtstr,
ch.Kind,
ch.URL,
ch.APIRateLimitQuota,
ch.APIRateLimitIntervalSeconds,
ch.GitRateLimitQuota,
ch.GitRateLimitIntervalSeconds,
ch.ID,
sqlf.Join(codeHostColumnExpressions, ","),
)
}
const updateCodeHostQueryFmtstr = `
UPDATE code_hosts
SET
kind = %s,
url = %s,
api_rate_limit_quota = %s,
api_rate_limit_interval_seconds = %s,
git_rate_limit_quota = %s,
git_rate_limit_interval_seconds = %s
WHERE
id = %s
RETURNING
%s`
func (s *codeHostStore) Delete(ctx context.Context, id int32) error {
query := deleteCodeHostQuery(id)
row := s.QueryRow(ctx, query)
if err := row.Err(); err != nil {
return err
}
return nil
}
func deleteCodeHostQuery(id int32) *sqlf.Query {
return sqlf.Sprintf(
deleteCodeHostQueryFmtstr,
id,
)
}
const deleteCodeHostQueryFmtstr = `
DELETE FROM code_hosts
WHERE
id = %s
`
func (e *codeHostStore) Count(ctx context.Context, opts ListCodeHostsOpts) (int32, error) {
q := countCodeHostsQuery(opts)
count, _, err := basestore.ScanFirstInt(e.Query(ctx, q))
return int32(count), err
}
func countCodeHostsQuery(opts ListCodeHostsOpts) *sqlf.Query {
return sqlf.Sprintf(countCodeHostsQueryFmtstr, listCodeHostsWhereQuery(opts))
}
const countCodeHostsQueryFmtstr = `
SELECT
count(*)
FROM
code_hosts
WHERE
%s
`
func listCodeHostsWhereQuery(opts ListCodeHostsOpts) *sqlf.Query {
conds := []*sqlf.Query{}
if !opts.IncludeDeleted {
// Don't show code hosts for which all external services are soft-deleted or hard-deleted.
conds = append(conds, sqlf.Sprintf("(EXISTS (SELECT 1 FROM external_services WHERE external_services.code_host_id = code_hosts.id AND deleted_at IS NULL))"))
}
if opts.ID > 0 {
conds = append(conds, sqlf.Sprintf("code_hosts.id = %s", opts.ID))
}
if opts.URL != "" {
conds = append(conds, sqlf.Sprintf("code_hosts.url = %s", opts.URL))
}
if opts.Cursor > 0 {
conds = append(conds, sqlf.Sprintf("code_hosts.id >= %s", opts.Cursor))
}
if opts.Search != "" {
conds = append(conds, sqlf.Sprintf("(code_hosts.kind ILIKE %s OR code_hosts.url ILIKE %s)", "%"+opts.Search+"%", "%"+opts.Search+"%"))
}
if len(conds) == 0 {
return sqlf.Sprintf("TRUE")
}
return sqlf.Join(conds, "AND")
}
func scanCodeHosts(s dbutil.Scanner) (*types.CodeHost, error) {
var ch types.CodeHost
err := scan(s, &ch)
if err != nil {
return &types.CodeHost{}, err
}
return &ch, nil
}
func scan(s dbutil.Scanner, ch *types.CodeHost) error {
return s.Scan(
&ch.ID,
&ch.Kind,
&ch.URL,
&ch.APIRateLimitQuota,
&ch.APIRateLimitIntervalSeconds,
&ch.GitRateLimitQuota,
&ch.GitRateLimitIntervalSeconds,
&dbutil.NullTime{Time: &ch.CreatedAt},
&dbutil.NullTime{Time: &ch.UpdatedAt},
)
}