diff --git a/internal/repository/models.go b/internal/repository/models.go index 97df882..ee9dfd6 100644 --- a/internal/repository/models.go +++ b/internal/repository/models.go @@ -8,7 +8,6 @@ import ( "time" "github.com/google/uuid" - "github.com/jackc/pgx/v5/pgtype" ) type ApiService struct { @@ -27,21 +26,21 @@ type ApiService struct { } type ServiceSession struct { - ID uuid.UUID `json:"id"` - ServiceID uuid.UUID `json:"service_id"` - ClientID string `json:"client_id"` - UserID *uuid.UUID `json:"user_id"` - IssuedAt time.Time `json:"issued_at"` - ExpiresAt *time.Time `json:"expires_at"` - LastActive *time.Time `json:"last_active"` - IpAddress pgtype.Text `json:"ip_address"` - UserAgent *string `json:"user_agent"` - AccessTokenID *uuid.UUID `json:"access_token_id"` - RefreshTokenID *uuid.UUID `json:"refresh_token_id"` - IsActive bool `json:"is_active"` - RevokedAt *time.Time `json:"revoked_at"` - Scope *string `json:"scope"` - Claims []byte `json:"claims"` + ID uuid.UUID `json:"id"` + ServiceID uuid.UUID `json:"service_id"` + ClientID string `json:"client_id"` + UserID *uuid.UUID `json:"user_id"` + IssuedAt time.Time `json:"issued_at"` + ExpiresAt *time.Time `json:"expires_at"` + LastActive *time.Time `json:"last_active"` + IpAddress *string `json:"ip_address"` + UserAgent *string `json:"user_agent"` + AccessTokenID *uuid.UUID `json:"access_token_id"` + RefreshTokenID *uuid.UUID `json:"refresh_token_id"` + IsActive bool `json:"is_active"` + RevokedAt *time.Time `json:"revoked_at"` + Scope *string `json:"scope"` + Claims []byte `json:"claims"` } type User struct { @@ -62,17 +61,17 @@ type User struct { } type UserSession struct { - ID uuid.UUID `json:"id"` - UserID uuid.UUID `json:"user_id"` - SessionType string `json:"session_type"` - IssuedAt time.Time `json:"issued_at"` - ExpiresAt *time.Time `json:"expires_at"` - LastActive *time.Time `json:"last_active"` - IpAddress pgtype.Text `json:"ip_address"` - UserAgent *string `json:"user_agent"` - AccessTokenID *uuid.UUID `json:"access_token_id"` - RefreshTokenID *uuid.UUID `json:"refresh_token_id"` - DeviceInfo []byte `json:"device_info"` - IsActive bool `json:"is_active"` - RevokedAt *time.Time `json:"revoked_at"` + ID uuid.UUID `json:"id"` + UserID uuid.UUID `json:"user_id"` + SessionType string `json:"session_type"` + IssuedAt time.Time `json:"issued_at"` + ExpiresAt *time.Time `json:"expires_at"` + LastActive *time.Time `json:"last_active"` + IpAddress *string `json:"ip_address"` + UserAgent *string `json:"user_agent"` + AccessTokenID *uuid.UUID `json:"access_token_id"` + RefreshTokenID *uuid.UUID `json:"refresh_token_id"` + DeviceInfo []byte `json:"device_info"` + IsActive bool `json:"is_active"` + RevokedAt *time.Time `json:"revoked_at"` } diff --git a/internal/repository/service_sessions.sql.go b/internal/repository/service_sessions.sql.go index d910340..187c13e 100644 --- a/internal/repository/service_sessions.sql.go +++ b/internal/repository/service_sessions.sql.go @@ -10,7 +10,6 @@ import ( "time" "github.com/google/uuid" - "github.com/jackc/pgx/v5/pgtype" ) const createServiceSession = `-- name: CreateServiceSession :one @@ -27,15 +26,15 @@ RETURNING id, service_id, client_id, user_id, issued_at, expires_at, last_active ` type CreateServiceSessionParams struct { - ServiceID uuid.UUID `json:"service_id"` - ClientID string `json:"client_id"` - UserID *uuid.UUID `json:"user_id"` - ExpiresAt *time.Time `json:"expires_at"` - LastActive *time.Time `json:"last_active"` - IpAddress pgtype.Text `json:"ip_address"` - UserAgent *string `json:"user_agent"` - AccessTokenID *uuid.UUID `json:"access_token_id"` - RefreshTokenID *uuid.UUID `json:"refresh_token_id"` + ServiceID uuid.UUID `json:"service_id"` + ClientID string `json:"client_id"` + UserID *uuid.UUID `json:"user_id"` + ExpiresAt *time.Time `json:"expires_at"` + LastActive *time.Time `json:"last_active"` + IpAddress *string `json:"ip_address"` + UserAgent *string `json:"user_agent"` + AccessTokenID *uuid.UUID `json:"access_token_id"` + RefreshTokenID *uuid.UUID `json:"refresh_token_id"` } func (q *Queries) CreateServiceSession(ctx context.Context, arg CreateServiceSessionParams) (ServiceSession, error) { diff --git a/internal/repository/user_sessions.sql.go b/internal/repository/user_sessions.sql.go index e8b5cd5..29fedcf 100644 --- a/internal/repository/user_sessions.sql.go +++ b/internal/repository/user_sessions.sql.go @@ -10,7 +10,6 @@ import ( "time" "github.com/google/uuid" - "github.com/jackc/pgx/v5/pgtype" ) const createUserSession = `-- name: CreateUserSession :one @@ -27,15 +26,15 @@ RETURNING id, user_id, session_type, issued_at, expires_at, last_active, ip_addr ` type CreateUserSessionParams struct { - UserID uuid.UUID `json:"user_id"` - SessionType string `json:"session_type"` - ExpiresAt *time.Time `json:"expires_at"` - LastActive *time.Time `json:"last_active"` - IpAddress pgtype.Text `json:"ip_address"` - UserAgent *string `json:"user_agent"` - AccessTokenID *uuid.UUID `json:"access_token_id"` - RefreshTokenID *uuid.UUID `json:"refresh_token_id"` - DeviceInfo []byte `json:"device_info"` + UserID uuid.UUID `json:"user_id"` + SessionType string `json:"session_type"` + ExpiresAt *time.Time `json:"expires_at"` + LastActive *time.Time `json:"last_active"` + IpAddress *string `json:"ip_address"` + UserAgent *string `json:"user_agent"` + AccessTokenID *uuid.UUID `json:"access_token_id"` + RefreshTokenID *uuid.UUID `json:"refresh_token_id"` + DeviceInfo []byte `json:"device_info"` } func (q *Queries) CreateUserSession(ctx context.Context, arg CreateUserSessionParams) (UserSession, error) { @@ -233,3 +232,27 @@ func (q *Queries) UpdateSessionLastActive(ctx context.Context, id uuid.UUID) err _, err := q.db.Exec(ctx, updateSessionLastActive, id) return err } + +const updateSessionTokens = `-- name: UpdateSessionTokens :exec +UPDATE user_sessions +SET access_token_id = $2, refresh_token_id = $3, expires_at = $4 +WHERE id = $1 + AND is_active = TRUE +` + +type UpdateSessionTokensParams struct { + ID uuid.UUID `json:"id"` + AccessTokenID *uuid.UUID `json:"access_token_id"` + RefreshTokenID *uuid.UUID `json:"refresh_token_id"` + ExpiresAt *time.Time `json:"expires_at"` +} + +func (q *Queries) UpdateSessionTokens(ctx context.Context, arg UpdateSessionTokensParams) error { + _, err := q.db.Exec(ctx, updateSessionTokens, + arg.ID, + arg.AccessTokenID, + arg.RefreshTokenID, + arg.ExpiresAt, + ) + return err +} diff --git a/queries/user_sessions.sql b/queries/user_sessions.sql index 0125920..734fe21 100644 --- a/queries/user_sessions.sql +++ b/queries/user_sessions.sql @@ -39,6 +39,12 @@ SET last_active = NOW() WHERE id = $1 AND is_active = TRUE; +-- name: UpdateSessionTokens :exec +UPDATE user_sessions +SET access_token_id = $2, refresh_token_id = $3, expires_at = $4 +WHERE id = $1 + AND is_active = TRUE; + -- name: ListAllSessions :many SELECT * FROM user_sessions ORDER BY issued_at DESC diff --git a/sqlc.yaml b/sqlc.yaml index e43b741..a76e898 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -41,20 +41,39 @@ sql: # ───── text ────────────────────────────────────────── - db_type: "pg_catalog.text" go_type: { type: "string" } - - db_type: "text" # or just "bool" + + - db_type: "text" go_type: { type: "string" } - db_type: "pg_catalog.text" nullable: true go_type: type: "string" - pointer: true # ⇒ *bool for NULLable columns + pointer: true - db_type: "text" nullable: true go_type: type: "string" - pointer: true # ⇒ *bool for NULLable columns + pointer: true + + - db_type: "pg_catalog.varchar" + go_type: { type: "string" } + + - db_type: "varchar" + go_type: { type: "string" } + + - db_type: "pg_catalog.varchar" + nullable: true + go_type: + type: "string" + pointer: true + + - db_type: "varchar" + nullable: true + go_type: + type: "string" + pointer: true # ───── timestamp (WITHOUT TZ) ──────────────────────── - db_type: "pg_catalog.timestamp" # or "timestamp"