@@ -34,7 +34,7 @@ INSERT INTO repos ( |
| 34 | 34 | RETURNING id, owner_user_id, owner_org_id, name, description, visibility, |
| 35 | 35 | default_branch, is_archived, archived_at, deleted_at, |
| 36 | 36 | disk_used_bytes, fork_of_repo_id, license_key, primary_language, |
| 37 | | - has_issues, has_pulls, created_at, updated_at |
| 37 | + has_issues, has_pulls, created_at, updated_at, default_branch_oid |
| 38 | 38 | ` |
| 39 | 39 | |
| 40 | 40 | type CreateRepoParams struct { |
@@ -80,6 +80,7 @@ func (q *Queries) CreateRepo(ctx context.Context, db DBTX, arg CreateRepoParams) |
| 80 | 80 | &i.HasPulls, |
| 81 | 81 | &i.CreatedAt, |
| 82 | 82 | &i.UpdatedAt, |
| 83 | + &i.DefaultBranchOid, |
| 83 | 84 | ) |
| 84 | 85 | return i, err |
| 85 | 86 | } |
@@ -103,11 +104,47 @@ func (q *Queries) ExistsRepoForOwnerUser(ctx context.Context, db DBTX, arg Exist |
| 103 | 104 | return exists, err |
| 104 | 105 | } |
| 105 | 106 | |
| 107 | +const getRepoByID = `-- name: GetRepoByID :one |
| 108 | +SELECT id, owner_user_id, owner_org_id, name, description, visibility, |
| 109 | + default_branch, is_archived, archived_at, deleted_at, |
| 110 | + disk_used_bytes, fork_of_repo_id, license_key, primary_language, |
| 111 | + has_issues, has_pulls, created_at, updated_at, default_branch_oid |
| 112 | +FROM repos |
| 113 | +WHERE id = $1 |
| 114 | +` |
| 115 | + |
| 116 | +func (q *Queries) GetRepoByID(ctx context.Context, db DBTX, id int64) (Repo, error) { |
| 117 | + row := db.QueryRow(ctx, getRepoByID, id) |
| 118 | + var i Repo |
| 119 | + err := row.Scan( |
| 120 | + &i.ID, |
| 121 | + &i.OwnerUserID, |
| 122 | + &i.OwnerOrgID, |
| 123 | + &i.Name, |
| 124 | + &i.Description, |
| 125 | + &i.Visibility, |
| 126 | + &i.DefaultBranch, |
| 127 | + &i.IsArchived, |
| 128 | + &i.ArchivedAt, |
| 129 | + &i.DeletedAt, |
| 130 | + &i.DiskUsedBytes, |
| 131 | + &i.ForkOfRepoID, |
| 132 | + &i.LicenseKey, |
| 133 | + &i.PrimaryLanguage, |
| 134 | + &i.HasIssues, |
| 135 | + &i.HasPulls, |
| 136 | + &i.CreatedAt, |
| 137 | + &i.UpdatedAt, |
| 138 | + &i.DefaultBranchOid, |
| 139 | + ) |
| 140 | + return i, err |
| 141 | +} |
| 142 | + |
| 106 | 143 | const getRepoByOwnerUserAndName = `-- name: GetRepoByOwnerUserAndName :one |
| 107 | 144 | SELECT id, owner_user_id, owner_org_id, name, description, visibility, |
| 108 | 145 | default_branch, is_archived, archived_at, deleted_at, |
| 109 | 146 | disk_used_bytes, fork_of_repo_id, license_key, primary_language, |
| 110 | | - has_issues, has_pulls, created_at, updated_at |
| 147 | + has_issues, has_pulls, created_at, updated_at, default_branch_oid |
| 111 | 148 | FROM repos |
| 112 | 149 | WHERE owner_user_id = $1 AND name = $2 AND deleted_at IS NULL |
| 113 | 150 | ` |
@@ -139,15 +176,77 @@ func (q *Queries) GetRepoByOwnerUserAndName(ctx context.Context, db DBTX, arg Ge |
| 139 | 176 | &i.HasPulls, |
| 140 | 177 | &i.CreatedAt, |
| 141 | 178 | &i.UpdatedAt, |
| 179 | + &i.DefaultBranchOid, |
| 142 | 180 | ) |
| 143 | 181 | return i, err |
| 144 | 182 | } |
| 145 | 183 | |
| 184 | +const getRepoOwnerUsernameByID = `-- name: GetRepoOwnerUsernameByID :one |
| 185 | +SELECT u.username AS owner_username, r.name AS repo_name |
| 186 | +FROM repos r |
| 187 | +JOIN users u ON u.id = r.owner_user_id |
| 188 | +WHERE r.id = $1 |
| 189 | +` |
| 190 | + |
| 191 | +type GetRepoOwnerUsernameByIDRow struct { |
| 192 | + OwnerUsername string |
| 193 | + RepoName string |
| 194 | +} |
| 195 | + |
| 196 | +// Returns the owner_username for a repo. Used by size-recalc and other |
| 197 | +// jobs that need to derive the bare-repo on-disk path without round- |
| 198 | +// tripping through the full user row. |
| 199 | +func (q *Queries) GetRepoOwnerUsernameByID(ctx context.Context, db DBTX, id int64) (GetRepoOwnerUsernameByIDRow, error) { |
| 200 | + row := db.QueryRow(ctx, getRepoOwnerUsernameByID, id) |
| 201 | + var i GetRepoOwnerUsernameByIDRow |
| 202 | + err := row.Scan(&i.OwnerUsername, &i.RepoName) |
| 203 | + return i, err |
| 204 | +} |
| 205 | + |
| 206 | +const listAllRepoFullNames = `-- name: ListAllRepoFullNames :many |
| 207 | +SELECT |
| 208 | + r.id, |
| 209 | + r.name, |
| 210 | + u.username AS owner_username |
| 211 | +FROM repos r |
| 212 | +JOIN users u ON u.id = r.owner_user_id |
| 213 | +WHERE r.deleted_at IS NULL |
| 214 | +ORDER BY r.id |
| 215 | +` |
| 216 | + |
| 217 | +type ListAllRepoFullNamesRow struct { |
| 218 | + ID int64 |
| 219 | + Name string |
| 220 | + OwnerUsername string |
| 221 | +} |
| 222 | + |
| 223 | +// Used by `shithubd hooks reinstall --all` to enumerate every active |
| 224 | +// bare repo on disk and re-link its hooks. |
| 225 | +func (q *Queries) ListAllRepoFullNames(ctx context.Context, db DBTX) ([]ListAllRepoFullNamesRow, error) { |
| 226 | + rows, err := db.Query(ctx, listAllRepoFullNames) |
| 227 | + if err != nil { |
| 228 | + return nil, err |
| 229 | + } |
| 230 | + defer rows.Close() |
| 231 | + items := []ListAllRepoFullNamesRow{} |
| 232 | + for rows.Next() { |
| 233 | + var i ListAllRepoFullNamesRow |
| 234 | + if err := rows.Scan(&i.ID, &i.Name, &i.OwnerUsername); err != nil { |
| 235 | + return nil, err |
| 236 | + } |
| 237 | + items = append(items, i) |
| 238 | + } |
| 239 | + if err := rows.Err(); err != nil { |
| 240 | + return nil, err |
| 241 | + } |
| 242 | + return items, nil |
| 243 | +} |
| 244 | + |
| 146 | 245 | const listReposForOwnerUser = `-- name: ListReposForOwnerUser :many |
| 147 | 246 | SELECT id, owner_user_id, owner_org_id, name, description, visibility, |
| 148 | 247 | default_branch, is_archived, archived_at, deleted_at, |
| 149 | 248 | disk_used_bytes, fork_of_repo_id, license_key, primary_language, |
| 150 | | - has_issues, has_pulls, created_at, updated_at |
| 249 | + has_issues, has_pulls, created_at, updated_at, default_branch_oid |
| 151 | 250 | FROM repos |
| 152 | 251 | WHERE owner_user_id = $1 AND deleted_at IS NULL |
| 153 | 252 | ORDER BY updated_at DESC |
@@ -181,6 +280,7 @@ func (q *Queries) ListReposForOwnerUser(ctx context.Context, db DBTX, ownerUserI |
| 181 | 280 | &i.HasPulls, |
| 182 | 281 | &i.CreatedAt, |
| 183 | 282 | &i.UpdatedAt, |
| 283 | + &i.DefaultBranchOid, |
| 184 | 284 | ); err != nil { |
| 185 | 285 | return nil, err |
| 186 | 286 | } |
@@ -201,6 +301,24 @@ func (q *Queries) SoftDeleteRepo(ctx context.Context, db DBTX, id int64) error { |
| 201 | 301 | return err |
| 202 | 302 | } |
| 203 | 303 | |
| 304 | +const updateRepoDefaultBranchOID = `-- name: UpdateRepoDefaultBranchOID :exec |
| 305 | +UPDATE repos SET default_branch_oid = $2::text WHERE id = $1 |
| 306 | +` |
| 307 | + |
| 308 | +type UpdateRepoDefaultBranchOIDParams struct { |
| 309 | + ID int64 |
| 310 | + DefaultBranchOid pgtype.Text |
| 311 | +} |
| 312 | + |
| 313 | +// Set when push:process detects a commit on the repo's default branch. |
| 314 | +// Pass NULL to clear (e.g. when the branch is force-deleted in a future |
| 315 | +// sprint). The repo home view reads this to decide between empty and |
| 316 | +// populated layouts. |
| 317 | +func (q *Queries) UpdateRepoDefaultBranchOID(ctx context.Context, db DBTX, arg UpdateRepoDefaultBranchOIDParams) error { |
| 318 | + _, err := db.Exec(ctx, updateRepoDefaultBranchOID, arg.ID, arg.DefaultBranchOid) |
| 319 | + return err |
| 320 | +} |
| 321 | + |
| 204 | 322 | const updateRepoDiskUsed = `-- name: UpdateRepoDiskUsed :exec |
| 205 | 323 | UPDATE repos SET disk_used_bytes = $2 WHERE id = $1 |
| 206 | 324 | ` |