From c8a4a930ea06a2623b53c8b34446b06afef272e4 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Mon, 20 Apr 2026 20:15:45 -0400 Subject: [PATCH] feat(dbq): generate session queries Adds Insert/Get/Touch/Delete helpers over the sessions table. Co-Authored-By: Claude Opus 4.7 --- internal/db/dbq/models.go | 9 ++++ internal/db/dbq/sessions.sql.go | 83 ++++++++++++++++++++++++++++++++ internal/db/queries/sessions.sql | 16 ++++++ 3 files changed, 108 insertions(+) create mode 100644 internal/db/dbq/sessions.sql.go create mode 100644 internal/db/queries/sessions.sql diff --git a/internal/db/dbq/models.go b/internal/db/dbq/models.go index e6e00138..57410eda 100644 --- a/internal/db/dbq/models.go +++ b/internal/db/dbq/models.go @@ -29,6 +29,15 @@ type Artist struct { UpdatedAt pgtype.Timestamptz } +type Session struct { + ID pgtype.UUID + UserID pgtype.UUID + TokenHash []byte + UserAgent string + CreatedAt pgtype.Timestamptz + LastSeenAt pgtype.Timestamptz +} + type Track struct { ID pgtype.UUID Title string diff --git a/internal/db/dbq/sessions.sql.go b/internal/db/dbq/sessions.sql.go new file mode 100644 index 00000000..3ac9741c --- /dev/null +++ b/internal/db/dbq/sessions.sql.go @@ -0,0 +1,83 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.27.0 +// source: sessions.sql + +package dbq + +import ( + "context" + + "github.com/jackc/pgx/v5/pgtype" +) + +const deleteSession = `-- name: DeleteSession :exec +DELETE FROM sessions WHERE id = $1 +` + +func (q *Queries) DeleteSession(ctx context.Context, id pgtype.UUID) error { + _, err := q.db.Exec(ctx, deleteSession, id) + return err +} + +const deleteSessionByTokenHash = `-- name: DeleteSessionByTokenHash :exec +DELETE FROM sessions WHERE token_hash = $1 +` + +func (q *Queries) DeleteSessionByTokenHash(ctx context.Context, tokenHash []byte) error { + _, err := q.db.Exec(ctx, deleteSessionByTokenHash, tokenHash) + return err +} + +const getSessionByTokenHash = `-- name: GetSessionByTokenHash :one +SELECT id, user_id, token_hash, user_agent, created_at, last_seen_at FROM sessions WHERE token_hash = $1 +` + +func (q *Queries) GetSessionByTokenHash(ctx context.Context, tokenHash []byte) (Session, error) { + row := q.db.QueryRow(ctx, getSessionByTokenHash, tokenHash) + var i Session + err := row.Scan( + &i.ID, + &i.UserID, + &i.TokenHash, + &i.UserAgent, + &i.CreatedAt, + &i.LastSeenAt, + ) + return i, err +} + +const insertSession = `-- name: InsertSession :one +INSERT INTO sessions (user_id, token_hash, user_agent) +VALUES ($1, $2, $3) +RETURNING id, user_id, token_hash, user_agent, created_at, last_seen_at +` + +type InsertSessionParams struct { + UserID pgtype.UUID + TokenHash []byte + UserAgent string +} + +func (q *Queries) InsertSession(ctx context.Context, arg InsertSessionParams) (Session, error) { + row := q.db.QueryRow(ctx, insertSession, arg.UserID, arg.TokenHash, arg.UserAgent) + var i Session + err := row.Scan( + &i.ID, + &i.UserID, + &i.TokenHash, + &i.UserAgent, + &i.CreatedAt, + &i.LastSeenAt, + ) + return i, err +} + +const touchSessionLastSeen = `-- name: TouchSessionLastSeen :exec +UPDATE sessions SET last_seen_at = now() WHERE id = $1 +` + +func (q *Queries) TouchSessionLastSeen(ctx context.Context, id pgtype.UUID) error { + _, err := q.db.Exec(ctx, touchSessionLastSeen, id) + return err +} diff --git a/internal/db/queries/sessions.sql b/internal/db/queries/sessions.sql new file mode 100644 index 00000000..438b066f --- /dev/null +++ b/internal/db/queries/sessions.sql @@ -0,0 +1,16 @@ +-- name: InsertSession :one +INSERT INTO sessions (user_id, token_hash, user_agent) +VALUES ($1, $2, $3) +RETURNING *; + +-- name: GetSessionByTokenHash :one +SELECT * FROM sessions WHERE token_hash = $1; + +-- name: TouchSessionLastSeen :exec +UPDATE sessions SET last_seen_at = now() WHERE id = $1; + +-- name: DeleteSession :exec +DELETE FROM sessions WHERE id = $1; + +-- name: DeleteSessionByTokenHash :exec +DELETE FROM sessions WHERE token_hash = $1;