Skip to content

Commit

Permalink
feat: use google/uuid in sqlc
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-vanesyan committed Jul 13, 2024
1 parent ed0c148 commit 3a75ae7
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 67 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ jobs:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: test
POSTGRES_USER: test
POSTGRES_DB: test
POSTGRES_PASSWORD: local
POSTGRES_USER: local
POSTGRES_DB: local
options: >-
--health-cmd pg_isready
--health-interval 10s
Expand Down
2 changes: 1 addition & 1 deletion .test.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DATABASE_URI="postgresql://test:test@localhost:5432/test?sslmode=disable"
DATABASE_URI="postgresql://local:local@localhost:5432/local?sslmode=disable"

21 changes: 11 additions & 10 deletions authentication/internal/query/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions authentication/internal/query/password_query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions authentication/internal/query/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions authentication/internal/query/session_query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 5 additions & 7 deletions authentication/password/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (h *Handler[T]) handleUserRegistrationTx(
uid := uuidv7.Must()
q := tx.Queries()
if err := q.CreateUser(ctx, query.CreateUserParams{
ID: uuidv7.ToPgxUUID(uid),
ID: uid,
Email: email,
}); err != nil {
if dbutil.IsUniqueViolationError(err) {
Expand All @@ -152,8 +152,8 @@ func (h *Handler[T]) handleUserRegistrationTx(
}

if err := q.CreateUserPasswordCredential(ctx, query.CreateUserPasswordCredentialParams{
ID: uuidv7.ToPgxUUID(uuidv7.Must()),
UserID: uuidv7.ToPgxUUID(uid),
ID: uuidv7.Must(),
UserID: uid,
UserCredentialKey: email,
UserCredentialSecret: passwordHash,
}); err != nil {
Expand Down Expand Up @@ -197,13 +197,11 @@ func (h *Handler[T]) HandleUserLogin(
return nil, authentication.ErrUserNotFound
}

uid := uuidv7.MustFromPgxUUID(user.ID)

// An entry point for hijacking the user login process.
var payload T
if h.config.Hijacker != nil {
d("login hijacking is enabled, trying to get payload")
payload, err = h.config.Hijacker.HijackUserLogin(ctx, uid, tx.Tx())
payload, err = h.config.Hijacker.HijackUserLogin(ctx, user.ID, tx.Tx())
if err != nil {
return nil, fmt.Errorf(
"authentication/password: failed to hijack user login: %w",
Expand All @@ -228,7 +226,7 @@ func (h *Handler[T]) HandleUserLogin(
}

return &strategy.User[T]{
ID: uid,
ID: user.ID,
T: payload,
}, nil
}
2 changes: 1 addition & 1 deletion authentication/passwordreset/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (h *Handler) HandlePasswordReset(

tokStr := must.Must(random.SecureHexString(h.config.TokenLength))
tok, err := q.UpsertPasswordResetToken(ctx, query.UpsertPasswordResetTokenParams{
ID: uuidv7.ToPgxUUID(uuidv7.Must()),
ID: uuidv7.Must(),
Token: tokStr,
UserID: user.ID,
ExpiresAt: pgtype.Timestamp{
Expand Down
6 changes: 6 additions & 0 deletions authentication/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ sql:
sql_package: "pgx/v5"
out: "internal/query"
emit_pointers_for_null_types: true
overrides:
- db_type: "uuid"
go_type: "github.com/google/uuid.UUID"
- db_type: "uuid"
nullable: true
go_type: "github.com/google/uuid.UUID"
3 changes: 1 addition & 2 deletions authentication/strategy/session/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"go.inout.gg/common/authentication/user"
"go.inout.gg/common/http/cookie"
httperror "go.inout.gg/common/http/error"
"go.inout.gg/common/internal/uuidv7"
)

// LogoutHandler is a handler that logs out the user and deletes the session.
Expand All @@ -30,7 +29,7 @@ func (h *LogoutHandler) HandleLogout(w http.ResponseWriter, r *http.Request) err
return httperror.FromError(authentication.ErrUnauthorizedUser, http.StatusUnauthorized)
}

if _, err := q.ExpireSessionByID(ctx, uuidv7.ToPgxUUID(usr.ID)); err != nil {
if _, err := q.ExpireSessionByID(ctx, usr.ID); err != nil {
return httperror.FromError(err, http.StatusInternalServerError)
}

Expand Down
8 changes: 4 additions & 4 deletions authentication/strategy/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func (s *sessionStrategy[T]) Issue(
sessionID := uuidv7.Must()
expiresAt := time.Now().Add(s.config.ExpiresIn)
_, err := q.CreateUserSession(ctx, query.CreateUserSessionParams{
ID: uuidv7.ToPgxUUID(sessionID),
UserID: uuidv7.ToPgxUUID(user.ID),
ID: sessionID,
UserID: user.ID,
ExpiresAt: pgtype.Timestamp{Time: expiresAt, Valid: true},
})
if err != nil {
Expand Down Expand Up @@ -116,7 +116,7 @@ func (s *sessionStrategy[T]) Authenticate(

q := tx.Queries()

session, err := q.FindUserSessionByID(ctx, uuidv7.ToPgxUUID(sessionID))
session, err := q.FindUserSessionByID(ctx, sessionID)
if err != nil {
if dbutil.IsNotFoundError(err) {
s.config.Logger.Error(
Expand All @@ -143,7 +143,7 @@ func (s *sessionStrategy[T]) Authenticate(
}

return &strategy.Session[T]{
ID: uuidv7.MustFromPgxUUID(session.ID),
ID: session.ID,
ExpiresAt: session.ExpiresAt.Time,
T: nil,
}, nil
Expand Down
15 changes: 0 additions & 15 deletions internal/uuidv7/uuidv7.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package uuidv7

import (
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"go.inout.gg/common/must"
)

Expand All @@ -16,17 +15,3 @@ func Must() uuid.UUID {
func FromString(s string) (uuid.UUID, error) {
return uuid.Parse(s)
}

// ToPgxUUID converts a UUID to a pgx.UUID.
func ToPgxUUID(u uuid.UUID) pgtype.UUID {
return pgtype.UUID{Bytes: u, Valid: true}
}

// FromPgxUUID converts a pgx.UUID to a UUID.
func FromPgxUUID(u pgtype.UUID) (uuid.UUID, error) {
return uuid.FromBytes(u.Bytes[:])
}

func MustFromPgxUUID(u pgtype.UUID) uuid.UUID {
return must.Must(FromPgxUUID(u))
}

0 comments on commit 3a75ae7

Please sign in to comment.