Skip to content

Commit

Permalink
Merge pull request #20 from imrishuroy/pgx
Browse files Browse the repository at this point in the history
switch db driver from lib/pq to pgx
  • Loading branch information
imrishuroy authored Dec 8, 2023
2 parents d74ef6f + 215fae4 commit 8ecae33
Show file tree
Hide file tree
Showing 35 changed files with 274 additions and 182 deletions.
15 changes: 5 additions & 10 deletions api/account.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package api

import (
"database/sql"
"errors"
"net/http"

"github.com/gin-gonic/gin"
db "github.com/imrishuroy/simplebank/db/sqlc"
"github.com/imrishuroy/simplebank/token"
"github.com/lib/pq"
)

type createAccountRequest struct {
Expand All @@ -33,13 +31,10 @@ func (server *Server) createAccount(ctx *gin.Context) {

account, err := server.store.CreateAccount(ctx, arg)
if err != nil {

if pqErr, ok := err.(*pq.Error); ok {
switch pqErr.Code.Name() {
case "foreign_key_violation", "unique_violation":
ctx.JSON(http.StatusForbidden, errorResponse(err))
return
}
errCode := db.ErrorCode(err)
if errCode == db.ForeignKeyViolation || errCode == db.UniqueViolation {
ctx.JSON(http.StatusForbidden, errorResponse(err))
return
}
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
Expand All @@ -63,7 +58,7 @@ func (server *Server) getAccount(ctx *gin.Context) {
account, err := server.store.GetAccount(ctx, req.ID)

if err != nil {
if err == sql.ErrNoRows {
if errors.Is(err, db.ErrRecordNotFound) {
ctx.JSON(http.StatusNotFound, errorResponse(err))
return
}
Expand Down
9 changes: 5 additions & 4 deletions api/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"io"

"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -89,7 +90,7 @@ func TestGetAccountAPI(t *testing.T) {
store.EXPECT().
GetAccount(gomock.Any(), gomock.Eq(account.ID)).
Times(1).
Return(db.Account{}, sql.ErrNoRows)
Return(db.Account{}, db.ErrRecordNotFound)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusNotFound, recorder.Code)
Expand Down Expand Up @@ -430,7 +431,7 @@ func randomAccount(owner string) db.Account {
}

func requireBodyMatchAccount(t *testing.T, body *bytes.Buffer, account db.Account) {
data, err := ioutil.ReadAll(body)
data, err := io.ReadAll(body)
require.NoError(t, err)

var gotAccount db.Account
Expand All @@ -440,7 +441,7 @@ func requireBodyMatchAccount(t *testing.T, body *bytes.Buffer, account db.Accoun
}

func requireBodyMatchAccounts(t *testing.T, body *bytes.Buffer, accounts []db.Account) {
data, err := ioutil.ReadAll(body)
data, err := io.ReadAll(body)
require.NoError(t, err)

var gotAccounts []db.Account
Expand Down
5 changes: 3 additions & 2 deletions api/token.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package api

import (
"database/sql"
"errors"
"fmt"
"net/http"
"time"

"github.com/gin-gonic/gin"
db "github.com/imrishuroy/simplebank/db/sqlc"
)

type renewAccessTokenRequest struct {
Expand All @@ -33,7 +34,7 @@ func (server *Server) renewAccessToken(ctx *gin.Context) {

session, err := server.store.GetSession(ctx, refreshPayload.ID)
if err != nil {
if err == sql.ErrNoRows {
if errors.Is(err, db.ErrRecordNotFound) {
ctx.JSON(http.StatusNotFound, errorResponse(err))
return
}
Expand Down
3 changes: 1 addition & 2 deletions api/transfer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package api

import (
"database/sql"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -62,7 +61,7 @@ func (server *Server) validAccount(ctx *gin.Context, accountId int64, currency s

account, err := server.store.GetAccount(ctx, accountId)
if err != nil {
if err == sql.ErrNoRows {
if errors.Is(err, db.ErrRecordNotFound) {
ctx.JSON(http.StatusNotFound, errorResponse(err))
return account, false
}
Expand Down
4 changes: 2 additions & 2 deletions api/transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestTransferAPI(t *testing.T) {
addAuthorization(t, request, tokenMaker, authorizationTypeBearer, user1.Username, time.Minute)
},
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account1.ID)).Times(1).Return(db.Account{}, sql.ErrNoRows)
store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account1.ID)).Times(1).Return(db.Account{}, db.ErrRecordNotFound)
store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account2.ID)).Times(0)
store.EXPECT().TransferTx(gomock.Any(), gomock.Any()).Times(0)
},
Expand All @@ -137,7 +137,7 @@ func TestTransferAPI(t *testing.T) {
},
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account1.ID)).Times(1).Return(account1, nil)
store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account2.ID)).Times(1).Return(db.Account{}, sql.ErrNoRows)
store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account2.ID)).Times(1).Return(db.Account{}, db.ErrRecordNotFound)
store.EXPECT().TransferTx(gomock.Any(), gomock.Any()).Times(0)
},
checkResponse: func(recorder *httptest.ResponseRecorder) {
Expand Down
15 changes: 5 additions & 10 deletions api/user.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package api

import (
"database/sql"
"errors"
"net/http"
"time"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
db "github.com/imrishuroy/simplebank/db/sqlc"
"github.com/imrishuroy/simplebank/util"
"github.com/lib/pq"
)

type createUserRequest struct {
Expand Down Expand Up @@ -58,13 +57,9 @@ func (server *Server) createUser(ctx *gin.Context) {

user, err := server.store.CreateUser(ctx, arg)
if err != nil {

if pqErr, ok := err.(*pq.Error); ok {
switch pqErr.Code.Name() {
case "unique_violation":
ctx.JSON(http.StatusForbidden, errorResponse(err))
return
}
if db.ErrorCode(err) == db.UniqueViolation {
ctx.JSON(http.StatusForbidden, errorResponse(err))
return
}
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
Expand Down Expand Up @@ -97,7 +92,7 @@ func (server *Server) loginUser(ctx *gin.Context) {
}
user, err := server.store.GetUser(ctx, req.Username)
if err != nil {
if err == sql.ErrNoRows {
if errors.Is(err, db.ErrRecordNotFound) {
ctx.JSON(http.StatusNotFound, errorResponse(err))
return
}
Expand Down
4 changes: 2 additions & 2 deletions api/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
mockdb "github.com/imrishuroy/simplebank/db/mock"
db "github.com/imrishuroy/simplebank/db/sqlc"
"github.com/imrishuroy/simplebank/util"
"github.com/lib/pq"

"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -111,7 +111,7 @@ func TestCreateUserAPI(t *testing.T) {
store.EXPECT().
CreateUser(gomock.Any(), gomock.Any()).
Times(1).
Return(db.User{}, &pq.Error{Code: "23505"})
Return(db.User{}, db.ErrUniqueViolation)
},
checkResponse: func(recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusForbidden, recorder.Code)
Expand Down
1 change: 0 additions & 1 deletion app.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
ENVIRONMENT=development
DB_DRIVER=postgres
DB_SOURCE=postgresql://root:Prince2024@localhost:5432/simple_bank?sslmode=disable
MIGRATION_URL=file://db/migration
HTTP_SERVER_ADDRESS=localhost:8080
Expand Down
17 changes: 7 additions & 10 deletions db/sqlc/account.sql.go

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

17 changes: 7 additions & 10 deletions db/sqlc/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package db

import (
"context"
"database/sql"
"testing"
"time"

Expand All @@ -11,7 +10,6 @@ import (
)

func createRandomAccount(t *testing.T) Account {

user := createRandomUser(t)

arg := CreateAccountParams{
Expand All @@ -20,7 +18,7 @@ func createRandomAccount(t *testing.T) Account {
Currency: util.RandomCurrency(),
}

account, err := testQueries.CreateAccount(context.Background(), arg)
account, err := testStore.CreateAccount(context.Background(), arg)
require.NoError(t, err)
require.NotEmpty(t, account)

Expand All @@ -40,7 +38,7 @@ func TestCreateAccount(t *testing.T) {

func TestGetAccount(t *testing.T) {
account1 := createRandomAccount(t)
account2, err := testQueries.GetAccount(context.Background(), account1.ID)
account2, err := testStore.GetAccount(context.Background(), account1.ID)
require.NoError(t, err)
require.NotEmpty(t, account2)

Expand All @@ -59,7 +57,7 @@ func TestUpdateAccount(t *testing.T) {
Balance: util.RandomMoney(),
}

account2, err := testQueries.UpdateAccount(context.Background(), arg)
account2, err := testStore.UpdateAccount(context.Background(), arg)
require.NoError(t, err)
require.NotEmpty(t, account2)

Expand All @@ -72,18 +70,17 @@ func TestUpdateAccount(t *testing.T) {

func TestDeleteAccount(t *testing.T) {
account1 := createRandomAccount(t)
err := testQueries.DeleteAccount(context.Background(), account1.ID)
err := testStore.DeleteAccount(context.Background(), account1.ID)
require.NoError(t, err)

account2, err := testQueries.GetAccount(context.Background(), account1.ID)
account2, err := testStore.GetAccount(context.Background(), account1.ID)
require.Error(t, err)
require.EqualError(t, err, sql.ErrNoRows.Error())
require.EqualError(t, err, ErrRecordNotFound.Error())
require.Empty(t, account2)
}

func TestListAccounts(t *testing.T) {
var lastAccount Account

for i := 0; i < 10; i++ {
lastAccount = createRandomAccount(t)
}
Expand All @@ -94,7 +91,7 @@ func TestListAccounts(t *testing.T) {
Offset: 0,
}

accounts, err := testQueries.ListAccounts(context.Background(), arg)
accounts, err := testStore.ListAccounts(context.Background(), arg)
require.NoError(t, err)
require.NotEmpty(t, accounts)

Expand Down
13 changes: 7 additions & 6 deletions db/sqlc/db.go

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

Loading

0 comments on commit 8ecae33

Please # to comment.