Skip to content

Commit 3a75ae7

Browse files
feat: use google/uuid in sqlc
1 parent ed0c148 commit 3a75ae7

File tree

12 files changed

+58
-67
lines changed

12 files changed

+58
-67
lines changed

.github/workflows/test.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ jobs:
1818
postgres:
1919
image: postgres
2020
env:
21-
POSTGRES_PASSWORD: test
22-
POSTGRES_USER: test
23-
POSTGRES_DB: test
21+
POSTGRES_PASSWORD: local
22+
POSTGRES_USER: local
23+
POSTGRES_DB: local
2424
options: >-
2525
--health-cmd pg_isready
2626
--health-interval 10s

.test.env

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
DATABASE_URI="postgresql://test:test@localhost:5432/test?sslmode=disable"
1+
DATABASE_URI="postgresql://local:local@localhost:5432/local?sslmode=disable"
22

authentication/internal/query/models.go

+11-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

authentication/internal/query/password_query.sql.go

+9-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

authentication/internal/query/query.sql.go

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

authentication/internal/query/session_query.sql.go

+10-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

authentication/password/handler.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (h *Handler[T]) handleUserRegistrationTx(
141141
uid := uuidv7.Must()
142142
q := tx.Queries()
143143
if err := q.CreateUser(ctx, query.CreateUserParams{
144-
ID: uuidv7.ToPgxUUID(uid),
144+
ID: uid,
145145
Email: email,
146146
}); err != nil {
147147
if dbutil.IsUniqueViolationError(err) {
@@ -152,8 +152,8 @@ func (h *Handler[T]) handleUserRegistrationTx(
152152
}
153153

154154
if err := q.CreateUserPasswordCredential(ctx, query.CreateUserPasswordCredentialParams{
155-
ID: uuidv7.ToPgxUUID(uuidv7.Must()),
156-
UserID: uuidv7.ToPgxUUID(uid),
155+
ID: uuidv7.Must(),
156+
UserID: uid,
157157
UserCredentialKey: email,
158158
UserCredentialSecret: passwordHash,
159159
}); err != nil {
@@ -197,13 +197,11 @@ func (h *Handler[T]) HandleUserLogin(
197197
return nil, authentication.ErrUserNotFound
198198
}
199199

200-
uid := uuidv7.MustFromPgxUUID(user.ID)
201-
202200
// An entry point for hijacking the user login process.
203201
var payload T
204202
if h.config.Hijacker != nil {
205203
d("login hijacking is enabled, trying to get payload")
206-
payload, err = h.config.Hijacker.HijackUserLogin(ctx, uid, tx.Tx())
204+
payload, err = h.config.Hijacker.HijackUserLogin(ctx, user.ID, tx.Tx())
207205
if err != nil {
208206
return nil, fmt.Errorf(
209207
"authentication/password: failed to hijack user login: %w",
@@ -228,7 +226,7 @@ func (h *Handler[T]) HandleUserLogin(
228226
}
229227

230228
return &strategy.User[T]{
231-
ID: uid,
229+
ID: user.ID,
232230
T: payload,
233231
}, nil
234232
}

authentication/passwordreset/handler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func (h *Handler) HandlePasswordReset(
124124

125125
tokStr := must.Must(random.SecureHexString(h.config.TokenLength))
126126
tok, err := q.UpsertPasswordResetToken(ctx, query.UpsertPasswordResetTokenParams{
127-
ID: uuidv7.ToPgxUUID(uuidv7.Must()),
127+
ID: uuidv7.Must(),
128128
Token: tokStr,
129129
UserID: user.ID,
130130
ExpiresAt: pgtype.Timestamp{

authentication/sqlc.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@ sql:
1212
sql_package: "pgx/v5"
1313
out: "internal/query"
1414
emit_pointers_for_null_types: true
15+
overrides:
16+
- db_type: "uuid"
17+
go_type: "github.com/google/uuid.UUID"
18+
- db_type: "uuid"
19+
nullable: true
20+
go_type: "github.com/google/uuid.UUID"

authentication/strategy/session/logout.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"go.inout.gg/common/authentication/user"
99
"go.inout.gg/common/http/cookie"
1010
httperror "go.inout.gg/common/http/error"
11-
"go.inout.gg/common/internal/uuidv7"
1211
)
1312

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

33-
if _, err := q.ExpireSessionByID(ctx, uuidv7.ToPgxUUID(usr.ID)); err != nil {
32+
if _, err := q.ExpireSessionByID(ctx, usr.ID); err != nil {
3433
return httperror.FromError(err, http.StatusInternalServerError)
3534
}
3635

authentication/strategy/session/session.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ func (s *sessionStrategy[T]) Issue(
6363
sessionID := uuidv7.Must()
6464
expiresAt := time.Now().Add(s.config.ExpiresIn)
6565
_, err := q.CreateUserSession(ctx, query.CreateUserSessionParams{
66-
ID: uuidv7.ToPgxUUID(sessionID),
67-
UserID: uuidv7.ToPgxUUID(user.ID),
66+
ID: sessionID,
67+
UserID: user.ID,
6868
ExpiresAt: pgtype.Timestamp{Time: expiresAt, Valid: true},
6969
})
7070
if err != nil {
@@ -116,7 +116,7 @@ func (s *sessionStrategy[T]) Authenticate(
116116

117117
q := tx.Queries()
118118

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

145145
return &strategy.Session[T]{
146-
ID: uuidv7.MustFromPgxUUID(session.ID),
146+
ID: session.ID,
147147
ExpiresAt: session.ExpiresAt.Time,
148148
T: nil,
149149
}, nil

internal/uuidv7/uuidv7.go

-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package uuidv7
33

44
import (
55
"github.com/google/uuid"
6-
"github.com/jackc/pgx/v5/pgtype"
76
"go.inout.gg/common/must"
87
)
98

@@ -16,17 +15,3 @@ func Must() uuid.UUID {
1615
func FromString(s string) (uuid.UUID, error) {
1716
return uuid.Parse(s)
1817
}
19-
20-
// ToPgxUUID converts a UUID to a pgx.UUID.
21-
func ToPgxUUID(u uuid.UUID) pgtype.UUID {
22-
return pgtype.UUID{Bytes: u, Valid: true}
23-
}
24-
25-
// FromPgxUUID converts a pgx.UUID to a UUID.
26-
func FromPgxUUID(u pgtype.UUID) (uuid.UUID, error) {
27-
return uuid.FromBytes(u.Bytes[:])
28-
}
29-
30-
func MustFromPgxUUID(u pgtype.UUID) uuid.UUID {
31-
return must.Must(FromPgxUUID(u))
32-
}

0 commit comments

Comments
 (0)