Go · 15996 bytes Raw Blame History
1 // Code generated by sqlc. DO NOT EDIT.
2 // versions:
3 // sqlc v1.31.1
4 // source: repos.sql
5
6 package reposdb
7
8 import (
9 "context"
10
11 "github.com/jackc/pgx/v5/pgtype"
12 )
13
14 const countForksOfRepo = `-- name: CountForksOfRepo :one
15 SELECT count(*) FROM repos
16 WHERE fork_of_repo_id = $1 AND deleted_at IS NULL
17 `
18
19 func (q *Queries) CountForksOfRepo(ctx context.Context, db DBTX, forkOfRepoID pgtype.Int8) (int64, error) {
20 row := db.QueryRow(ctx, countForksOfRepo, forkOfRepoID)
21 var count int64
22 err := row.Scan(&count)
23 return count, err
24 }
25
26 const countReposForOwnerUser = `-- name: CountReposForOwnerUser :one
27 SELECT count(*) FROM repos
28 WHERE owner_user_id = $1 AND deleted_at IS NULL
29 `
30
31 func (q *Queries) CountReposForOwnerUser(ctx context.Context, db DBTX, ownerUserID pgtype.Int8) (int64, error) {
32 row := db.QueryRow(ctx, countReposForOwnerUser, ownerUserID)
33 var count int64
34 err := row.Scan(&count)
35 return count, err
36 }
37
38 const createForkRepo = `-- name: CreateForkRepo :one
39
40 INSERT INTO repos (
41 owner_user_id, owner_org_id, name, description, visibility,
42 default_branch, fork_of_repo_id, init_status
43 ) VALUES (
44 $1, $2, $3, $4, $5, $6, $7, 'init_pending'
45 )
46 RETURNING id, owner_user_id, owner_org_id, name, description, visibility,
47 default_branch, is_archived, archived_at, deleted_at,
48 disk_used_bytes, fork_of_repo_id, license_key, primary_language,
49 has_issues, has_pulls, created_at, updated_at, default_branch_oid,
50 allow_squash_merge, allow_rebase_merge, allow_merge_commit, default_merge_method,
51 star_count, watcher_count, fork_count, init_status
52 `
53
54 type CreateForkRepoParams struct {
55 OwnerUserID pgtype.Int8
56 OwnerOrgID pgtype.Int8
57 Name string
58 Description string
59 Visibility RepoVisibility
60 DefaultBranch string
61 ForkOfRepoID pgtype.Int8
62 }
63
64 // ─── S27 forks ─────────────────────────────────────────────────────
65 // Insert a fork shell. Distinct from CreateRepo because forks set
66 // `fork_of_repo_id` (which fires the fork_count trigger) and start
67 // at init_status='init_pending' so the worker job can flip them to
68 // 'initialized' once `git clone --bare --shared` finishes.
69 func (q *Queries) CreateForkRepo(ctx context.Context, db DBTX, arg CreateForkRepoParams) (Repo, error) {
70 row := db.QueryRow(ctx, createForkRepo,
71 arg.OwnerUserID,
72 arg.OwnerOrgID,
73 arg.Name,
74 arg.Description,
75 arg.Visibility,
76 arg.DefaultBranch,
77 arg.ForkOfRepoID,
78 )
79 var i Repo
80 err := row.Scan(
81 &i.ID,
82 &i.OwnerUserID,
83 &i.OwnerOrgID,
84 &i.Name,
85 &i.Description,
86 &i.Visibility,
87 &i.DefaultBranch,
88 &i.IsArchived,
89 &i.ArchivedAt,
90 &i.DeletedAt,
91 &i.DiskUsedBytes,
92 &i.ForkOfRepoID,
93 &i.LicenseKey,
94 &i.PrimaryLanguage,
95 &i.HasIssues,
96 &i.HasPulls,
97 &i.CreatedAt,
98 &i.UpdatedAt,
99 &i.DefaultBranchOid,
100 &i.AllowSquashMerge,
101 &i.AllowRebaseMerge,
102 &i.AllowMergeCommit,
103 &i.DefaultMergeMethod,
104 &i.StarCount,
105 &i.WatcherCount,
106 &i.ForkCount,
107 &i.InitStatus,
108 )
109 return i, err
110 }
111
112 const createRepo = `-- name: CreateRepo :one
113
114 INSERT INTO repos (
115 owner_user_id, owner_org_id, name, description, visibility,
116 default_branch, license_key, primary_language
117 ) VALUES (
118 $1, $2, $3, $4, $5, $6, $7, $8
119 )
120 RETURNING id, owner_user_id, owner_org_id, name, description, visibility,
121 default_branch, is_archived, archived_at, deleted_at,
122 disk_used_bytes, fork_of_repo_id, license_key, primary_language,
123 has_issues, has_pulls, created_at, updated_at, default_branch_oid,
124 allow_squash_merge, allow_rebase_merge, allow_merge_commit, default_merge_method,
125 star_count, watcher_count, fork_count, init_status
126 `
127
128 type CreateRepoParams struct {
129 OwnerUserID pgtype.Int8
130 OwnerOrgID pgtype.Int8
131 Name string
132 Description string
133 Visibility RepoVisibility
134 DefaultBranch string
135 LicenseKey pgtype.Text
136 PrimaryLanguage pgtype.Text
137 }
138
139 // SPDX-License-Identifier: AGPL-3.0-or-later
140 func (q *Queries) CreateRepo(ctx context.Context, db DBTX, arg CreateRepoParams) (Repo, error) {
141 row := db.QueryRow(ctx, createRepo,
142 arg.OwnerUserID,
143 arg.OwnerOrgID,
144 arg.Name,
145 arg.Description,
146 arg.Visibility,
147 arg.DefaultBranch,
148 arg.LicenseKey,
149 arg.PrimaryLanguage,
150 )
151 var i Repo
152 err := row.Scan(
153 &i.ID,
154 &i.OwnerUserID,
155 &i.OwnerOrgID,
156 &i.Name,
157 &i.Description,
158 &i.Visibility,
159 &i.DefaultBranch,
160 &i.IsArchived,
161 &i.ArchivedAt,
162 &i.DeletedAt,
163 &i.DiskUsedBytes,
164 &i.ForkOfRepoID,
165 &i.LicenseKey,
166 &i.PrimaryLanguage,
167 &i.HasIssues,
168 &i.HasPulls,
169 &i.CreatedAt,
170 &i.UpdatedAt,
171 &i.DefaultBranchOid,
172 &i.AllowSquashMerge,
173 &i.AllowRebaseMerge,
174 &i.AllowMergeCommit,
175 &i.DefaultMergeMethod,
176 &i.StarCount,
177 &i.WatcherCount,
178 &i.ForkCount,
179 &i.InitStatus,
180 )
181 return i, err
182 }
183
184 const existsRepoForOwnerUser = `-- name: ExistsRepoForOwnerUser :one
185 SELECT EXISTS(
186 SELECT 1 FROM repos
187 WHERE owner_user_id = $1 AND name = $2 AND deleted_at IS NULL
188 )
189 `
190
191 type ExistsRepoForOwnerUserParams struct {
192 OwnerUserID pgtype.Int8
193 Name string
194 }
195
196 func (q *Queries) ExistsRepoForOwnerUser(ctx context.Context, db DBTX, arg ExistsRepoForOwnerUserParams) (bool, error) {
197 row := db.QueryRow(ctx, existsRepoForOwnerUser, arg.OwnerUserID, arg.Name)
198 var exists bool
199 err := row.Scan(&exists)
200 return exists, err
201 }
202
203 const getRepoByID = `-- name: GetRepoByID :one
204 SELECT id, owner_user_id, owner_org_id, name, description, visibility,
205 default_branch, is_archived, archived_at, deleted_at,
206 disk_used_bytes, fork_of_repo_id, license_key, primary_language,
207 has_issues, has_pulls, created_at, updated_at, default_branch_oid,
208 allow_squash_merge, allow_rebase_merge, allow_merge_commit, default_merge_method,
209 star_count, watcher_count, fork_count, init_status
210 FROM repos
211 WHERE id = $1
212 `
213
214 func (q *Queries) GetRepoByID(ctx context.Context, db DBTX, id int64) (Repo, error) {
215 row := db.QueryRow(ctx, getRepoByID, id)
216 var i Repo
217 err := row.Scan(
218 &i.ID,
219 &i.OwnerUserID,
220 &i.OwnerOrgID,
221 &i.Name,
222 &i.Description,
223 &i.Visibility,
224 &i.DefaultBranch,
225 &i.IsArchived,
226 &i.ArchivedAt,
227 &i.DeletedAt,
228 &i.DiskUsedBytes,
229 &i.ForkOfRepoID,
230 &i.LicenseKey,
231 &i.PrimaryLanguage,
232 &i.HasIssues,
233 &i.HasPulls,
234 &i.CreatedAt,
235 &i.UpdatedAt,
236 &i.DefaultBranchOid,
237 &i.AllowSquashMerge,
238 &i.AllowRebaseMerge,
239 &i.AllowMergeCommit,
240 &i.DefaultMergeMethod,
241 &i.StarCount,
242 &i.WatcherCount,
243 &i.ForkCount,
244 &i.InitStatus,
245 )
246 return i, err
247 }
248
249 const getRepoByOwnerUserAndName = `-- name: GetRepoByOwnerUserAndName :one
250 SELECT id, owner_user_id, owner_org_id, name, description, visibility,
251 default_branch, is_archived, archived_at, deleted_at,
252 disk_used_bytes, fork_of_repo_id, license_key, primary_language,
253 has_issues, has_pulls, created_at, updated_at, default_branch_oid,
254 allow_squash_merge, allow_rebase_merge, allow_merge_commit, default_merge_method,
255 star_count, watcher_count, fork_count, init_status
256 FROM repos
257 WHERE owner_user_id = $1 AND name = $2 AND deleted_at IS NULL
258 `
259
260 type GetRepoByOwnerUserAndNameParams struct {
261 OwnerUserID pgtype.Int8
262 Name string
263 }
264
265 func (q *Queries) GetRepoByOwnerUserAndName(ctx context.Context, db DBTX, arg GetRepoByOwnerUserAndNameParams) (Repo, error) {
266 row := db.QueryRow(ctx, getRepoByOwnerUserAndName, arg.OwnerUserID, arg.Name)
267 var i Repo
268 err := row.Scan(
269 &i.ID,
270 &i.OwnerUserID,
271 &i.OwnerOrgID,
272 &i.Name,
273 &i.Description,
274 &i.Visibility,
275 &i.DefaultBranch,
276 &i.IsArchived,
277 &i.ArchivedAt,
278 &i.DeletedAt,
279 &i.DiskUsedBytes,
280 &i.ForkOfRepoID,
281 &i.LicenseKey,
282 &i.PrimaryLanguage,
283 &i.HasIssues,
284 &i.HasPulls,
285 &i.CreatedAt,
286 &i.UpdatedAt,
287 &i.DefaultBranchOid,
288 &i.AllowSquashMerge,
289 &i.AllowRebaseMerge,
290 &i.AllowMergeCommit,
291 &i.DefaultMergeMethod,
292 &i.StarCount,
293 &i.WatcherCount,
294 &i.ForkCount,
295 &i.InitStatus,
296 )
297 return i, err
298 }
299
300 const getRepoOwnerUsernameByID = `-- name: GetRepoOwnerUsernameByID :one
301 SELECT u.username AS owner_username, r.name AS repo_name
302 FROM repos r
303 JOIN users u ON u.id = r.owner_user_id
304 WHERE r.id = $1
305 `
306
307 type GetRepoOwnerUsernameByIDRow struct {
308 OwnerUsername string
309 RepoName string
310 }
311
312 // Returns the owner_username for a repo. Used by size-recalc and other
313 // jobs that need to derive the bare-repo on-disk path without round-
314 // tripping through the full user row.
315 func (q *Queries) GetRepoOwnerUsernameByID(ctx context.Context, db DBTX, id int64) (GetRepoOwnerUsernameByIDRow, error) {
316 row := db.QueryRow(ctx, getRepoOwnerUsernameByID, id)
317 var i GetRepoOwnerUsernameByIDRow
318 err := row.Scan(&i.OwnerUsername, &i.RepoName)
319 return i, err
320 }
321
322 const listAllRepoFullNames = `-- name: ListAllRepoFullNames :many
323 SELECT
324 r.id,
325 r.name,
326 u.username AS owner_username
327 FROM repos r
328 JOIN users u ON u.id = r.owner_user_id
329 WHERE r.deleted_at IS NULL
330 ORDER BY r.id
331 `
332
333 type ListAllRepoFullNamesRow struct {
334 ID int64
335 Name string
336 OwnerUsername string
337 }
338
339 // Used by `shithubd hooks reinstall --all` to enumerate every active
340 // bare repo on disk and re-link its hooks.
341 func (q *Queries) ListAllRepoFullNames(ctx context.Context, db DBTX) ([]ListAllRepoFullNamesRow, error) {
342 rows, err := db.Query(ctx, listAllRepoFullNames)
343 if err != nil {
344 return nil, err
345 }
346 defer rows.Close()
347 items := []ListAllRepoFullNamesRow{}
348 for rows.Next() {
349 var i ListAllRepoFullNamesRow
350 if err := rows.Scan(&i.ID, &i.Name, &i.OwnerUsername); err != nil {
351 return nil, err
352 }
353 items = append(items, i)
354 }
355 if err := rows.Err(); err != nil {
356 return nil, err
357 }
358 return items, nil
359 }
360
361 const listForksOfRepo = `-- name: ListForksOfRepo :many
362 SELECT r.id, r.name, r.description, r.visibility, r.created_at,
363 r.star_count, r.fork_count, r.init_status,
364 u.username AS owner_username, u.display_name AS owner_display_name
365 FROM repos r
366 JOIN users u ON u.id = r.owner_user_id
367 WHERE r.fork_of_repo_id = $1
368 AND r.deleted_at IS NULL
369 ORDER BY r.created_at DESC
370 LIMIT $2 OFFSET $3
371 `
372
373 type ListForksOfRepoParams struct {
374 ForkOfRepoID pgtype.Int8
375 Limit int32
376 Offset int32
377 }
378
379 type ListForksOfRepoRow struct {
380 ID int64
381 Name string
382 Description string
383 Visibility RepoVisibility
384 CreatedAt pgtype.Timestamptz
385 StarCount int64
386 ForkCount int64
387 InitStatus RepoInitStatus
388 OwnerUsername string
389 OwnerDisplayName string
390 }
391
392 // Forks of a given source repo, paginated, recency-sorted. Joined
393 // with users for the owner display name. Excludes soft-deleted.
394 func (q *Queries) ListForksOfRepo(ctx context.Context, db DBTX, arg ListForksOfRepoParams) ([]ListForksOfRepoRow, error) {
395 rows, err := db.Query(ctx, listForksOfRepo, arg.ForkOfRepoID, arg.Limit, arg.Offset)
396 if err != nil {
397 return nil, err
398 }
399 defer rows.Close()
400 items := []ListForksOfRepoRow{}
401 for rows.Next() {
402 var i ListForksOfRepoRow
403 if err := rows.Scan(
404 &i.ID,
405 &i.Name,
406 &i.Description,
407 &i.Visibility,
408 &i.CreatedAt,
409 &i.StarCount,
410 &i.ForkCount,
411 &i.InitStatus,
412 &i.OwnerUsername,
413 &i.OwnerDisplayName,
414 ); err != nil {
415 return nil, err
416 }
417 items = append(items, i)
418 }
419 if err := rows.Err(); err != nil {
420 return nil, err
421 }
422 return items, nil
423 }
424
425 const listForksOfRepoForRepack = `-- name: ListForksOfRepoForRepack :many
426 SELECT r.id, r.name, u.username AS owner_username
427 FROM repos r
428 JOIN users u ON u.id = r.owner_user_id
429 WHERE r.fork_of_repo_id = $1
430 AND r.deleted_at IS NULL
431 `
432
433 type ListForksOfRepoForRepackRow struct {
434 ID int64
435 Name string
436 OwnerUsername string
437 }
438
439 // Used by S16's hard-delete cascade (S27 amendment): before deleting
440 // a source repo, every fork must `git repack -a -d --no-shared` so
441 // it has its own copy of the objects. Returns just enough to locate
442 // the bare repo on disk.
443 func (q *Queries) ListForksOfRepoForRepack(ctx context.Context, db DBTX, forkOfRepoID pgtype.Int8) ([]ListForksOfRepoForRepackRow, error) {
444 rows, err := db.Query(ctx, listForksOfRepoForRepack, forkOfRepoID)
445 if err != nil {
446 return nil, err
447 }
448 defer rows.Close()
449 items := []ListForksOfRepoForRepackRow{}
450 for rows.Next() {
451 var i ListForksOfRepoForRepackRow
452 if err := rows.Scan(&i.ID, &i.Name, &i.OwnerUsername); err != nil {
453 return nil, err
454 }
455 items = append(items, i)
456 }
457 if err := rows.Err(); err != nil {
458 return nil, err
459 }
460 return items, nil
461 }
462
463 const listReposForOwnerUser = `-- name: ListReposForOwnerUser :many
464 SELECT id, owner_user_id, owner_org_id, name, description, visibility,
465 default_branch, is_archived, archived_at, deleted_at,
466 disk_used_bytes, fork_of_repo_id, license_key, primary_language,
467 has_issues, has_pulls, created_at, updated_at, default_branch_oid,
468 allow_squash_merge, allow_rebase_merge, allow_merge_commit, default_merge_method,
469 star_count, watcher_count, fork_count, init_status
470 FROM repos
471 WHERE owner_user_id = $1 AND deleted_at IS NULL
472 ORDER BY updated_at DESC
473 `
474
475 func (q *Queries) ListReposForOwnerUser(ctx context.Context, db DBTX, ownerUserID pgtype.Int8) ([]Repo, error) {
476 rows, err := db.Query(ctx, listReposForOwnerUser, ownerUserID)
477 if err != nil {
478 return nil, err
479 }
480 defer rows.Close()
481 items := []Repo{}
482 for rows.Next() {
483 var i Repo
484 if err := rows.Scan(
485 &i.ID,
486 &i.OwnerUserID,
487 &i.OwnerOrgID,
488 &i.Name,
489 &i.Description,
490 &i.Visibility,
491 &i.DefaultBranch,
492 &i.IsArchived,
493 &i.ArchivedAt,
494 &i.DeletedAt,
495 &i.DiskUsedBytes,
496 &i.ForkOfRepoID,
497 &i.LicenseKey,
498 &i.PrimaryLanguage,
499 &i.HasIssues,
500 &i.HasPulls,
501 &i.CreatedAt,
502 &i.UpdatedAt,
503 &i.DefaultBranchOid,
504 &i.AllowSquashMerge,
505 &i.AllowRebaseMerge,
506 &i.AllowMergeCommit,
507 &i.DefaultMergeMethod,
508 &i.StarCount,
509 &i.WatcherCount,
510 &i.ForkCount,
511 &i.InitStatus,
512 ); err != nil {
513 return nil, err
514 }
515 items = append(items, i)
516 }
517 if err := rows.Err(); err != nil {
518 return nil, err
519 }
520 return items, nil
521 }
522
523 const setRepoInitStatus = `-- name: SetRepoInitStatus :exec
524 UPDATE repos SET init_status = $2 WHERE id = $1
525 `
526
527 type SetRepoInitStatusParams struct {
528 ID int64
529 InitStatus RepoInitStatus
530 }
531
532 // Promotes a fork from init_pending to initialized (or init_failed).
533 // The DB row is created up-front so the URL resolves immediately and
534 // the user sees a "preparing your fork" placeholder while the worker
535 // runs `git clone --bare --shared`.
536 func (q *Queries) SetRepoInitStatus(ctx context.Context, db DBTX, arg SetRepoInitStatusParams) error {
537 _, err := db.Exec(ctx, setRepoInitStatus, arg.ID, arg.InitStatus)
538 return err
539 }
540
541 const softDeleteRepo = `-- name: SoftDeleteRepo :exec
542 UPDATE repos SET deleted_at = now() WHERE id = $1
543 `
544
545 func (q *Queries) SoftDeleteRepo(ctx context.Context, db DBTX, id int64) error {
546 _, err := db.Exec(ctx, softDeleteRepo, id)
547 return err
548 }
549
550 const updateRepoDefaultBranchOID = `-- name: UpdateRepoDefaultBranchOID :exec
551 UPDATE repos SET default_branch_oid = $2::text WHERE id = $1
552 `
553
554 type UpdateRepoDefaultBranchOIDParams struct {
555 ID int64
556 DefaultBranchOid pgtype.Text
557 }
558
559 // Set when push:process detects a commit on the repo's default branch.
560 // Pass NULL to clear (e.g. when the branch is force-deleted in a future
561 // sprint). The repo home view reads this to decide between empty and
562 // populated layouts.
563 func (q *Queries) UpdateRepoDefaultBranchOID(ctx context.Context, db DBTX, arg UpdateRepoDefaultBranchOIDParams) error {
564 _, err := db.Exec(ctx, updateRepoDefaultBranchOID, arg.ID, arg.DefaultBranchOid)
565 return err
566 }
567
568 const updateRepoDiskUsed = `-- name: UpdateRepoDiskUsed :exec
569 UPDATE repos SET disk_used_bytes = $2 WHERE id = $1
570 `
571
572 type UpdateRepoDiskUsedParams struct {
573 ID int64
574 DiskUsedBytes int64
575 }
576
577 func (q *Queries) UpdateRepoDiskUsed(ctx context.Context, db DBTX, arg UpdateRepoDiskUsedParams) error {
578 _, err := db.Exec(ctx, updateRepoDiskUsed, arg.ID, arg.DiskUsedBytes)
579 return err
580 }
581