webhooks.sql.go290 lines, 7.4 KB
| 1 | // Code generated by sqlc. DO NOT EDIT. |
| 2 | // versions: |
| 3 | // sqlc v1.31.1 |
| 4 | // source: webhooks.sql |
| 5 | |
| 6 | package webhookdb |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/google/uuid" |
| 13 | ) |
| 14 | |
| 15 | const claimDue = `-- name: ClaimDue :many |
| 16 | UPDATE webhook_deliveries d SET next_attempt_at = now() + interval '1 minute' |
| 17 | FROM webhook_endpoints e |
| 18 | WHERE e.id = d.endpoint_id AND d.id IN ( |
| 19 | SELECT id FROM webhook_deliveries |
| 20 | WHERE state = 'pending' AND next_attempt_at <= now() |
| 21 | ORDER BY next_attempt_at |
| 22 | LIMIT $1 |
| 23 | FOR UPDATE SKIP LOCKED |
| 24 | ) |
| 25 | RETURNING d.id, d.event_id, d.payload, d.attempts, e.url, e.secret |
| 26 | ` |
| 27 | |
| 28 | type ClaimDueRow struct { |
| 29 | ID uuid.UUID `json:"id"` |
| 30 | EventID uuid.UUID `json:"event_id"` |
| 31 | Payload []byte `json:"payload"` |
| 32 | Attempts int32 `json:"attempts"` |
| 33 | URL string `json:"url"` |
| 34 | Secret string `json:"secret"` |
| 35 | } |
| 36 | |
| 37 | // Takes up to $1 due deliveries, and leases them for a minute: a worker that |
| 38 | // dies holding them only delays them. SKIP LOCKED lets workers share the work. |
| 39 | func (q *Queries) ClaimDue(ctx context.Context, limit int32) ([]ClaimDueRow, error) { |
| 40 | rows, err := q.db.Query(ctx, claimDue, limit) |
| 41 | if err != nil { |
| 42 | return nil, err |
| 43 | } |
| 44 | defer rows.Close() |
| 45 | items := []ClaimDueRow{} |
| 46 | for rows.Next() { |
| 47 | var i ClaimDueRow |
| 48 | if err := rows.Scan( |
| 49 | &i.ID, |
| 50 | &i.EventID, |
| 51 | &i.Payload, |
| 52 | &i.Attempts, |
| 53 | &i.URL, |
| 54 | &i.Secret, |
| 55 | ); err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | items = append(items, i) |
| 59 | } |
| 60 | if err := rows.Err(); err != nil { |
| 61 | return nil, err |
| 62 | } |
| 63 | return items, nil |
| 64 | } |
| 65 | |
| 66 | const createEndpoint = `-- name: CreateEndpoint :one |
| 67 | INSERT INTO webhook_endpoints (id, user_id, url, event_types, secret) |
| 68 | VALUES ($1, $2, $3, $4, $5) |
| 69 | RETURNING id, user_id, url, event_types, secret, created_at |
| 70 | ` |
| 71 | |
| 72 | type CreateEndpointParams struct { |
| 73 | ID uuid.UUID `json:"id"` |
| 74 | UserID uuid.UUID `json:"user_id"` |
| 75 | URL string `json:"url"` |
| 76 | EventTypes []string `json:"event_types"` |
| 77 | Secret string `json:"secret"` |
| 78 | } |
| 79 | |
| 80 | func (q *Queries) CreateEndpoint(ctx context.Context, arg CreateEndpointParams) (WebhookEndpoint, error) { |
| 81 | row := q.db.QueryRow(ctx, createEndpoint, |
| 82 | arg.ID, |
| 83 | arg.UserID, |
| 84 | arg.URL, |
| 85 | arg.EventTypes, |
| 86 | arg.Secret, |
| 87 | ) |
| 88 | var i WebhookEndpoint |
| 89 | err := row.Scan( |
| 90 | &i.ID, |
| 91 | &i.UserID, |
| 92 | &i.URL, |
| 93 | &i.EventTypes, |
| 94 | &i.Secret, |
| 95 | &i.CreatedAt, |
| 96 | ) |
| 97 | return i, err |
| 98 | } |
| 99 | |
| 100 | const deleteEndpoint = `-- name: DeleteEndpoint :execrows |
| 101 | DELETE FROM webhook_endpoints WHERE id = $1 AND user_id = $2 |
| 102 | ` |
| 103 | |
| 104 | type DeleteEndpointParams struct { |
| 105 | ID uuid.UUID `json:"id"` |
| 106 | UserID uuid.UUID `json:"user_id"` |
| 107 | } |
| 108 | |
| 109 | func (q *Queries) DeleteEndpoint(ctx context.Context, arg DeleteEndpointParams) (int64, error) { |
| 110 | result, err := q.db.Exec(ctx, deleteEndpoint, arg.ID, arg.UserID) |
| 111 | if err != nil { |
| 112 | return 0, err |
| 113 | } |
| 114 | return result.RowsAffected(), nil |
| 115 | } |
| 116 | |
| 117 | const fanOut = `-- name: FanOut :execrows |
| 118 | INSERT INTO webhook_deliveries (endpoint_id, event_id, event_type, payload) |
| 119 | SELECT id, $1::uuid, $2::text, $3::bytea FROM webhook_endpoints |
| 120 | WHERE user_id = $4 AND $2 = ANY (event_types) |
| 121 | ON CONFLICT (endpoint_id, event_id) DO NOTHING |
| 122 | ` |
| 123 | |
| 124 | type FanOutParams struct { |
| 125 | EventID uuid.UUID `json:"event_id"` |
| 126 | EventType string `json:"event_type"` |
| 127 | Payload []byte `json:"payload"` |
| 128 | UserID uuid.UUID `json:"user_id"` |
| 129 | } |
| 130 | |
| 131 | // A delivery for each of the user's endpoints that wants this event. |
| 132 | func (q *Queries) FanOut(ctx context.Context, arg FanOutParams) (int64, error) { |
| 133 | result, err := q.db.Exec(ctx, fanOut, |
| 134 | arg.EventID, |
| 135 | arg.EventType, |
| 136 | arg.Payload, |
| 137 | arg.UserID, |
| 138 | ) |
| 139 | if err != nil { |
| 140 | return 0, err |
| 141 | } |
| 142 | return result.RowsAffected(), nil |
| 143 | } |
| 144 | |
| 145 | const listDeliveries = `-- name: ListDeliveries :many |
| 146 | SELECT d.id, d.event_id, d.event_type, d.state, d.attempts, d.last_status, |
| 147 | d.last_error, d.last_response_ms, d.next_attempt_at, d.created_at |
| 148 | FROM webhook_deliveries d JOIN webhook_endpoints e ON e.id = d.endpoint_id |
| 149 | WHERE e.user_id = $1 AND d.endpoint_id = $2 |
| 150 | ORDER BY d.created_at DESC |
| 151 | LIMIT 50 |
| 152 | ` |
| 153 | |
| 154 | type ListDeliveriesParams struct { |
| 155 | UserID uuid.UUID `json:"user_id"` |
| 156 | EndpointID uuid.UUID `json:"endpoint_id"` |
| 157 | } |
| 158 | |
| 159 | type ListDeliveriesRow struct { |
| 160 | ID uuid.UUID `json:"id"` |
| 161 | EventID uuid.UUID `json:"event_id"` |
| 162 | EventType string `json:"event_type"` |
| 163 | State string `json:"state"` |
| 164 | Attempts int32 `json:"attempts"` |
| 165 | LastStatus int32 `json:"last_status"` |
| 166 | LastError string `json:"last_error"` |
| 167 | LastResponseMs int32 `json:"last_response_ms"` |
| 168 | NextAttemptAt time.Time `json:"next_attempt_at"` |
| 169 | CreatedAt time.Time `json:"created_at"` |
| 170 | } |
| 171 | |
| 172 | func (q *Queries) ListDeliveries(ctx context.Context, arg ListDeliveriesParams) ([]ListDeliveriesRow, error) { |
| 173 | rows, err := q.db.Query(ctx, listDeliveries, arg.UserID, arg.EndpointID) |
| 174 | if err != nil { |
| 175 | return nil, err |
| 176 | } |
| 177 | defer rows.Close() |
| 178 | items := []ListDeliveriesRow{} |
| 179 | for rows.Next() { |
| 180 | var i ListDeliveriesRow |
| 181 | if err := rows.Scan( |
| 182 | &i.ID, |
| 183 | &i.EventID, |
| 184 | &i.EventType, |
| 185 | &i.State, |
| 186 | &i.Attempts, |
| 187 | &i.LastStatus, |
| 188 | &i.LastError, |
| 189 | &i.LastResponseMs, |
| 190 | &i.NextAttemptAt, |
| 191 | &i.CreatedAt, |
| 192 | ); err != nil { |
| 193 | return nil, err |
| 194 | } |
| 195 | items = append(items, i) |
| 196 | } |
| 197 | if err := rows.Err(); err != nil { |
| 198 | return nil, err |
| 199 | } |
| 200 | return items, nil |
| 201 | } |
| 202 | |
| 203 | const listEndpoints = `-- name: ListEndpoints :many |
| 204 | SELECT id, url, event_types, created_at FROM webhook_endpoints |
| 205 | WHERE user_id = $1 |
| 206 | ORDER BY created_at |
| 207 | ` |
| 208 | |
| 209 | type ListEndpointsRow struct { |
| 210 | ID uuid.UUID `json:"id"` |
| 211 | URL string `json:"url"` |
| 212 | EventTypes []string `json:"event_types"` |
| 213 | CreatedAt time.Time `json:"created_at"` |
| 214 | } |
| 215 | |
| 216 | // Never the secrets: they're shown once, when an endpoint is created. |
| 217 | func (q *Queries) ListEndpoints(ctx context.Context, userID uuid.UUID) ([]ListEndpointsRow, error) { |
| 218 | rows, err := q.db.Query(ctx, listEndpoints, userID) |
| 219 | if err != nil { |
| 220 | return nil, err |
| 221 | } |
| 222 | defer rows.Close() |
| 223 | items := []ListEndpointsRow{} |
| 224 | for rows.Next() { |
| 225 | var i ListEndpointsRow |
| 226 | if err := rows.Scan( |
| 227 | &i.ID, |
| 228 | &i.URL, |
| 229 | &i.EventTypes, |
| 230 | &i.CreatedAt, |
| 231 | ); err != nil { |
| 232 | return nil, err |
| 233 | } |
| 234 | items = append(items, i) |
| 235 | } |
| 236 | if err := rows.Err(); err != nil { |
| 237 | return nil, err |
| 238 | } |
| 239 | return items, nil |
| 240 | } |
| 241 | |
| 242 | const recordAttempt = `-- name: RecordAttempt :exec |
| 243 | UPDATE webhook_deliveries |
| 244 | SET attempts = attempts + 1, state = $1, |
| 245 | next_attempt_at = now() + make_interval(secs => $2), |
| 246 | last_status = $3, last_error = $4, last_response_ms = $5 |
| 247 | WHERE id = $6 |
| 248 | ` |
| 249 | |
| 250 | type RecordAttemptParams struct { |
| 251 | State string `json:"state"` |
| 252 | RetryInSeconds float64 `json:"retry_in_seconds"` |
| 253 | LastStatus int32 `json:"last_status"` |
| 254 | LastError string `json:"last_error"` |
| 255 | LastResponseMs int32 `json:"last_response_ms"` |
| 256 | ID uuid.UUID `json:"id"` |
| 257 | } |
| 258 | |
| 259 | // The next attempt is due by the database clock, the one ClaimDue goes by. |
| 260 | func (q *Queries) RecordAttempt(ctx context.Context, arg RecordAttemptParams) error { |
| 261 | _, err := q.db.Exec(ctx, recordAttempt, |
| 262 | arg.State, |
| 263 | arg.RetryInSeconds, |
| 264 | arg.LastStatus, |
| 265 | arg.LastError, |
| 266 | arg.LastResponseMs, |
| 267 | arg.ID, |
| 268 | ) |
| 269 | return err |
| 270 | } |
| 271 | |
| 272 | const replay = `-- name: Replay :execrows |
| 273 | UPDATE webhook_deliveries d SET state = 'pending', attempts = 0, next_attempt_at = now() |
| 274 | FROM webhook_endpoints e |
| 275 | WHERE e.id = d.endpoint_id AND d.id = $1 AND e.user_id = $2 |
| 276 | ` |
| 277 | |
| 278 | type ReplayParams struct { |
| 279 | ID uuid.UUID `json:"id"` |
| 280 | UserID uuid.UUID `json:"user_id"` |
| 281 | } |
| 282 | |
| 283 | // Sends a delivery again, as new: due now, with the whole schedule ahead. |
| 284 | func (q *Queries) Replay(ctx context.Context, arg ReplayParams) (int64, error) { |
| 285 | result, err := q.db.Exec(ctx, replay, arg.ID, arg.UserID) |
| 286 | if err != nil { |
| 287 | return 0, err |
| 288 | } |
| 289 | return result.RowsAffected(), nil |
| 290 | } |