Skip to content

Commit

Permalink
fix: postgres driver issues
Browse files Browse the repository at this point in the history
Related: #380
  • Loading branch information
aymanbagabas committed Sep 13, 2023
1 parent 6a42d89 commit fa09286
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 21 deletions.
3 changes: 0 additions & 3 deletions cmd/soft/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
1 change: 1 addition & 0 deletions server/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
15 changes: 5 additions & 10 deletions server/store/database/access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion server/store/database/collab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ?
`)
Expand Down
10 changes: 3 additions & 7 deletions server/store/database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit fa09286

Please sign in to comment.