From fa0928691b5f088477ac08bb71277116e202df14 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Wed, 13 Sep 2023 12:07:55 -0400 Subject: [PATCH] fix: postgres driver issues Related: https://github.com/charmbracelet/soft-serve/issues/380 --- cmd/soft/root.go | 3 --- server/db/db.go | 1 + server/store/database/access_token.go | 15 +++++---------- server/store/database/collab.go | 2 +- server/store/database/user.go | 10 +++------- 5 files changed, 10 insertions(+), 21 deletions(-) diff --git a/cmd/soft/root.go b/cmd/soft/root.go index 329ec143e..31a1f7803 100644 --- a/cmd/soft/root.go +++ b/cmd/soft/root.go @@ -15,11 +15,8 @@ import ( logr "github.com/charmbracelet/soft-serve/server/log" "github.com/charmbracelet/soft-serve/server/store" "github.com/charmbracelet/soft-serve/server/store/database" - _ "github.com/lib/pq" // postgres driver "github.com/spf13/cobra" "go.uber.org/automaxprocs/maxprocs" - - _ "modernc.org/sqlite" // sqlite driver ) var ( diff --git a/server/db/db.go b/server/db/db.go index 587880dc6..ae0bfcaae 100644 --- a/server/db/db.go +++ b/server/db/db.go @@ -9,6 +9,7 @@ import ( "github.com/charmbracelet/log" "github.com/charmbracelet/soft-serve/server/config" "github.com/jmoiron/sqlx" + _ "github.com/lib/pq" // postgres driver _ "modernc.org/sqlite" // sqlite driver ) diff --git a/server/store/database/access_token.go b/server/store/database/access_token.go index 0ba0d822f..16f72b6aa 100644 --- a/server/store/database/access_token.go +++ b/server/store/database/access_token.go @@ -16,24 +16,19 @@ var _ store.AccessTokenStore = (*accessTokenStore)(nil) // CreateAccessToken implements store.AccessTokenStore. func (s *accessTokenStore) CreateAccessToken(ctx context.Context, h db.Handler, name string, userID int64, token string, expiresAt time.Time) (models.AccessToken, error) { queryWithoutExpires := `INSERT INTO access_tokens (name, user_id, token, created_at, updated_at) - VALUES (?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)` + VALUES (?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) RETURNING id` queryWithExpires := `INSERT INTO access_tokens (name, user_id, token, expires_at, created_at, updated_at) - VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)` + VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) RETURNING id` query := queryWithoutExpires values := []interface{}{name, userID, token} if !expiresAt.IsZero() { query = queryWithExpires - values = append(values, expiresAt) + values = append(values, expiresAt.UTC()) } - result, err := h.ExecContext(ctx, query, values...) - if err != nil { - return models.AccessToken{}, err - } - - id, err := result.LastInsertId() - if err != nil { + var id int64 + if err := h.GetContext(ctx, &id, h.Rebind(query), values...); err != nil { return models.AccessToken{}, err } diff --git a/server/store/database/collab.go b/server/store/database/collab.go index e93044e01..ea4e24a2f 100644 --- a/server/store/database/collab.go +++ b/server/store/database/collab.go @@ -93,8 +93,8 @@ func (*collabStore) ListCollabsByRepoAsUsers(ctx context.Context, tx db.Handler, users.* FROM users - INNER JOIN repos ON repos.id = collabs.repo_id INNER JOIN collabs ON collabs.user_id = users.id + INNER JOIN repos ON repos.id = collabs.repo_id WHERE repos.name = ? `) diff --git a/server/store/database/user.go b/server/store/database/user.go index 5f00f45b7..95572010d 100644 --- a/server/store/database/user.go +++ b/server/store/database/user.go @@ -44,14 +44,10 @@ func (*userStore) CreateUser(ctx context.Context, tx db.Handler, username string } query := tx.Rebind(`INSERT INTO users (username, admin, updated_at) - VALUES (?, ?, CURRENT_TIMESTAMP);`) - result, err := tx.ExecContext(ctx, query, username, isAdmin) - if err != nil { - return err - } + VALUES (?, ?, CURRENT_TIMESTAMP) RETURNING id;`) - userID, err := result.LastInsertId() - if err != nil { + var userID int64 + if err := tx.GetContext(ctx, &userID, query, username, isAdmin); err != nil { return err }