CI / ci (push) Successful in 5m31s
Co-authored-by: orchestrator-agent <20+orchestrator-agent@code.cubecraftcreations.com>
161 lines
4.0 KiB
Go
161 lines
4.0 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: push_subscriptions.sql
|
|
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
)
|
|
|
|
const deletePushSubscription = `-- name: DeletePushSubscription :execrows
|
|
DELETE FROM push_subscriptions
|
|
WHERE endpoint = ?
|
|
`
|
|
|
|
func (q *Queries) DeletePushSubscription(ctx context.Context, endpoint string) (int64, error) {
|
|
result, err := q.db.ExecContext(ctx, deletePushSubscription, endpoint)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected()
|
|
}
|
|
|
|
const deletePushSubscriptionByID = `-- name: DeletePushSubscriptionByID :exec
|
|
DELETE FROM push_subscriptions
|
|
WHERE id = ?
|
|
`
|
|
|
|
// Removes a subscription the push service reports gone (404 or 410).
|
|
func (q *Queries) DeletePushSubscriptionByID(ctx context.Context, id string) error {
|
|
_, err := q.db.ExecContext(ctx, deletePushSubscriptionByID, id)
|
|
return err
|
|
}
|
|
|
|
const listPushSubscriptions = `-- name: ListPushSubscriptions :many
|
|
SELECT id, endpoint, p256dh, auth
|
|
FROM push_subscriptions
|
|
ORDER BY created_at, id
|
|
`
|
|
|
|
type ListPushSubscriptionsRow struct {
|
|
ID string
|
|
Endpoint string
|
|
P256dh string
|
|
Auth string
|
|
}
|
|
|
|
// Every stored subscription with its keys, for the push sender, oldest
|
|
// first.
|
|
func (q *Queries) ListPushSubscriptions(ctx context.Context) ([]ListPushSubscriptionsRow, error) {
|
|
rows, err := q.db.QueryContext(ctx, listPushSubscriptions)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListPushSubscriptionsRow
|
|
for rows.Next() {
|
|
var i ListPushSubscriptionsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Endpoint,
|
|
&i.P256dh,
|
|
&i.Auth,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const recordPushFailure = `-- name: RecordPushFailure :exec
|
|
UPDATE push_subscriptions
|
|
SET failure_count = failure_count + 1
|
|
WHERE id = ?
|
|
`
|
|
|
|
// Counts a delivery to a subscription that the push service did not accept.
|
|
func (q *Queries) RecordPushFailure(ctx context.Context, id string) error {
|
|
_, err := q.db.ExecContext(ctx, recordPushFailure, id)
|
|
return err
|
|
}
|
|
|
|
const recordPushSuccess = `-- name: RecordPushSuccess :exec
|
|
UPDATE push_subscriptions
|
|
SET last_success_at = ?
|
|
WHERE id = ?
|
|
`
|
|
|
|
type RecordPushSuccessParams struct {
|
|
LastSuccessAt sql.NullString
|
|
ID string
|
|
}
|
|
|
|
// Records that the push service accepted a notification for a subscription.
|
|
func (q *Queries) RecordPushSuccess(ctx context.Context, arg RecordPushSuccessParams) error {
|
|
_, err := q.db.ExecContext(ctx, recordPushSuccess, arg.LastSuccessAt, arg.ID)
|
|
return err
|
|
}
|
|
|
|
const upsertPushSubscription = `-- name: UpsertPushSubscription :one
|
|
INSERT INTO push_subscriptions (
|
|
id, endpoint, p256dh, auth, user_agent, failure_count, created_at, updated_at
|
|
) VALUES (
|
|
?, ?, ?, ?, ?, 0, ?, ?
|
|
)
|
|
ON CONFLICT (endpoint) DO UPDATE SET
|
|
p256dh = excluded.p256dh,
|
|
auth = excluded.auth,
|
|
user_agent = excluded.user_agent,
|
|
failure_count = 0,
|
|
updated_at = excluded.updated_at
|
|
RETURNING id, endpoint, p256dh, auth, user_agent, failure_count, last_success_at, created_at, updated_at
|
|
`
|
|
|
|
type UpsertPushSubscriptionParams struct {
|
|
ID string
|
|
Endpoint string
|
|
P256dh string
|
|
Auth string
|
|
UserAgent sql.NullString
|
|
CreatedAt string
|
|
UpdatedAt string
|
|
}
|
|
|
|
// Stores a browser's push subscription, keyed by its endpoint. Subscribing
|
|
// an endpoint already stored replaces its keys and user agent and clears
|
|
// its failure count, keeping its id, created_at and last_success_at.
|
|
func (q *Queries) UpsertPushSubscription(ctx context.Context, arg UpsertPushSubscriptionParams) (PushSubscription, error) {
|
|
row := q.db.QueryRowContext(ctx, upsertPushSubscription,
|
|
arg.ID,
|
|
arg.Endpoint,
|
|
arg.P256dh,
|
|
arg.Auth,
|
|
arg.UserAgent,
|
|
arg.CreatedAt,
|
|
arg.UpdatedAt,
|
|
)
|
|
var i PushSubscription
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Endpoint,
|
|
&i.P256dh,
|
|
&i.Auth,
|
|
&i.UserAgent,
|
|
&i.FailureCount,
|
|
&i.LastSuccessAt,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|