Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch db driver from lib/pq to pgx #36

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ mock:
mockgen -package mockwk -destination worker/mock/distributor.go github.com/raphaeldiscky/simple-bank/worker TaskDistributor

proto:
rm -f pb/*.go
rm -f doc/swagger/*.go
rm -f pb/*.go
rm -f doc/swagger/*.swagger.json
protoc --proto_path=proto --go_out=pb --go_opt=paths=source_relative \
--go-grpc_out=pb --go-grpc_opt=paths=source_relative \
--grpc-gateway_out=pb --grpc-gateway_opt=paths=source_relative \
--go-grpc_out=pb --go-grpc_opt=paths=source_relative \
--grpc-gateway_out=pb --grpc-gateway_opt=paths=source_relative \
--openapiv2_out=doc/swagger --openapiv2_opt=allow_merge=true,merge_file_name=simple_bank \
proto/*.proto
statik -src=./doc/swagger -dest=./doc -f
statik -src=./doc/swagger -dest=./doc

evans:
evans --host localhost --port 9090 -r repl
Expand Down
14 changes: 5 additions & 9 deletions api/account.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package api

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

"github.com/gin-gonic/gin"
"github.com/lib/pq"
db "github.com/raphaeldiscky/simple-bank/db/sqlc"
"github.com/raphaeldiscky/simple-bank/token"
)
Expand All @@ -32,12 +30,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 @@ -60,7 +56,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
2 changes: 1 addition & 1 deletion api/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,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
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/raphaeldiscky/simple-bank/db/sqlc"
)

type renewAccessTokenRequest struct {
Expand Down Expand Up @@ -34,7 +35,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 @@ -61,7 +60,7 @@ func (server *Server) createTransfer(ctx *gin.Context) {
func (server *Server) validAccount(ctx *gin.Context, accountID int64, currency string) (db.Account, bool) {
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: 6 additions & 9 deletions api/user.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package api

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

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/lib/pq"

db "github.com/raphaeldiscky/simple-bank/db/sqlc"
"github.com/raphaeldiscky/simple-bank/util"
)
Expand Down Expand Up @@ -61,12 +61,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 @@ -99,7 +96,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
1 change: 1 addition & 0 deletions api/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ func TestLoginUserAPI(t *testing.T) {
require.Equal(t, http.StatusInternalServerError, recorder.Code)
},
},

{
name: "InvalidUsername",
body: gin.H{
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:secret@localhost:5432/simple_bank?sslmode=disable
MIGRATION_URL=file://db/migration
HTTP_SERVER_ADDRESS=127.0.0.1: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.

15 changes: 7 additions & 8 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 @@ -19,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 @@ -38,7 +37,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 @@ -57,7 +56,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 @@ -70,12 +69,12 @@ 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)
}

Expand All @@ -91,7 +90,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.

9 changes: 3 additions & 6 deletions db/sqlc/entry.sql.go

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

Loading
Loading