| 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 | |
| 11 | "github.com/jackc/pgx/v5/pgtype" |
| 12 | ) |
| 13 | |
| 14 | const autoDisableWebhook = `-- name: AutoDisableWebhook :exec |
| 15 | UPDATE webhooks |
| 16 | SET disabled_at = now(), |
| 17 | disabled_reason = $2, |
| 18 | updated_at = now() |
| 19 | WHERE id = $1 |
| 20 | ` |
| 21 | |
| 22 | type AutoDisableWebhookParams struct { |
| 23 | ID int64 |
| 24 | DisabledReason pgtype.Text |
| 25 | } |
| 26 | |
| 27 | func (q *Queries) AutoDisableWebhook(ctx context.Context, db DBTX, arg AutoDisableWebhookParams) error { |
| 28 | _, err := db.Exec(ctx, autoDisableWebhook, arg.ID, arg.DisabledReason) |
| 29 | return err |
| 30 | } |
| 31 | |
| 32 | const claimDueDeliveries = `-- name: ClaimDueDeliveries :many |
| 33 | SELECT id FROM webhook_deliveries |
| 34 | WHERE status IN ('pending', 'failed_retry') |
| 35 | AND (next_retry_at IS NULL OR next_retry_at <= now()) |
| 36 | ORDER BY next_retry_at NULLS FIRST, id |
| 37 | LIMIT $1 |
| 38 | FOR UPDATE SKIP LOCKED |
| 39 | ` |
| 40 | |
| 41 | // Hot-path for the deliver worker: picks up to $1 rows that are |
| 42 | // pending or in retry-ready state, FOR UPDATE SKIP LOCKED so concurrent |
| 43 | // workers don't double-send. The deliverer marks the row 'pending' |
| 44 | // with a far-future next_retry_at while it works on it (defense |
| 45 | // against re-claim during a long HTTP timeout). |
| 46 | func (q *Queries) ClaimDueDeliveries(ctx context.Context, db DBTX, limit int32) ([]int64, error) { |
| 47 | rows, err := db.Query(ctx, claimDueDeliveries, limit) |
| 48 | if err != nil { |
| 49 | return nil, err |
| 50 | } |
| 51 | defer rows.Close() |
| 52 | items := []int64{} |
| 53 | for rows.Next() { |
| 54 | var id int64 |
| 55 | if err := rows.Scan(&id); err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | items = append(items, id) |
| 59 | } |
| 60 | if err := rows.Err(); err != nil { |
| 61 | return nil, err |
| 62 | } |
| 63 | return items, nil |
| 64 | } |
| 65 | |
| 66 | const countDeliveriesForWebhook = `-- name: CountDeliveriesForWebhook :one |
| 67 | SELECT count(*)::bigint FROM webhook_deliveries WHERE webhook_id = $1 |
| 68 | ` |
| 69 | |
| 70 | func (q *Queries) CountDeliveriesForWebhook(ctx context.Context, db DBTX, webhookID int64) (int64, error) { |
| 71 | row := db.QueryRow(ctx, countDeliveriesForWebhook, webhookID) |
| 72 | var column_1 int64 |
| 73 | err := row.Scan(&column_1) |
| 74 | return column_1, err |
| 75 | } |
| 76 | |
| 77 | const createDelivery = `-- name: CreateDelivery :one |
| 78 | INSERT INTO webhook_deliveries ( |
| 79 | webhook_id, event_kind, event_id, payload, request_headers, |
| 80 | request_body, attempt, max_attempts, next_retry_at, status, |
| 81 | idempotency_key, redeliver_of |
| 82 | ) VALUES ( |
| 83 | $1, $2, $3::bigint, |
| 84 | $4, $5, $6, |
| 85 | $7, $8, $9::timestamptz, |
| 86 | $10, $11, $12::bigint |
| 87 | ) |
| 88 | RETURNING id, webhook_id, event_kind, event_id, delivery_uuid, payload, request_headers, request_body, response_status, response_headers, response_body, response_truncated, started_at, completed_at, attempt, max_attempts, next_retry_at, status, idempotency_key, error_summary, redeliver_of |
| 89 | ` |
| 90 | |
| 91 | type CreateDeliveryParams struct { |
| 92 | WebhookID int64 |
| 93 | EventKind string |
| 94 | EventID pgtype.Int8 |
| 95 | Payload []byte |
| 96 | RequestHeaders []byte |
| 97 | RequestBody []byte |
| 98 | Attempt int32 |
| 99 | MaxAttempts int32 |
| 100 | NextRetryAt pgtype.Timestamptz |
| 101 | Status WebhookDeliveryStatus |
| 102 | IdempotencyKey string |
| 103 | RedeliverOf pgtype.Int8 |
| 104 | } |
| 105 | |
| 106 | func (q *Queries) CreateDelivery(ctx context.Context, db DBTX, arg CreateDeliveryParams) (WebhookDelivery, error) { |
| 107 | row := db.QueryRow(ctx, createDelivery, |
| 108 | arg.WebhookID, |
| 109 | arg.EventKind, |
| 110 | arg.EventID, |
| 111 | arg.Payload, |
| 112 | arg.RequestHeaders, |
| 113 | arg.RequestBody, |
| 114 | arg.Attempt, |
| 115 | arg.MaxAttempts, |
| 116 | arg.NextRetryAt, |
| 117 | arg.Status, |
| 118 | arg.IdempotencyKey, |
| 119 | arg.RedeliverOf, |
| 120 | ) |
| 121 | var i WebhookDelivery |
| 122 | err := row.Scan( |
| 123 | &i.ID, |
| 124 | &i.WebhookID, |
| 125 | &i.EventKind, |
| 126 | &i.EventID, |
| 127 | &i.DeliveryUuid, |
| 128 | &i.Payload, |
| 129 | &i.RequestHeaders, |
| 130 | &i.RequestBody, |
| 131 | &i.ResponseStatus, |
| 132 | &i.ResponseHeaders, |
| 133 | &i.ResponseBody, |
| 134 | &i.ResponseTruncated, |
| 135 | &i.StartedAt, |
| 136 | &i.CompletedAt, |
| 137 | &i.Attempt, |
| 138 | &i.MaxAttempts, |
| 139 | &i.NextRetryAt, |
| 140 | &i.Status, |
| 141 | &i.IdempotencyKey, |
| 142 | &i.ErrorSummary, |
| 143 | &i.RedeliverOf, |
| 144 | ) |
| 145 | return i, err |
| 146 | } |
| 147 | |
| 148 | const createWebhook = `-- name: CreateWebhook :one |
| 149 | |
| 150 | INSERT INTO webhooks ( |
| 151 | owner_kind, owner_id, url, content_type, events, |
| 152 | secret_ciphertext, secret_nonce, active, ssl_verification, |
| 153 | auto_disable_threshold, created_by_user_id |
| 154 | ) VALUES ( |
| 155 | $1, $2, $3, |
| 156 | $4, $5, |
| 157 | $6, $7, |
| 158 | $8, $9, |
| 159 | $10, |
| 160 | $11::bigint |
| 161 | ) |
| 162 | RETURNING id, owner_kind, owner_id, url, content_type, events, secret_ciphertext, secret_nonce, active, ssl_verification, consecutive_failures, auto_disable_threshold, disabled_at, disabled_reason, last_success_at, last_failure_at, created_by_user_id, created_at, updated_at |
| 163 | ` |
| 164 | |
| 165 | type CreateWebhookParams struct { |
| 166 | OwnerKind WebhookOwnerKind |
| 167 | OwnerID int64 |
| 168 | Url string |
| 169 | ContentType WebhookContentType |
| 170 | Events []string |
| 171 | SecretCiphertext []byte |
| 172 | SecretNonce []byte |
| 173 | Active bool |
| 174 | SslVerification bool |
| 175 | AutoDisableThreshold int32 |
| 176 | CreatedByUserID pgtype.Int8 |
| 177 | } |
| 178 | |
| 179 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 180 | // |
| 181 | // Query surface for the webhook package. Naming mirrors the verb the |
| 182 | // caller uses; visibility / ownership filters are applied by the |
| 183 | // handler layer (the queries are deliberately narrow on key columns). |
| 184 | func (q *Queries) CreateWebhook(ctx context.Context, db DBTX, arg CreateWebhookParams) (Webhook, error) { |
| 185 | row := db.QueryRow(ctx, createWebhook, |
| 186 | arg.OwnerKind, |
| 187 | arg.OwnerID, |
| 188 | arg.Url, |
| 189 | arg.ContentType, |
| 190 | arg.Events, |
| 191 | arg.SecretCiphertext, |
| 192 | arg.SecretNonce, |
| 193 | arg.Active, |
| 194 | arg.SslVerification, |
| 195 | arg.AutoDisableThreshold, |
| 196 | arg.CreatedByUserID, |
| 197 | ) |
| 198 | var i Webhook |
| 199 | err := row.Scan( |
| 200 | &i.ID, |
| 201 | &i.OwnerKind, |
| 202 | &i.OwnerID, |
| 203 | &i.Url, |
| 204 | &i.ContentType, |
| 205 | &i.Events, |
| 206 | &i.SecretCiphertext, |
| 207 | &i.SecretNonce, |
| 208 | &i.Active, |
| 209 | &i.SslVerification, |
| 210 | &i.ConsecutiveFailures, |
| 211 | &i.AutoDisableThreshold, |
| 212 | &i.DisabledAt, |
| 213 | &i.DisabledReason, |
| 214 | &i.LastSuccessAt, |
| 215 | &i.LastFailureAt, |
| 216 | &i.CreatedByUserID, |
| 217 | &i.CreatedAt, |
| 218 | &i.UpdatedAt, |
| 219 | ) |
| 220 | return i, err |
| 221 | } |
| 222 | |
| 223 | const deleteWebhook = `-- name: DeleteWebhook :exec |
| 224 | DELETE FROM webhooks WHERE id = $1 |
| 225 | ` |
| 226 | |
| 227 | func (q *Queries) DeleteWebhook(ctx context.Context, db DBTX, id int64) error { |
| 228 | _, err := db.Exec(ctx, deleteWebhook, id) |
| 229 | return err |
| 230 | } |
| 231 | |
| 232 | const getDeliveryByID = `-- name: GetDeliveryByID :one |
| 233 | SELECT id, webhook_id, event_kind, event_id, delivery_uuid, payload, request_headers, request_body, response_status, response_headers, response_body, response_truncated, started_at, completed_at, attempt, max_attempts, next_retry_at, status, idempotency_key, error_summary, redeliver_of FROM webhook_deliveries WHERE id = $1 |
| 234 | ` |
| 235 | |
| 236 | func (q *Queries) GetDeliveryByID(ctx context.Context, db DBTX, id int64) (WebhookDelivery, error) { |
| 237 | row := db.QueryRow(ctx, getDeliveryByID, id) |
| 238 | var i WebhookDelivery |
| 239 | err := row.Scan( |
| 240 | &i.ID, |
| 241 | &i.WebhookID, |
| 242 | &i.EventKind, |
| 243 | &i.EventID, |
| 244 | &i.DeliveryUuid, |
| 245 | &i.Payload, |
| 246 | &i.RequestHeaders, |
| 247 | &i.RequestBody, |
| 248 | &i.ResponseStatus, |
| 249 | &i.ResponseHeaders, |
| 250 | &i.ResponseBody, |
| 251 | &i.ResponseTruncated, |
| 252 | &i.StartedAt, |
| 253 | &i.CompletedAt, |
| 254 | &i.Attempt, |
| 255 | &i.MaxAttempts, |
| 256 | &i.NextRetryAt, |
| 257 | &i.Status, |
| 258 | &i.IdempotencyKey, |
| 259 | &i.ErrorSummary, |
| 260 | &i.RedeliverOf, |
| 261 | ) |
| 262 | return i, err |
| 263 | } |
| 264 | |
| 265 | const getWebhookByID = `-- name: GetWebhookByID :one |
| 266 | SELECT id, owner_kind, owner_id, url, content_type, events, secret_ciphertext, secret_nonce, active, ssl_verification, consecutive_failures, auto_disable_threshold, disabled_at, disabled_reason, last_success_at, last_failure_at, created_by_user_id, created_at, updated_at FROM webhooks WHERE id = $1 |
| 267 | ` |
| 268 | |
| 269 | func (q *Queries) GetWebhookByID(ctx context.Context, db DBTX, id int64) (Webhook, error) { |
| 270 | row := db.QueryRow(ctx, getWebhookByID, id) |
| 271 | var i Webhook |
| 272 | err := row.Scan( |
| 273 | &i.ID, |
| 274 | &i.OwnerKind, |
| 275 | &i.OwnerID, |
| 276 | &i.Url, |
| 277 | &i.ContentType, |
| 278 | &i.Events, |
| 279 | &i.SecretCiphertext, |
| 280 | &i.SecretNonce, |
| 281 | &i.Active, |
| 282 | &i.SslVerification, |
| 283 | &i.ConsecutiveFailures, |
| 284 | &i.AutoDisableThreshold, |
| 285 | &i.DisabledAt, |
| 286 | &i.DisabledReason, |
| 287 | &i.LastSuccessAt, |
| 288 | &i.LastFailureAt, |
| 289 | &i.CreatedByUserID, |
| 290 | &i.CreatedAt, |
| 291 | &i.UpdatedAt, |
| 292 | ) |
| 293 | return i, err |
| 294 | } |
| 295 | |
| 296 | const listActiveWebhooksForOwner = `-- name: ListActiveWebhooksForOwner :many |
| 297 | SELECT id, owner_kind, owner_id, url, content_type, events, secret_ciphertext, secret_nonce, active, ssl_verification, consecutive_failures, auto_disable_threshold, disabled_at, disabled_reason, last_success_at, last_failure_at, created_by_user_id, created_at, updated_at FROM webhooks |
| 298 | WHERE owner_kind = $1 AND owner_id = $2 |
| 299 | AND active = true AND disabled_at IS NULL |
| 300 | ` |
| 301 | |
| 302 | type ListActiveWebhooksForOwnerParams struct { |
| 303 | OwnerKind WebhookOwnerKind |
| 304 | OwnerID int64 |
| 305 | } |
| 306 | |
| 307 | // Used by fanout to find subscribers for an event. |
| 308 | func (q *Queries) ListActiveWebhooksForOwner(ctx context.Context, db DBTX, arg ListActiveWebhooksForOwnerParams) ([]Webhook, error) { |
| 309 | rows, err := db.Query(ctx, listActiveWebhooksForOwner, arg.OwnerKind, arg.OwnerID) |
| 310 | if err != nil { |
| 311 | return nil, err |
| 312 | } |
| 313 | defer rows.Close() |
| 314 | items := []Webhook{} |
| 315 | for rows.Next() { |
| 316 | var i Webhook |
| 317 | if err := rows.Scan( |
| 318 | &i.ID, |
| 319 | &i.OwnerKind, |
| 320 | &i.OwnerID, |
| 321 | &i.Url, |
| 322 | &i.ContentType, |
| 323 | &i.Events, |
| 324 | &i.SecretCiphertext, |
| 325 | &i.SecretNonce, |
| 326 | &i.Active, |
| 327 | &i.SslVerification, |
| 328 | &i.ConsecutiveFailures, |
| 329 | &i.AutoDisableThreshold, |
| 330 | &i.DisabledAt, |
| 331 | &i.DisabledReason, |
| 332 | &i.LastSuccessAt, |
| 333 | &i.LastFailureAt, |
| 334 | &i.CreatedByUserID, |
| 335 | &i.CreatedAt, |
| 336 | &i.UpdatedAt, |
| 337 | ); err != nil { |
| 338 | return nil, err |
| 339 | } |
| 340 | items = append(items, i) |
| 341 | } |
| 342 | if err := rows.Err(); err != nil { |
| 343 | return nil, err |
| 344 | } |
| 345 | return items, nil |
| 346 | } |
| 347 | |
| 348 | const listDeliveriesForWebhook = `-- name: ListDeliveriesForWebhook :many |
| 349 | SELECT id, webhook_id, event_kind, event_id, delivery_uuid, response_status, |
| 350 | response_truncated, started_at, completed_at, attempt, max_attempts, |
| 351 | next_retry_at, status, error_summary, redeliver_of |
| 352 | FROM webhook_deliveries |
| 353 | WHERE webhook_id = $1 |
| 354 | ORDER BY started_at DESC |
| 355 | LIMIT $2 |
| 356 | ` |
| 357 | |
| 358 | type ListDeliveriesForWebhookParams struct { |
| 359 | WebhookID int64 |
| 360 | Limit int32 |
| 361 | } |
| 362 | |
| 363 | type ListDeliveriesForWebhookRow struct { |
| 364 | ID int64 |
| 365 | WebhookID int64 |
| 366 | EventKind string |
| 367 | EventID pgtype.Int8 |
| 368 | DeliveryUuid pgtype.UUID |
| 369 | ResponseStatus pgtype.Int4 |
| 370 | ResponseTruncated bool |
| 371 | StartedAt pgtype.Timestamptz |
| 372 | CompletedAt pgtype.Timestamptz |
| 373 | Attempt int32 |
| 374 | MaxAttempts int32 |
| 375 | NextRetryAt pgtype.Timestamptz |
| 376 | Status WebhookDeliveryStatus |
| 377 | ErrorSummary pgtype.Text |
| 378 | RedeliverOf pgtype.Int8 |
| 379 | } |
| 380 | |
| 381 | func (q *Queries) ListDeliveriesForWebhook(ctx context.Context, db DBTX, arg ListDeliveriesForWebhookParams) ([]ListDeliveriesForWebhookRow, error) { |
| 382 | rows, err := db.Query(ctx, listDeliveriesForWebhook, arg.WebhookID, arg.Limit) |
| 383 | if err != nil { |
| 384 | return nil, err |
| 385 | } |
| 386 | defer rows.Close() |
| 387 | items := []ListDeliveriesForWebhookRow{} |
| 388 | for rows.Next() { |
| 389 | var i ListDeliveriesForWebhookRow |
| 390 | if err := rows.Scan( |
| 391 | &i.ID, |
| 392 | &i.WebhookID, |
| 393 | &i.EventKind, |
| 394 | &i.EventID, |
| 395 | &i.DeliveryUuid, |
| 396 | &i.ResponseStatus, |
| 397 | &i.ResponseTruncated, |
| 398 | &i.StartedAt, |
| 399 | &i.CompletedAt, |
| 400 | &i.Attempt, |
| 401 | &i.MaxAttempts, |
| 402 | &i.NextRetryAt, |
| 403 | &i.Status, |
| 404 | &i.ErrorSummary, |
| 405 | &i.RedeliverOf, |
| 406 | ); err != nil { |
| 407 | return nil, err |
| 408 | } |
| 409 | items = append(items, i) |
| 410 | } |
| 411 | if err := rows.Err(); err != nil { |
| 412 | return nil, err |
| 413 | } |
| 414 | return items, nil |
| 415 | } |
| 416 | |
| 417 | const listDeliveriesForWebhookPaged = `-- name: ListDeliveriesForWebhookPaged :many |
| 418 | SELECT id, webhook_id, event_kind, event_id, delivery_uuid, response_status, |
| 419 | response_truncated, started_at, completed_at, attempt, max_attempts, |
| 420 | next_retry_at, status, error_summary, redeliver_of |
| 421 | FROM webhook_deliveries |
| 422 | WHERE webhook_id = $1 |
| 423 | ORDER BY started_at DESC |
| 424 | LIMIT $2 OFFSET $3 |
| 425 | ` |
| 426 | |
| 427 | type ListDeliveriesForWebhookPagedParams struct { |
| 428 | WebhookID int64 |
| 429 | Limit int32 |
| 430 | Offset int32 |
| 431 | } |
| 432 | |
| 433 | type ListDeliveriesForWebhookPagedRow struct { |
| 434 | ID int64 |
| 435 | WebhookID int64 |
| 436 | EventKind string |
| 437 | EventID pgtype.Int8 |
| 438 | DeliveryUuid pgtype.UUID |
| 439 | ResponseStatus pgtype.Int4 |
| 440 | ResponseTruncated bool |
| 441 | StartedAt pgtype.Timestamptz |
| 442 | CompletedAt pgtype.Timestamptz |
| 443 | Attempt int32 |
| 444 | MaxAttempts int32 |
| 445 | NextRetryAt pgtype.Timestamptz |
| 446 | Status WebhookDeliveryStatus |
| 447 | ErrorSummary pgtype.Text |
| 448 | RedeliverOf pgtype.Int8 |
| 449 | } |
| 450 | |
| 451 | // Paginated mirror of ListDeliveriesForWebhook for the REST surface. |
| 452 | // Order matches the unpaginated form so callers can swap freely. |
| 453 | func (q *Queries) ListDeliveriesForWebhookPaged(ctx context.Context, db DBTX, arg ListDeliveriesForWebhookPagedParams) ([]ListDeliveriesForWebhookPagedRow, error) { |
| 454 | rows, err := db.Query(ctx, listDeliveriesForWebhookPaged, arg.WebhookID, arg.Limit, arg.Offset) |
| 455 | if err != nil { |
| 456 | return nil, err |
| 457 | } |
| 458 | defer rows.Close() |
| 459 | items := []ListDeliveriesForWebhookPagedRow{} |
| 460 | for rows.Next() { |
| 461 | var i ListDeliveriesForWebhookPagedRow |
| 462 | if err := rows.Scan( |
| 463 | &i.ID, |
| 464 | &i.WebhookID, |
| 465 | &i.EventKind, |
| 466 | &i.EventID, |
| 467 | &i.DeliveryUuid, |
| 468 | &i.ResponseStatus, |
| 469 | &i.ResponseTruncated, |
| 470 | &i.StartedAt, |
| 471 | &i.CompletedAt, |
| 472 | &i.Attempt, |
| 473 | &i.MaxAttempts, |
| 474 | &i.NextRetryAt, |
| 475 | &i.Status, |
| 476 | &i.ErrorSummary, |
| 477 | &i.RedeliverOf, |
| 478 | ); err != nil { |
| 479 | return nil, err |
| 480 | } |
| 481 | items = append(items, i) |
| 482 | } |
| 483 | if err := rows.Err(); err != nil { |
| 484 | return nil, err |
| 485 | } |
| 486 | return items, nil |
| 487 | } |
| 488 | |
| 489 | const listWebhooksForOwner = `-- name: ListWebhooksForOwner :many |
| 490 | SELECT id, owner_kind, owner_id, url, content_type, events, secret_ciphertext, secret_nonce, active, ssl_verification, consecutive_failures, auto_disable_threshold, disabled_at, disabled_reason, last_success_at, last_failure_at, created_by_user_id, created_at, updated_at FROM webhooks |
| 491 | WHERE owner_kind = $1 AND owner_id = $2 |
| 492 | ORDER BY created_at DESC |
| 493 | ` |
| 494 | |
| 495 | type ListWebhooksForOwnerParams struct { |
| 496 | OwnerKind WebhookOwnerKind |
| 497 | OwnerID int64 |
| 498 | } |
| 499 | |
| 500 | func (q *Queries) ListWebhooksForOwner(ctx context.Context, db DBTX, arg ListWebhooksForOwnerParams) ([]Webhook, error) { |
| 501 | rows, err := db.Query(ctx, listWebhooksForOwner, arg.OwnerKind, arg.OwnerID) |
| 502 | if err != nil { |
| 503 | return nil, err |
| 504 | } |
| 505 | defer rows.Close() |
| 506 | items := []Webhook{} |
| 507 | for rows.Next() { |
| 508 | var i Webhook |
| 509 | if err := rows.Scan( |
| 510 | &i.ID, |
| 511 | &i.OwnerKind, |
| 512 | &i.OwnerID, |
| 513 | &i.Url, |
| 514 | &i.ContentType, |
| 515 | &i.Events, |
| 516 | &i.SecretCiphertext, |
| 517 | &i.SecretNonce, |
| 518 | &i.Active, |
| 519 | &i.SslVerification, |
| 520 | &i.ConsecutiveFailures, |
| 521 | &i.AutoDisableThreshold, |
| 522 | &i.DisabledAt, |
| 523 | &i.DisabledReason, |
| 524 | &i.LastSuccessAt, |
| 525 | &i.LastFailureAt, |
| 526 | &i.CreatedByUserID, |
| 527 | &i.CreatedAt, |
| 528 | &i.UpdatedAt, |
| 529 | ); err != nil { |
| 530 | return nil, err |
| 531 | } |
| 532 | items = append(items, i) |
| 533 | } |
| 534 | if err := rows.Err(); err != nil { |
| 535 | return nil, err |
| 536 | } |
| 537 | return items, nil |
| 538 | } |
| 539 | |
| 540 | const markDeliveryPermanentFailure = `-- name: MarkDeliveryPermanentFailure :exec |
| 541 | UPDATE webhook_deliveries |
| 542 | SET status = 'failed_permanent', |
| 543 | response_status = $1::int, |
| 544 | response_headers = $2::jsonb, |
| 545 | response_body = $3::bytea, |
| 546 | response_truncated = $4::bool, |
| 547 | completed_at = now(), |
| 548 | error_summary = $5::text |
| 549 | WHERE id = $6::bigint |
| 550 | ` |
| 551 | |
| 552 | type MarkDeliveryPermanentFailureParams struct { |
| 553 | ResponseStatus pgtype.Int4 |
| 554 | ResponseHeaders []byte |
| 555 | ResponseBody []byte |
| 556 | ResponseTruncated bool |
| 557 | ErrorSummary string |
| 558 | ID int64 |
| 559 | } |
| 560 | |
| 561 | func (q *Queries) MarkDeliveryPermanentFailure(ctx context.Context, db DBTX, arg MarkDeliveryPermanentFailureParams) error { |
| 562 | _, err := db.Exec(ctx, markDeliveryPermanentFailure, |
| 563 | arg.ResponseStatus, |
| 564 | arg.ResponseHeaders, |
| 565 | arg.ResponseBody, |
| 566 | arg.ResponseTruncated, |
| 567 | arg.ErrorSummary, |
| 568 | arg.ID, |
| 569 | ) |
| 570 | return err |
| 571 | } |
| 572 | |
| 573 | const markDeliveryRetry = `-- name: MarkDeliveryRetry :exec |
| 574 | UPDATE webhook_deliveries |
| 575 | SET status = 'failed_retry', |
| 576 | attempt = attempt + 1, |
| 577 | response_status = $1::int, |
| 578 | response_headers = $2::jsonb, |
| 579 | response_body = $3::bytea, |
| 580 | response_truncated = $4::bool, |
| 581 | next_retry_at = $5::timestamptz, |
| 582 | error_summary = $6::text |
| 583 | WHERE id = $7::bigint |
| 584 | ` |
| 585 | |
| 586 | type MarkDeliveryRetryParams struct { |
| 587 | ResponseStatus pgtype.Int4 |
| 588 | ResponseHeaders []byte |
| 589 | ResponseBody []byte |
| 590 | ResponseTruncated bool |
| 591 | NextRetryAt pgtype.Timestamptz |
| 592 | ErrorSummary string |
| 593 | ID int64 |
| 594 | } |
| 595 | |
| 596 | func (q *Queries) MarkDeliveryRetry(ctx context.Context, db DBTX, arg MarkDeliveryRetryParams) error { |
| 597 | _, err := db.Exec(ctx, markDeliveryRetry, |
| 598 | arg.ResponseStatus, |
| 599 | arg.ResponseHeaders, |
| 600 | arg.ResponseBody, |
| 601 | arg.ResponseTruncated, |
| 602 | arg.NextRetryAt, |
| 603 | arg.ErrorSummary, |
| 604 | arg.ID, |
| 605 | ) |
| 606 | return err |
| 607 | } |
| 608 | |
| 609 | const markDeliverySucceeded = `-- name: MarkDeliverySucceeded :exec |
| 610 | UPDATE webhook_deliveries |
| 611 | SET status = 'succeeded', |
| 612 | response_status = $2, |
| 613 | response_headers = $3, |
| 614 | response_body = $4, |
| 615 | response_truncated = $5, |
| 616 | completed_at = now(), |
| 617 | error_summary = NULL |
| 618 | WHERE id = $1 |
| 619 | ` |
| 620 | |
| 621 | type MarkDeliverySucceededParams struct { |
| 622 | ID int64 |
| 623 | ResponseStatus pgtype.Int4 |
| 624 | ResponseHeaders []byte |
| 625 | ResponseBody []byte |
| 626 | ResponseTruncated bool |
| 627 | } |
| 628 | |
| 629 | func (q *Queries) MarkDeliverySucceeded(ctx context.Context, db DBTX, arg MarkDeliverySucceededParams) error { |
| 630 | _, err := db.Exec(ctx, markDeliverySucceeded, |
| 631 | arg.ID, |
| 632 | arg.ResponseStatus, |
| 633 | arg.ResponseHeaders, |
| 634 | arg.ResponseBody, |
| 635 | arg.ResponseTruncated, |
| 636 | ) |
| 637 | return err |
| 638 | } |
| 639 | |
| 640 | const purgeOldDeliveries = `-- name: PurgeOldDeliveries :execrows |
| 641 | DELETE FROM webhook_deliveries |
| 642 | WHERE status IN ('succeeded', 'failed_permanent') |
| 643 | AND completed_at < now() - $1::interval |
| 644 | ` |
| 645 | |
| 646 | // Cron: drops terminal deliveries older than the retention window. |
| 647 | // pending/failed_retry rows are left alone so an in-flight retry isn't |
| 648 | // aborted out from under the worker. |
| 649 | func (q *Queries) PurgeOldDeliveries(ctx context.Context, db DBTX, retention pgtype.Interval) (int64, error) { |
| 650 | result, err := db.Exec(ctx, purgeOldDeliveries, retention) |
| 651 | if err != nil { |
| 652 | return 0, err |
| 653 | } |
| 654 | return result.RowsAffected(), nil |
| 655 | } |
| 656 | |
| 657 | const recordWebhookFailure = `-- name: RecordWebhookFailure :one |
| 658 | UPDATE webhooks |
| 659 | SET consecutive_failures = consecutive_failures + 1, |
| 660 | last_failure_at = now(), |
| 661 | updated_at = now() |
| 662 | WHERE id = $1 |
| 663 | RETURNING consecutive_failures, auto_disable_threshold |
| 664 | ` |
| 665 | |
| 666 | type RecordWebhookFailureRow struct { |
| 667 | ConsecutiveFailures int32 |
| 668 | AutoDisableThreshold int32 |
| 669 | } |
| 670 | |
| 671 | // Increments the failure counter and reports the new value so the |
| 672 | // caller can decide whether to auto-disable. |
| 673 | func (q *Queries) RecordWebhookFailure(ctx context.Context, db DBTX, id int64) (RecordWebhookFailureRow, error) { |
| 674 | row := db.QueryRow(ctx, recordWebhookFailure, id) |
| 675 | var i RecordWebhookFailureRow |
| 676 | err := row.Scan(&i.ConsecutiveFailures, &i.AutoDisableThreshold) |
| 677 | return i, err |
| 678 | } |
| 679 | |
| 680 | const recordWebhookSuccess = `-- name: RecordWebhookSuccess :exec |
| 681 | UPDATE webhooks |
| 682 | SET consecutive_failures = 0, |
| 683 | last_success_at = now(), |
| 684 | updated_at = now() |
| 685 | WHERE id = $1 |
| 686 | ` |
| 687 | |
| 688 | func (q *Queries) RecordWebhookSuccess(ctx context.Context, db DBTX, id int64) error { |
| 689 | _, err := db.Exec(ctx, recordWebhookSuccess, id) |
| 690 | return err |
| 691 | } |
| 692 | |
| 693 | const setWebhookActive = `-- name: SetWebhookActive :exec |
| 694 | UPDATE webhooks |
| 695 | SET active = $2, |
| 696 | disabled_at = NULL, |
| 697 | disabled_reason = NULL, |
| 698 | consecutive_failures = 0, |
| 699 | updated_at = now() |
| 700 | WHERE id = $1 |
| 701 | ` |
| 702 | |
| 703 | type SetWebhookActiveParams struct { |
| 704 | ID int64 |
| 705 | Active bool |
| 706 | } |
| 707 | |
| 708 | // Re-enables a previously auto-disabled webhook (UI affordance). |
| 709 | // Resets the failure counter and clears disabled_at/reason. |
| 710 | func (q *Queries) SetWebhookActive(ctx context.Context, db DBTX, arg SetWebhookActiveParams) error { |
| 711 | _, err := db.Exec(ctx, setWebhookActive, arg.ID, arg.Active) |
| 712 | return err |
| 713 | } |
| 714 | |
| 715 | const updateWebhook = `-- name: UpdateWebhook :exec |
| 716 | UPDATE webhooks |
| 717 | SET url = $2, |
| 718 | content_type = $3, |
| 719 | events = $4, |
| 720 | active = $5, |
| 721 | ssl_verification = $6, |
| 722 | auto_disable_threshold = $7, |
| 723 | updated_at = now() |
| 724 | WHERE id = $1 |
| 725 | ` |
| 726 | |
| 727 | type UpdateWebhookParams struct { |
| 728 | ID int64 |
| 729 | Url string |
| 730 | ContentType WebhookContentType |
| 731 | Events []string |
| 732 | Active bool |
| 733 | SslVerification bool |
| 734 | AutoDisableThreshold int32 |
| 735 | } |
| 736 | |
| 737 | func (q *Queries) UpdateWebhook(ctx context.Context, db DBTX, arg UpdateWebhookParams) error { |
| 738 | _, err := db.Exec(ctx, updateWebhook, |
| 739 | arg.ID, |
| 740 | arg.Url, |
| 741 | arg.ContentType, |
| 742 | arg.Events, |
| 743 | arg.Active, |
| 744 | arg.SslVerification, |
| 745 | arg.AutoDisableThreshold, |
| 746 | ) |
| 747 | return err |
| 748 | } |
| 749 | |
| 750 | const updateWebhookSecret = `-- name: UpdateWebhookSecret :exec |
| 751 | UPDATE webhooks |
| 752 | SET secret_ciphertext = $2, |
| 753 | secret_nonce = $3, |
| 754 | updated_at = now() |
| 755 | WHERE id = $1 |
| 756 | ` |
| 757 | |
| 758 | type UpdateWebhookSecretParams struct { |
| 759 | ID int64 |
| 760 | SecretCiphertext []byte |
| 761 | SecretNonce []byte |
| 762 | } |
| 763 | |
| 764 | func (q *Queries) UpdateWebhookSecret(ctx context.Context, db DBTX, arg UpdateWebhookSecretParams) error { |
| 765 | _, err := db.Exec(ctx, updateWebhookSecret, arg.ID, arg.SecretCiphertext, arg.SecretNonce) |
| 766 | return err |
| 767 | } |
| 768 |