Skip to content

Commit

Permalink
Switch database driver from pq to pgx.
Browse files Browse the repository at this point in the history
  • Loading branch information
zyro committed Jun 21, 2019
1 parent 2178c4c commit 2c13ff5
Show file tree
Hide file tree
Showing 283 changed files with 67,293 additions and 7,166 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr
- Update GRPC (1.21.1), GRPC-Gateway (1.9.2), Protobuf (1.3.1), Mux (1.7.2), and OpenCensus (0.22.0) dependencies.
- Use Go 1.12.6 as base Docker container image and native builds.
- Move from dep to Go modules for dependency management.
- Switch database driver from pq to pgx.

### Fixed
- Fix delayed first time invocation of tournament and leaderboard callbacks.
Expand Down
18 changes: 0 additions & 18 deletions ga/ga.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,6 @@ type Event struct {
Ev string // event value
}

// AppInfo represents a mobile app info GA event.
type AppInfo struct {
An string // app name
Aid string // app identifier
Av string // app version
Aiid string // app installer identifier
}

// SendAppInfo will send the AppInfo struct to GA over HTTP.
func SendAppInfo(httpc *http.Client, gacode string, cookie string, app *AppInfo) error {
values := url.Values{}
values.Add("an", app.An)
values.Add("av", app.Av)
values.Add("aid", app.Aid)
values.Add("aiid", app.Aiid)
return SendValues(httpc, gacode, cookie, values)
}

// SendEvent will send the event struct to GA over HTTP.
func SendEvent(httpc *http.Client, gacode string, cookie string, event *Event) error {
if len(event.Ec) < 1 || len(event.Ea) < 1 {
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/blevesearch/blevex v0.0.0-20180227211930-4b158bb555a3 // indirect
github.com/blevesearch/go-porterstemmer v1.0.2 // indirect
github.com/blevesearch/segment v0.0.0-20160915185041-762005e7a34f // indirect
github.com/cockroachdb/apd v1.1.0 // indirect
github.com/couchbase/vellum v0.0.0-20190610201045-ec7b775d247f // indirect
github.com/cznic/b v0.0.0-20181122101859-a26611c4d92d // indirect
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
Expand All @@ -29,12 +30,16 @@ require (
github.com/gorilla/mux v1.7.2
github.com/gorilla/websocket v0.0.0-20190427040300-80c2d40e9b91
github.com/grpc-ecosystem/grpc-gateway v1.9.2
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 // indirect
github.com/jackc/pgx v3.4.0+incompatible
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/lib/pq v0.0.0-20190507191800-2ff3cb3adc01
github.com/lib/pq v1.1.1 // indirect
github.com/mattn/go-sqlite3 v1.10.0 // indirect
github.com/pkg/errors v0.8.1
github.com/remyoudompheng/bigfft v0.0.0-20190512091148-babf20351dd7 // indirect
github.com/rubenv/sql-migrate v0.0.0-20190618074400-f4d34eae5a5c
github.com/satori/go.uuid v1.2.0 // indirect
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24 // indirect
github.com/steveyen/gtreap v0.0.0-20150807155958-0abe01ef9be2 // indirect
github.com/stretchr/testify v1.3.0
github.com/syndtr/goleveldb v1.0.0 // indirect
Expand Down
14 changes: 12 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWR
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
Expand Down Expand Up @@ -115,6 +117,10 @@ github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 h1:vr3AYkKovP8uR8AvSGGUK1IDqRa5lAAvEkZG1LKaCRc=
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733/go.mod h1:WrMFNQdiFJ80sQsxDoMokWK1W5TQtxBFNpzWTD84ibQ=
github.com/jackc/pgx v3.4.0+incompatible h1:XRfh5KFhf3AVttfC0D93ij0oNNGYlSm0xlc532nXdBM=
github.com/jackc/pgx v3.4.0+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmhodges/levigo v1.0.0 h1:q5EC36kV79HWeTBWsod3mG11EgStG3qArTKcvlksN1U=
Expand All @@ -132,8 +138,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lib/pq v0.0.0-20190507191800-2ff3cb3adc01 h1:Tw9dWgbVf7xkUElIQjbW922J6i8Zl944Sd73C++JzjI=
github.com/lib/pq v0.0.0-20190507191800-2ff3cb3adc01/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4=
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE=
github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0=
Expand Down Expand Up @@ -174,6 +180,10 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rubenv/sql-migrate v0.0.0-20190618074400-f4d34eae5a5c h1:D7a64hNIAYYKqFtELrXTROQ1xjvbK4S4pzegr6xUEUQ=
github.com/rubenv/sql-migrate v0.0.0-20190618074400-f4d34eae5a5c/go.mod h1:WS0rl9eEliYI8DPnr3TOwz4439pay+qNgzJoVya/DmY=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24 h1:pntxY8Ary0t43dCZ5dqY4YTJCObLY1kIXl0uzMv+7DE=
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
"github.com/heroiclabs/nakama/migrate"
"github.com/heroiclabs/nakama/server"
"github.com/heroiclabs/nakama/social"
_ "github.com/lib/pq"
_ "github.com/jackc/pgx/stdlib"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
Expand Down Expand Up @@ -211,7 +211,7 @@ func dbConnect(multiLogger *zap.Logger, config server.Config) (*sql.DB, string)
}

multiLogger.Debug("Complete database connection URL", zap.String("raw_url", parsedUrl.String()))
db, err := sql.Open("postgres", parsedUrl.String())
db, err := sql.Open("pgx", parsedUrl.String())
if err != nil {
multiLogger.Fatal("Error connecting to database", zap.Error(err))
}
Expand Down
12 changes: 7 additions & 5 deletions migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import (

"github.com/gobuffalo/packr"
"github.com/heroiclabs/nakama/server"
"github.com/lib/pq"
"github.com/jackc/pgx"
_ "github.com/jackc/pgx/stdlib"
"github.com/rubenv/sql-migrate"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -110,6 +111,7 @@ func Parse(args []string, tmpLogger *zap.Logger) {
exec = ms.status
default:
tmpLogger.Fatal("Unrecognized migrate subcommand. Available commands are: 'up', 'down', 'redo', 'status'.")
return
}

ms.parseSubcommand(args[1:], tmpLogger)
Expand Down Expand Up @@ -137,7 +139,7 @@ func Parse(args []string, tmpLogger *zap.Logger) {
logger.Info("Database connection", zap.String("dsn", ms.dbAddress))

parsedUrl.Path = ""
db, err := sql.Open(dialect, parsedUrl.String())
db, err := sql.Open("pgx", parsedUrl.String())
if err != nil {
logger.Fatal("Failed to open database", zap.Error(err))
}
Expand All @@ -152,19 +154,19 @@ func Parse(args []string, tmpLogger *zap.Logger) {
logger.Info("Database information", zap.String("version", dbVersion))

if _, err = db.Exec(fmt.Sprintf("CREATE DATABASE %q", dbname)); err != nil {
if e, ok := err.(*pq.Error); ok && e.Code == dbErrorDuplicateDatabase {
if e, ok := err.(pgx.PgError); ok && e.Code == dbErrorDuplicateDatabase {
logger.Info("Using existing database", zap.String("name", dbname))
} else {
logger.Fatal("Database query failed", zap.Error(err))
}
} else {
logger.Info("Creating new database", zap.String("name", dbname))
}
db.Close()
_ = db.Close()

// Append dbname to data source name.
parsedUrl.Path = fmt.Sprintf("/%s", dbname)
db, err = sql.Open(dialect, parsedUrl.String())
db, err = sql.Open("pgx", parsedUrl.String())
if err != nil {
logger.Fatal("Failed to open database", zap.Error(err))
}
Expand Down
4 changes: 2 additions & 2 deletions server/api_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/gofrs/uuid"
"github.com/golang/protobuf/ptypes/empty"
"github.com/heroiclabs/nakama/api"
"github.com/lib/pq"
"github.com/jackc/pgx"
"go.uber.org/zap"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -102,7 +102,7 @@ func (s *ApiServer) UpdateAccount(ctx context.Context, in *api.UpdateAccountRequ

err := UpdateAccount(ctx, s.logger, s.db, userID, username, in.GetDisplayName(), in.GetTimezone(), in.GetLocation(), in.GetLangTag(), in.GetAvatarUrl(), nil)
if err != nil {
if _, ok := err.(*pq.Error); ok {
if _, ok := err.(pgx.PgError); ok {
return nil, status.Error(codes.Internal, "Error while trying to update account.")
}
return nil, status.Error(codes.InvalidArgument, err.Error())
Expand Down
4 changes: 2 additions & 2 deletions server/api_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/gofrs/uuid"
"github.com/golang/protobuf/ptypes/empty"
"github.com/heroiclabs/nakama/api"
"github.com/lib/pq"
"github.com/jackc/pgx"
"go.uber.org/zap"
"golang.org/x/crypto/bcrypt"
"golang.org/x/net/context"
Expand Down Expand Up @@ -147,7 +147,7 @@ func (s *ApiServer) LinkDevice(ctx context.Context, in *api.AccountDevice) (*emp
if dbDeviceIdLinkedUser == 0 {
_, err = tx.ExecContext(ctx, "INSERT INTO user_device (id, user_id) VALUES ($1, $2)", deviceID, userID)
if err != nil {
if e, ok := err.(*pq.Error); ok && e.Code == dbErrorUniqueViolation {
if e, ok := err.(pgx.PgError); ok && e.Code == dbErrorUniqueViolation {
return StatusError(codes.AlreadyExists, "Device ID already in use.", err)
}
s.logger.Debug("Cannot link device ID.", zap.Error(err), zap.Any("input", in))
Expand Down
10 changes: 5 additions & 5 deletions server/console_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/heroiclabs/nakama/api"
"github.com/heroiclabs/nakama/console"
"github.com/lib/pq"
"github.com/jackc/pgx/pgtype"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -130,11 +130,11 @@ func (s *ConsoleServer) ListStorage(ctx context.Context, in *console.ListStorage

for rows.Next() {
o := &api.StorageObject{CreateTime: &timestamp.Timestamp{}, UpdateTime: &timestamp.Timestamp{}}
var createTime pq.NullTime
var updateTime pq.NullTime
var createTime pgtype.Timestamptz
var updateTime pgtype.Timestamptz

if err := rows.Scan(&o.Collection, &o.Key, &o.UserId, &o.Value, &o.Version, &o.PermissionRead, &o.PermissionWrite, &createTime, &updateTime); err != nil {
rows.Close()
_ = rows.Close()
s.logger.Error("Error scanning storage objects.", zap.Any("in", in), zap.Error(err))
return nil, status.Error(codes.Internal, "An error occurred while trying to list storage objects.")
}
Expand All @@ -144,7 +144,7 @@ func (s *ConsoleServer) ListStorage(ctx context.Context, in *console.ListStorage

objects = append(objects, o)
}
rows.Close()
_ = rows.Close()

return &console.StorageList{
Objects: objects,
Expand Down
18 changes: 9 additions & 9 deletions server/console_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/heroiclabs/nakama/api"
"github.com/heroiclabs/nakama/console"
"github.com/lib/pq"
"github.com/jackc/pgx/pgtype"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -90,7 +90,7 @@ func (s *ConsoleServer) ListUsers(ctx context.Context, in *console.ListUsersRequ

if userID != nil {
// Looking up a single specific tombstone.
var createTime pq.NullTime
var createTime pgtype.Timestamptz
err := s.db.QueryRowContext(ctx, "SELECT create_time FROM user_tombstone WHERE user_id = $1", *userID).Scan(&createTime)
if err != nil {
if err == sql.ErrNoRows {
Expand Down Expand Up @@ -125,9 +125,9 @@ func (s *ConsoleServer) ListUsers(ctx context.Context, in *console.ListUsersRequ

for rows.Next() {
var id string
var createTime pq.NullTime
var createTime pgtype.Timestamptz
if err = rows.Scan(&id, &createTime); err != nil {
rows.Close()
_ = rows.Close()
s.logger.Error("Error scanning user tombstones.", zap.Any("in", in), zap.Error(err))
return nil, status.Error(codes.Internal, "An error occurred while trying to list users.")
}
Expand All @@ -137,7 +137,7 @@ func (s *ConsoleServer) ListUsers(ctx context.Context, in *console.ListUsersRequ
UpdateTime: &timestamp.Timestamp{Seconds: createTime.Time.Unix()},
})
}
rows.Close()
_ = rows.Close()

return &console.UserList{
Users: users,
Expand Down Expand Up @@ -172,13 +172,13 @@ func (s *ConsoleServer) ListUsers(ctx context.Context, in *console.ListUsersRequ
for rows.Next() {
user, err := convertUser(s.tracker, rows)
if err != nil {
rows.Close()
_ = rows.Close()
s.logger.Error("Error scanning users.", zap.Any("in", in), zap.Error(err))
return nil, status.Error(codes.Internal, "An error occurred while trying to list users.")
}
users = append(users, user)
}
rows.Close()
_ = rows.Close()

return &console.UserList{
Users: users,
Expand All @@ -205,14 +205,14 @@ func (s *ConsoleServer) ListUsers(ctx context.Context, in *console.ListUsersRequ
for rows.Next() {
user, err := convertUser(s.tracker, rows)
if err != nil {
rows.Close()
_ = rows.Close()
s.logger.Error("Error scanning users.", zap.Any("in", in), zap.Error(err))
return nil, status.Error(codes.Internal, "An error occurred while trying to list users.")
}

users = append(users, user)
}
rows.Close()
_ = rows.Close()

return &console.UserList{
Users: users,
Expand Down
Loading

0 comments on commit 2c13ff5

Please sign in to comment.