Skip to content

Commit

Permalink
feat: use pgxpool
Browse files Browse the repository at this point in the history
  • Loading branch information
clstb committed Feb 25, 2022
1 parent c6e49e8 commit 3b0d447
Show file tree
Hide file tree
Showing 32 changed files with 150 additions and 440 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/gofrs/uuid v4.2.0+incompatible
github.com/golang-migrate/migrate/v4 v4.15.1
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/jackc/pgconn v1.11.0
github.com/jackc/pgx/v4 v4.15.0
github.com/lithammer/fuzzysearch v1.1.3
github.com/shopspring/decimal v1.3.1
Expand All @@ -31,12 +32,12 @@ require (
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.11.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.2.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.10.0 // indirect
github.com/jackc/puddle v1.2.1 // indirect
github.com/lib/pq v1.10.2 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0f
github.com/jackc/puddle v1.1.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.2.1 h1:gI8os0wpRXFd4FiAY2dWiqRK037tjj3t7rKFeO4X5iw=
github.com/jackc/puddle v1.2.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
Expand Down
1 change: 1 addition & 0 deletions go/cmd/auth/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ go_library(
"//go/pkg/util",
"@com_github_golang_migrate_migrate_v4//source/file",
"@com_github_grpc_ecosystem_go_grpc_middleware//:go-grpc-middleware",
"@com_github_jackc_pgx_v4//pgxpool",
"@com_github_jackc_pgx_v4//stdlib",
"@com_github_urfave_cli_v2//:cli",
"@org_golang_google_grpc//:go_default_library",
Expand Down
4 changes: 2 additions & 2 deletions go/cmd/auth/migrate.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package main

import (
"database/sql"
"fmt"

authdb "github.com/clstb/phi/go/pkg/auth/db"
"github.com/clstb/phi/go/pkg/util"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/jackc/pgx/v4/pgxpool"
_ "github.com/jackc/pgx/v4/stdlib"
"github.com/urfave/cli/v2"
)

func Migrate(ctx *cli.Context) error {
db, ok := ctx.Context.Value("db").(*sql.DB)
db, ok := ctx.Context.Value("db").(*pgxpool.Pool)
if !ok {
return fmt.Errorf("missing db")
}
Expand Down
4 changes: 2 additions & 2 deletions go/cmd/auth/server.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package main

import (
"database/sql"
"fmt"

pb "github.com/clstb/phi/go/pkg/auth/pb"
"github.com/clstb/phi/go/pkg/auth/server"
"github.com/clstb/phi/go/pkg/interceptor"
"github.com/clstb/phi/go/pkg/util"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
"google.golang.org/grpc"
)

func Server(ctx *cli.Context) error {
db, ok := ctx.Context.Value("db").(*sql.DB)
db, ok := ctx.Context.Value("db").(*pgxpool.Pool)
if !ok {
return fmt.Errorf("missing db")
}
Expand Down
1 change: 1 addition & 0 deletions go/cmd/tinkgw/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ go_library(
"//go/pkg/util",
"@com_github_golang_migrate_migrate_v4//source/file",
"@com_github_grpc_ecosystem_go_grpc_middleware//:go-grpc-middleware",
"@com_github_jackc_pgx_v4//pgxpool",
"@com_github_jackc_pgx_v4//stdlib",
"@com_github_urfave_cli_v2//:cli",
"@org_golang_google_grpc//:go_default_library",
Expand Down
4 changes: 2 additions & 2 deletions go/cmd/tinkgw/migrate.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package main

import (
"database/sql"
"fmt"

tinkgwdb "github.com/clstb/phi/go/pkg/tinkgw/db"
"github.com/clstb/phi/go/pkg/util"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/jackc/pgx/v4/pgxpool"
_ "github.com/jackc/pgx/v4/stdlib"
"github.com/urfave/cli/v2"
)

func Migrate(ctx *cli.Context) error {
db, ok := ctx.Context.Value("db").(*sql.DB)
db, ok := ctx.Context.Value("db").(*pgxpool.Pool)
if !ok {
return fmt.Errorf("missing db")
}
Expand Down
4 changes: 2 additions & 2 deletions go/cmd/tinkgw/server.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package main

import (
"database/sql"
"fmt"

"github.com/clstb/phi/go/pkg/interceptor"
pb "github.com/clstb/phi/go/pkg/tinkgw/pb"
"github.com/clstb/phi/go/pkg/tinkgw/server"
"github.com/clstb/phi/go/pkg/util"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
"google.golang.org/grpc"
)

func Server(ctx *cli.Context) error {
db, ok := ctx.Context.Value("db").(*sql.DB)
db, ok := ctx.Context.Value("db").(*pgxpool.Pool)
if !ok {
return fmt.Errorf("missing db")
}
Expand Down
6 changes: 5 additions & 1 deletion go/pkg/auth/db/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ go_library(
],
importpath = "github.com/clstb/phi/go/pkg/auth/db",
visibility = ["//visibility:public"],
deps = ["@com_github_gofrs_uuid//:uuid"],
deps = [
"@com_github_gofrs_uuid//:uuid",
"@com_github_jackc_pgconn//:pgconn",
"@com_github_jackc_pgx_v4//:pgx",
],
)
94 changes: 9 additions & 85 deletions go/pkg/auth/db/db.go

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

1 change: 1 addition & 0 deletions go/pkg/auth/db/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ packages:
queries: "./query"
schema: "./schema"
engine: "postgresql"
sql_package: "pgx/v4"
emit_json_tags: true
emit_prepared_queries: true
emit_interface: true
Expand Down
6 changes: 3 additions & 3 deletions go/pkg/auth/db/users.sql.go

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

1 change: 1 addition & 0 deletions go/pkg/auth/server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go_library(
"//go/pkg/auth/db",
"//go/pkg/auth/pb",
"@com_github_dgrijalva_jwt_go//:jwt-go",
"@com_github_jackc_pgx_v4//:pgx",
"@org_golang_google_grpc//codes",
"@org_golang_google_grpc//status",
"@org_golang_x_crypto//bcrypt",
Expand Down
4 changes: 2 additions & 2 deletions go/pkg/auth/server/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package server

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

"github.com/clstb/phi/go/pkg/auth/db"
"github.com/clstb/phi/go/pkg/auth/pb"
"github.com/dgrijalva/jwt-go"
"github.com/jackc/pgx/v4"
"golang.org/x/crypto/bcrypt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (s *Server) Login(ctx context.Context, user *pb.User) (*pb.Token, error) {
tx, ok := ctx.Value("tx").(*sql.Tx)
tx, ok := ctx.Value("tx").(pgx.Tx)
if !ok {
return nil, status.Error(codes.Internal, "missing tx")
}
Expand Down
4 changes: 2 additions & 2 deletions go/pkg/auth/server/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package server

import (
"context"
"database/sql"

"github.com/clstb/phi/go/pkg/auth/db"
"github.com/clstb/phi/go/pkg/auth/pb"
"github.com/jackc/pgx/v4"
"golang.org/x/crypto/bcrypt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (s *Server) Register(ctx context.Context, user *pb.User) (*pb.Token, error) {
tx, ok := ctx.Value("tx").(*sql.Tx)
tx, ok := ctx.Value("tx").(pgx.Tx)
if !ok {
return nil, status.Error(codes.Internal, "missing tx")
}
Expand Down
5 changes: 4 additions & 1 deletion go/pkg/auth/server/server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server_test

import (
"context"
"log"
"os"
"testing"
Expand All @@ -10,7 +11,9 @@ import (
)

func TestMain(t *testing.M) {
db, err := util.TestDB(os.Getenv("DATABASE_URL"), "phi_auth")
ctx := context.Background()

db, err := util.TestDB(ctx, os.Getenv("DATABASE_URL"), "phi_auth")
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 2 additions & 0 deletions go/pkg/interceptor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ go_library(
"@com_github_gofrs_uuid//:uuid",
"@com_github_grpc_ecosystem_go_grpc_middleware//:go-grpc-middleware",
"@com_github_grpc_ecosystem_go_grpc_middleware//auth",
"@com_github_jackc_pgx_v4//:pgx",
"@com_github_jackc_pgx_v4//pgxpool",
"@org_golang_google_grpc//:go_default_library",
"@org_golang_google_grpc//metadata",
],
Expand Down
Loading

0 comments on commit 3b0d447

Please sign in to comment.