Skip to content

Commit

Permalink
Upgrade pgx to v5
Browse files Browse the repository at this point in the history
Use trimmed sql-migrate
Config and db creation refactor.
  • Loading branch information
sesposito committed Jun 19, 2023
1 parent 5cf4992 commit e5636d7
Show file tree
Hide file tree
Showing 556 changed files with 29,730 additions and 43,439 deletions.
22 changes: 8 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ require (
github.com/gorilla/websocket v1.5.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3
github.com/heroiclabs/nakama-common v1.27.0
github.com/jackc/pgconn v1.13.0
github.com/heroiclabs/sql-migrate v0.0.0-20230615133120-fb3ad977aaaf
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa
github.com/jackc/pgtype v1.12.0
github.com/jackc/pgx/v4 v4.17.2
github.com/rubenv/sql-migrate v1.2.0
github.com/stretchr/testify v1.8.0
github.com/jackc/pgx/v5 v5.4.1
github.com/stretchr/testify v1.8.1
github.com/uber-go/tally/v4 v4.1.3
go.uber.org/atomic v1.10.0
go.uber.org/zap v1.23.0
golang.org/x/crypto v0.0.0-20221012134737-56aed061732a
golang.org/x/crypto v0.10.0
golang.org/x/oauth2 v0.0.0-20220822191816-0ebed06d0094
google.golang.org/genproto v0.0.0-20220822174746-9e6da59bd2fc
google.golang.org/grpc v1.50.0
Expand Down Expand Up @@ -52,16 +50,12 @@ require (
github.com/dgryski/go-metro v0.0.0-20180109044635-280f6062b5bc // indirect
github.com/dlclark/regexp2 v1.7.0 // indirect
github.com/felixge/httpsnoop v1.0.1 // indirect
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/klauspost/compress v1.15.2 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mschoch/smat v0.2.0 // indirect
Expand All @@ -73,8 +67,8 @@ require (
github.com/prometheus/procfs v0.6.0 // indirect
github.com/twmb/murmur3 v1.1.5 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect
golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
)
299 changes: 19 additions & 280 deletions go.sum

Large diffs are not rendered by default.

48 changes: 38 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"encoding/binary"
"flag"
"fmt"
"github.com/heroiclabs/nakama/v3/migrate"
"github.com/jackc/pgx/v5/stdlib"
"math/rand"
"net/http"
"net/url"
Expand All @@ -34,10 +36,9 @@ import (
"github.com/gofrs/uuid"
"github.com/heroiclabs/nakama/v3/console"
"github.com/heroiclabs/nakama/v3/ga"
"github.com/heroiclabs/nakama/v3/migrate"
"github.com/heroiclabs/nakama/v3/server"
"github.com/heroiclabs/nakama/v3/social"
_ "github.com/jackc/pgx/v4/stdlib"
_ "github.com/jackc/pgx/v5/stdlib"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/protobuf/encoding/protojson"
Expand Down Expand Up @@ -68,13 +69,33 @@ func main() {

tmpLogger := server.NewJSONLogger(os.Stdout, zapcore.InfoLevel, server.JSONFormat)

ctx, ctxCancelFn := context.WithCancel(context.Background())

if len(os.Args) > 1 {
switch os.Args[1] {
case "--version":
fmt.Println(semver)
return
case "migrate":
migrate.Parse(os.Args[2:], tmpLogger)
config := server.ParseArgs(tmpLogger, os.Args[2:])
config.ValidateDatabase(tmpLogger)
db := server.DbConnect(ctx, tmpLogger, config, true)
defer db.Close()

conn, err := db.Conn(ctx)
if err != nil {
tmpLogger.Fatal("Failed to acquire db conn for migration", zap.Error(err))
}

if err = conn.Raw(func(driverConn any) error {
pgxConn := driverConn.(*stdlib.Conn).Conn()
migrate.RunCmd(ctx, tmpLogger, pgxConn, os.Args[2], config.GetLimit(), config.GetLogger().Format)

return nil
}); err != nil {
tmpLogger.Fatal("Failed to acquire pgx conn for migration", zap.Error(err))
}

return
case "check":
// Parse any command line args to look up runtime path.
Expand All @@ -98,7 +119,7 @@ func main() {

config := server.ParseArgs(tmpLogger, os.Args)
logger, startupLogger := server.SetupLogging(tmpLogger, config)
configWarnings := server.CheckConfig(logger, config)
configWarnings := config.Validate(logger)

startupLogger.Info("Nakama starting")
startupLogger.Info("Node", zap.String("name", config.GetName()), zap.String("version", semver), zap.String("runtime", runtime.Version()), zap.Int("cpu", runtime.NumCPU()), zap.Int("proc", runtime.GOMAXPROCS(0)))
Expand All @@ -123,14 +144,21 @@ func main() {
}
startupLogger.Info("Database connections", zap.Strings("dsns", redactedAddresses))

// Global server context.
ctx, ctxCancelFn := context.WithCancel(context.Background())

db, dbVersion := server.DbConnect(ctx, startupLogger, config)
startupLogger.Info("Database information", zap.String("version", dbVersion))
db := server.DbConnect(ctx, startupLogger, config, false)

// Check migration status and fail fast if the schema has diverged.
migrate.StartupCheck(startupLogger, db)
conn, err := db.Conn(context.Background())
if err != nil {
logger.Fatal("Failed to acquire db conn for migration check", zap.Error(err))
}

if err = conn.Raw(func(driverConn any) error {
pgxConn := driverConn.(*stdlib.Conn).Conn()
migrate.Check(ctx, startupLogger, pgxConn)
return nil
}); err != nil {
logger.Fatal("Failed to acquire pgx conn for migration check", zap.Error(err))
}

// Access to social provider integrations.
socialClient := social.NewClient(logger, 5*time.Second, config.GetGoogleAuth().OAuthConfig)
Expand Down
Loading

0 comments on commit e5636d7

Please sign in to comment.