Go · 3162 bytes Raw Blame History
1 // Code generated by sqlc. DO NOT EDIT.
2 // versions:
3 // sqlc v1.31.1
4 // source: cursor.sql
5
6 package webhookdb
7
8 import (
9 "context"
10
11 "github.com/jackc/pgx/v5/pgtype"
12 )
13
14 const getRepoOwnerKindForFanout = `-- name: GetRepoOwnerKindForFanout :one
15 SELECT owner_user_id, owner_org_id
16 FROM repos
17 WHERE id = $1
18 `
19
20 type GetRepoOwnerKindForFanoutRow struct {
21 OwnerUserID pgtype.Int8
22 OwnerOrgID pgtype.Int8
23 }
24
25 // Resolve a repo's owner so the fanout knows which webhooks to fire.
26 // Returns owner_kind 'user' or 'org' so the matcher knows what bucket
27 // to look in. (We use 'user' as a literal string here; the webhook
28 // machinery itself only stores 'repo'/'org' as owners — repo-owned
29 // webhooks attach to the repo regardless of who owns the repo.)
30 func (q *Queries) GetRepoOwnerKindForFanout(ctx context.Context, db DBTX, id int64) (GetRepoOwnerKindForFanoutRow, error) {
31 row := db.QueryRow(ctx, getRepoOwnerKindForFanout, id)
32 var i GetRepoOwnerKindForFanoutRow
33 err := row.Scan(&i.OwnerUserID, &i.OwnerOrgID)
34 return i, err
35 }
36
37 const getWebhookCursor = `-- name: GetWebhookCursor :one
38
39 SELECT consumer, last_event_id, updated_at
40 FROM domain_events_processed
41 WHERE consumer = $1
42 `
43
44 // SPDX-License-Identifier: AGPL-3.0-or-later
45 //
46 // Webhook fanout reads the same domain_events / cursor surface that
47 // notifications do — but with its own consumer row so progress
48 // doesn't collide with notify_fanout.
49 func (q *Queries) GetWebhookCursor(ctx context.Context, db DBTX, consumer string) (DomainEventsProcessed, error) {
50 row := db.QueryRow(ctx, getWebhookCursor, consumer)
51 var i DomainEventsProcessed
52 err := row.Scan(&i.Consumer, &i.LastEventID, &i.UpdatedAt)
53 return i, err
54 }
55
56 const listUnprocessedDomainEvents = `-- name: ListUnprocessedDomainEvents :many
57 SELECT id, actor_user_id, kind, repo_id, source_kind, source_id,
58 public, payload, created_at
59 FROM domain_events
60 WHERE id > $1
61 ORDER BY id
62 LIMIT $2
63 `
64
65 type ListUnprocessedDomainEventsParams struct {
66 ID int64
67 Limit int32
68 }
69
70 func (q *Queries) ListUnprocessedDomainEvents(ctx context.Context, db DBTX, arg ListUnprocessedDomainEventsParams) ([]DomainEvent, error) {
71 rows, err := db.Query(ctx, listUnprocessedDomainEvents, arg.ID, arg.Limit)
72 if err != nil {
73 return nil, err
74 }
75 defer rows.Close()
76 items := []DomainEvent{}
77 for rows.Next() {
78 var i DomainEvent
79 if err := rows.Scan(
80 &i.ID,
81 &i.ActorUserID,
82 &i.Kind,
83 &i.RepoID,
84 &i.SourceKind,
85 &i.SourceID,
86 &i.Public,
87 &i.Payload,
88 &i.CreatedAt,
89 ); err != nil {
90 return nil, err
91 }
92 items = append(items, i)
93 }
94 if err := rows.Err(); err != nil {
95 return nil, err
96 }
97 return items, nil
98 }
99
100 const setWebhookCursor = `-- name: SetWebhookCursor :exec
101 INSERT INTO domain_events_processed (consumer, last_event_id)
102 VALUES ($1, $2)
103 ON CONFLICT (consumer)
104 DO UPDATE SET last_event_id = EXCLUDED.last_event_id,
105 updated_at = now()
106 `
107
108 type SetWebhookCursorParams struct {
109 Consumer string
110 LastEventID int64
111 }
112
113 func (q *Queries) SetWebhookCursor(ctx context.Context, db DBTX, arg SetWebhookCursorParams) error {
114 _, err := db.Exec(ctx, setWebhookCursor, arg.Consumer, arg.LastEventID)
115 return err
116 }
117