| 1 | // Code generated by sqlc. DO NOT EDIT. |
| 2 | // versions: |
| 3 | // sqlc v1.31.1 |
| 4 | // source: orgs.sql |
| 5 | |
| 6 | package orgsdb |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | |
| 11 | "github.com/jackc/pgx/v5/pgtype" |
| 12 | ) |
| 13 | |
| 14 | const createOrg = `-- name: CreateOrg :one |
| 15 | |
| 16 | |
| 17 | INSERT INTO orgs (slug, display_name, description, billing_email, created_by_user_id) |
| 18 | VALUES ($1, $2, $3, $4, $5::bigint) |
| 19 | RETURNING id, slug, display_name, description, avatar_object_key, location, website, billing_email, plan, allow_member_repo_create, created_by_user_id, suspended_at, suspended_reason, deleted_at, created_at, updated_at |
| 20 | ` |
| 21 | |
| 22 | type CreateOrgParams struct { |
| 23 | Slug string |
| 24 | DisplayName string |
| 25 | Description string |
| 26 | BillingEmail string |
| 27 | CreatedByUserID pgtype.Int8 |
| 28 | } |
| 29 | |
| 30 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 31 | // ─── orgs ────────────────────────────────────────────────────────── |
| 32 | // Inserts a new org. The trigger on `orgs` populates `principals` for |
| 33 | // /{slug} resolution; the caller adds the creator as owner in the same |
| 34 | // tx (separate query so the orchestrator owns ordering). |
| 35 | func (q *Queries) CreateOrg(ctx context.Context, db DBTX, arg CreateOrgParams) (Org, error) { |
| 36 | row := db.QueryRow(ctx, createOrg, |
| 37 | arg.Slug, |
| 38 | arg.DisplayName, |
| 39 | arg.Description, |
| 40 | arg.BillingEmail, |
| 41 | arg.CreatedByUserID, |
| 42 | ) |
| 43 | var i Org |
| 44 | err := row.Scan( |
| 45 | &i.ID, |
| 46 | &i.Slug, |
| 47 | &i.DisplayName, |
| 48 | &i.Description, |
| 49 | &i.AvatarObjectKey, |
| 50 | &i.Location, |
| 51 | &i.Website, |
| 52 | &i.BillingEmail, |
| 53 | &i.Plan, |
| 54 | &i.AllowMemberRepoCreate, |
| 55 | &i.CreatedByUserID, |
| 56 | &i.SuspendedAt, |
| 57 | &i.SuspendedReason, |
| 58 | &i.DeletedAt, |
| 59 | &i.CreatedAt, |
| 60 | &i.UpdatedAt, |
| 61 | ) |
| 62 | return i, err |
| 63 | } |
| 64 | |
| 65 | const getOrgByID = `-- name: GetOrgByID :one |
| 66 | SELECT id, slug, display_name, description, avatar_object_key, location, website, billing_email, plan, allow_member_repo_create, created_by_user_id, suspended_at, suspended_reason, deleted_at, created_at, updated_at FROM orgs WHERE id = $1 |
| 67 | ` |
| 68 | |
| 69 | func (q *Queries) GetOrgByID(ctx context.Context, db DBTX, id int64) (Org, error) { |
| 70 | row := db.QueryRow(ctx, getOrgByID, id) |
| 71 | var i Org |
| 72 | err := row.Scan( |
| 73 | &i.ID, |
| 74 | &i.Slug, |
| 75 | &i.DisplayName, |
| 76 | &i.Description, |
| 77 | &i.AvatarObjectKey, |
| 78 | &i.Location, |
| 79 | &i.Website, |
| 80 | &i.BillingEmail, |
| 81 | &i.Plan, |
| 82 | &i.AllowMemberRepoCreate, |
| 83 | &i.CreatedByUserID, |
| 84 | &i.SuspendedAt, |
| 85 | &i.SuspendedReason, |
| 86 | &i.DeletedAt, |
| 87 | &i.CreatedAt, |
| 88 | &i.UpdatedAt, |
| 89 | ) |
| 90 | return i, err |
| 91 | } |
| 92 | |
| 93 | const getOrgBySlug = `-- name: GetOrgBySlug :one |
| 94 | SELECT id, slug, display_name, description, avatar_object_key, location, website, billing_email, plan, allow_member_repo_create, created_by_user_id, suspended_at, suspended_reason, deleted_at, created_at, updated_at FROM orgs |
| 95 | WHERE slug = $1 |
| 96 | AND deleted_at IS NULL |
| 97 | ` |
| 98 | |
| 99 | // Slug is citext so the comparison is case-insensitive. Soft-deleted |
| 100 | // orgs are filtered out so a slug freed by deletion is invisible to |
| 101 | // normal lookups (the resolver still sees the row via the deleted_at |
| 102 | // column for grace-period restore). |
| 103 | func (q *Queries) GetOrgBySlug(ctx context.Context, db DBTX, slug string) (Org, error) { |
| 104 | row := db.QueryRow(ctx, getOrgBySlug, slug) |
| 105 | var i Org |
| 106 | err := row.Scan( |
| 107 | &i.ID, |
| 108 | &i.Slug, |
| 109 | &i.DisplayName, |
| 110 | &i.Description, |
| 111 | &i.AvatarObjectKey, |
| 112 | &i.Location, |
| 113 | &i.Website, |
| 114 | &i.BillingEmail, |
| 115 | &i.Plan, |
| 116 | &i.AllowMemberRepoCreate, |
| 117 | &i.CreatedByUserID, |
| 118 | &i.SuspendedAt, |
| 119 | &i.SuspendedReason, |
| 120 | &i.DeletedAt, |
| 121 | &i.CreatedAt, |
| 122 | &i.UpdatedAt, |
| 123 | ) |
| 124 | return i, err |
| 125 | } |
| 126 | |
| 127 | const getOrgBySlugIncludingDeleted = `-- name: GetOrgBySlugIncludingDeleted :one |
| 128 | SELECT id, slug, display_name, description, avatar_object_key, location, website, billing_email, plan, allow_member_repo_create, created_by_user_id, suspended_at, suspended_reason, deleted_at, created_at, updated_at FROM orgs WHERE slug = $1 |
| 129 | ` |
| 130 | |
| 131 | func (q *Queries) GetOrgBySlugIncludingDeleted(ctx context.Context, db DBTX, slug string) (Org, error) { |
| 132 | row := db.QueryRow(ctx, getOrgBySlugIncludingDeleted, slug) |
| 133 | var i Org |
| 134 | err := row.Scan( |
| 135 | &i.ID, |
| 136 | &i.Slug, |
| 137 | &i.DisplayName, |
| 138 | &i.Description, |
| 139 | &i.AvatarObjectKey, |
| 140 | &i.Location, |
| 141 | &i.Website, |
| 142 | &i.BillingEmail, |
| 143 | &i.Plan, |
| 144 | &i.AllowMemberRepoCreate, |
| 145 | &i.CreatedByUserID, |
| 146 | &i.SuspendedAt, |
| 147 | &i.SuspendedReason, |
| 148 | &i.DeletedAt, |
| 149 | &i.CreatedAt, |
| 150 | &i.UpdatedAt, |
| 151 | ) |
| 152 | return i, err |
| 153 | } |
| 154 | |
| 155 | const hardDeleteOrgRow = `-- name: HardDeleteOrgRow :exec |
| 156 | DELETE FROM orgs WHERE id = $1 |
| 157 | ` |
| 158 | |
| 159 | // Final row removal after the cascade finished. The principals |
| 160 | // trigger drops the matching principals row in the same tx. |
| 161 | func (q *Queries) HardDeleteOrgRow(ctx context.Context, db DBTX, id int64) error { |
| 162 | _, err := db.Exec(ctx, hardDeleteOrgRow, id) |
| 163 | return err |
| 164 | } |
| 165 | |
| 166 | const listOrgIDsPastSoftDeleteGrace = `-- name: ListOrgIDsPastSoftDeleteGrace :many |
| 167 | SELECT id FROM orgs |
| 168 | WHERE deleted_at IS NOT NULL |
| 169 | AND deleted_at < (now() - interval '14 days') |
| 170 | ` |
| 171 | |
| 172 | // Sweep input for the lifecycle worker: every soft-deleted org whose |
| 173 | // 14-day grace window has elapsed. The interval is intentionally a |
| 174 | // DB literal (not a parameter) so the policy lives next to the data. |
| 175 | func (q *Queries) ListOrgIDsPastSoftDeleteGrace(ctx context.Context, db DBTX) ([]int64, error) { |
| 176 | rows, err := db.Query(ctx, listOrgIDsPastSoftDeleteGrace) |
| 177 | if err != nil { |
| 178 | return nil, err |
| 179 | } |
| 180 | defer rows.Close() |
| 181 | items := []int64{} |
| 182 | for rows.Next() { |
| 183 | var id int64 |
| 184 | if err := rows.Scan(&id); err != nil { |
| 185 | return nil, err |
| 186 | } |
| 187 | items = append(items, id) |
| 188 | } |
| 189 | if err := rows.Err(); err != nil { |
| 190 | return nil, err |
| 191 | } |
| 192 | return items, nil |
| 193 | } |
| 194 | |
| 195 | const listOrgRepoIDs = `-- name: ListOrgRepoIDs :many |
| 196 | SELECT id FROM repos WHERE owner_org_id = $1 |
| 197 | ` |
| 198 | |
| 199 | // All repo IDs (including soft-deleted) belonging to an org. Used by |
| 200 | // the org hard-delete cascade to fan out per-repo destruction. |
| 201 | func (q *Queries) ListOrgRepoIDs(ctx context.Context, db DBTX, ownerOrgID pgtype.Int8) ([]int64, error) { |
| 202 | rows, err := db.Query(ctx, listOrgRepoIDs, ownerOrgID) |
| 203 | if err != nil { |
| 204 | return nil, err |
| 205 | } |
| 206 | defer rows.Close() |
| 207 | items := []int64{} |
| 208 | for rows.Next() { |
| 209 | var id int64 |
| 210 | if err := rows.Scan(&id); err != nil { |
| 211 | return nil, err |
| 212 | } |
| 213 | items = append(items, id) |
| 214 | } |
| 215 | if err := rows.Err(); err != nil { |
| 216 | return nil, err |
| 217 | } |
| 218 | return items, nil |
| 219 | } |
| 220 | |
| 221 | const resolvePrincipal = `-- name: ResolvePrincipal :one |
| 222 | |
| 223 | SELECT slug, kind, id FROM principals WHERE slug = $1 |
| 224 | ` |
| 225 | |
| 226 | // ─── principals (read-only from this domain) ─────────────────────── |
| 227 | // Single-query /{slug} resolver. Returns the (kind, id) tuple that |
| 228 | // /{slug}/* routes use to dispatch to the user-profile or org-profile |
| 229 | // handler. Both kinds share the slug PK so collisions are |
| 230 | // impossible at the DB layer. |
| 231 | func (q *Queries) ResolvePrincipal(ctx context.Context, db DBTX, slug string) (Principal, error) { |
| 232 | row := db.QueryRow(ctx, resolvePrincipal, slug) |
| 233 | var i Principal |
| 234 | err := row.Scan(&i.Slug, &i.Kind, &i.ID) |
| 235 | return i, err |
| 236 | } |
| 237 | |
| 238 | const restoreOrg = `-- name: RestoreOrg :exec |
| 239 | UPDATE orgs SET deleted_at = NULL, updated_at = now() WHERE id = $1 |
| 240 | ` |
| 241 | |
| 242 | func (q *Queries) RestoreOrg(ctx context.Context, db DBTX, id int64) error { |
| 243 | _, err := db.Exec(ctx, restoreOrg, id) |
| 244 | return err |
| 245 | } |
| 246 | |
| 247 | const setOrgAllowMemberRepoCreate = `-- name: SetOrgAllowMemberRepoCreate :exec |
| 248 | UPDATE orgs SET allow_member_repo_create = $2, updated_at = now() WHERE id = $1 |
| 249 | ` |
| 250 | |
| 251 | type SetOrgAllowMemberRepoCreateParams struct { |
| 252 | ID int64 |
| 253 | AllowMemberRepoCreate bool |
| 254 | } |
| 255 | |
| 256 | func (q *Queries) SetOrgAllowMemberRepoCreate(ctx context.Context, db DBTX, arg SetOrgAllowMemberRepoCreateParams) error { |
| 257 | _, err := db.Exec(ctx, setOrgAllowMemberRepoCreate, arg.ID, arg.AllowMemberRepoCreate) |
| 258 | return err |
| 259 | } |
| 260 | |
| 261 | const setOrgAvatarKey = `-- name: SetOrgAvatarKey :exec |
| 262 | UPDATE orgs SET avatar_object_key = $2, updated_at = now() WHERE id = $1 |
| 263 | ` |
| 264 | |
| 265 | type SetOrgAvatarKeyParams struct { |
| 266 | ID int64 |
| 267 | AvatarObjectKey pgtype.Text |
| 268 | } |
| 269 | |
| 270 | func (q *Queries) SetOrgAvatarKey(ctx context.Context, db DBTX, arg SetOrgAvatarKeyParams) error { |
| 271 | _, err := db.Exec(ctx, setOrgAvatarKey, arg.ID, arg.AvatarObjectKey) |
| 272 | return err |
| 273 | } |
| 274 | |
| 275 | const setOrgSuspended = `-- name: SetOrgSuspended :exec |
| 276 | UPDATE orgs |
| 277 | SET suspended_at = CASE WHEN $2::boolean THEN now() ELSE NULL END, |
| 278 | suspended_reason = CASE WHEN $2::boolean THEN $3 ELSE NULL END, |
| 279 | updated_at = now() |
| 280 | WHERE id = $1 |
| 281 | ` |
| 282 | |
| 283 | type SetOrgSuspendedParams struct { |
| 284 | ID int64 |
| 285 | Column2 bool |
| 286 | SuspendedReason pgtype.Text |
| 287 | } |
| 288 | |
| 289 | func (q *Queries) SetOrgSuspended(ctx context.Context, db DBTX, arg SetOrgSuspendedParams) error { |
| 290 | _, err := db.Exec(ctx, setOrgSuspended, arg.ID, arg.Column2, arg.SuspendedReason) |
| 291 | return err |
| 292 | } |
| 293 | |
| 294 | const softDeleteOrg = `-- name: SoftDeleteOrg :exec |
| 295 | UPDATE orgs SET deleted_at = now(), updated_at = now() WHERE id = $1 |
| 296 | ` |
| 297 | |
| 298 | func (q *Queries) SoftDeleteOrg(ctx context.Context, db DBTX, id int64) error { |
| 299 | _, err := db.Exec(ctx, softDeleteOrg, id) |
| 300 | return err |
| 301 | } |
| 302 | |
| 303 | const updateOrgProfile = `-- name: UpdateOrgProfile :exec |
| 304 | UPDATE orgs |
| 305 | SET display_name = $2, |
| 306 | description = $3, |
| 307 | location = $4, |
| 308 | website = $5, |
| 309 | billing_email = $6, |
| 310 | updated_at = now() |
| 311 | WHERE id = $1 |
| 312 | ` |
| 313 | |
| 314 | type UpdateOrgProfileParams struct { |
| 315 | ID int64 |
| 316 | DisplayName string |
| 317 | Description string |
| 318 | Location string |
| 319 | Website string |
| 320 | BillingEmail string |
| 321 | } |
| 322 | |
| 323 | func (q *Queries) UpdateOrgProfile(ctx context.Context, db DBTX, arg UpdateOrgProfileParams) error { |
| 324 | _, err := db.Exec(ctx, updateOrgProfile, |
| 325 | arg.ID, |
| 326 | arg.DisplayName, |
| 327 | arg.Description, |
| 328 | arg.Location, |
| 329 | arg.Website, |
| 330 | arg.BillingEmail, |
| 331 | ) |
| 332 | return err |
| 333 | } |
| 334 |