Skip to content

Commit

Permalink
chore: switch to int64 ids (snowflake)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Oct 19, 2019
1 parent ff9754c commit 787a229
Show file tree
Hide file tree
Showing 37 changed files with 1,275 additions and 1,830 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Cassius de Leeuwe <[email protected]>
Corentin Kerisit <[email protected]>
dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Emi_Janina <[email protected]>
Janina <[email protected]>
Lucas Germano <[email protected]>
Manfred Touron <[email protected]>
Expand Down
94 changes: 47 additions & 47 deletions api/pwdb.proto

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions api/pwengine.proto
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ message Void {
}

message ListTournamentTeamsInput {
string tournament_id = 1 [(gogoproto.customname) = "TournamentID"];
int64 tournament_id = 1 [(gogoproto.customname) = "TournamentID"];
}

message SetPreferencesInput {
string active_tournament_id = 1 [(gogoproto.customname) = "ActiveTournamentID"];
int64 active_tournament_id = 1 [(gogoproto.customname) = "ActiveTournamentID"];
// bool dark_mode
}

Expand Down
4 changes: 2 additions & 2 deletions docs/gen.sum

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

63 changes: 34 additions & 29 deletions go/cmd/pathwar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os/signal"
"time"

"github.com/bwmarrin/snowflake"
_ "github.com/go-sql-driver/mysql" // required by gorm
"github.com/jinzhu/gorm"
"github.com/oklog/run"
Expand Down Expand Up @@ -93,7 +94,7 @@ func main() {
var err error
logger, err = config.Build()
if err != nil {
return fmt.Errorf("failed to initialize logger: %w", err)
return fmt.Errorf("init logger: %w", err)
}
} else {
config := zap.NewDevelopmentConfig()
Expand All @@ -103,7 +104,7 @@ func main() {
var err error
logger, err = config.Build()
if err != nil {
return fmt.Errorf("failed to initialize logger: %w", err)
return fmt.Errorf("init logger: %w", err)
}
}
return nil
Expand Down Expand Up @@ -132,10 +133,10 @@ func main() {
return err
}

// initialize engine
// init engine
engine, _, _, closer, err := engineFromFlags()
if err != nil {
return fmt.Errorf("failed to start engine: %w", err)
return fmt.Errorf("start engine: %w", err)
}
defer closer()

Expand All @@ -155,7 +156,7 @@ func main() {
}
start, cleanup, err := pwserver.Start(ctx, engine, opts)
if err != nil {
return fmt.Errorf("failed to initialize server: %w", err)
return fmt.Errorf("init server: %w", err)
}
g.Add(
start,
Expand Down Expand Up @@ -197,16 +198,16 @@ func main() {
return err
}

// initialize engine
// init engine
_, db, _, closer, err := engineFromFlags()
if err != nil {
return fmt.Errorf("failed to start engine: %w", err)
return fmt.Errorf("start engine: %w", err)
}
defer closer()

dump, err := pwdb.GetDump(db)
if err != nil {
return fmt.Errorf("failed to dump database: %w", err)
return fmt.Errorf("dump database: %w", err)
}

out, _ := json.MarshalIndent(dump, "", " ")
Expand All @@ -223,16 +224,16 @@ func main() {
return err
}

// initialize engine
// init engine
_, db, _, closer, err := engineFromFlags()
if err != nil {
return fmt.Errorf("failed to start engine: %w", err)
return fmt.Errorf("start engine: %w", err)
}
defer closer()

info, err := pwdb.GetInfo(db, logger)
if err != nil {
return fmt.Errorf("failed to get database info: %w", err)
return fmt.Errorf("get database info: %w", err)
}

out, _ := json.MarshalIndent(info, "", " ")
Expand Down Expand Up @@ -284,13 +285,13 @@ func main() {
}
sso, err := ssoFromFlags()
if err != nil {
return fmt.Errorf("failed to get sso client from flags: %w", err)
return fmt.Errorf("get sso client from flags: %w", err)
}

// whoami
info, err := sso.Whoami(args[0])
if err != nil {
return fmt.Errorf("failed to get 'whoami' from SSO: %w", err)
return fmt.Errorf("get 'whoami' from SSO: %w", err)
}
for k, v := range info {
fmt.Printf("- %s: %v\n", k, v)
Expand All @@ -311,12 +312,12 @@ func main() {
}
sso, err := ssoFromFlags()
if err != nil {
return fmt.Errorf("failed to get sso client from flags: %w", err)
return fmt.Errorf("get sso client from flags: %w", err)
}

// logout
if err := sso.Logout(args[0]); err != nil {
return fmt.Errorf("failed to logout from SSO: %w", err)
return fmt.Errorf("logout from SSO: %w", err)
}
return nil
},
Expand All @@ -334,13 +335,13 @@ func main() {
}
sso, err := ssoFromFlags()
if err != nil {
return fmt.Errorf("failed to get sso client from flags: %w", err)
return fmt.Errorf("get sso client from flags: %w", err)
}

// token
token, _, err := sso.TokenWithClaims(args[0])
if err != nil {
return fmt.Errorf("failed to get claims: %w", err)
return fmt.Errorf("get claims: %w", err)
}
out, _ := json.MarshalIndent(token, "", " ")
fmt.Println(string(out))
Expand Down Expand Up @@ -394,20 +395,24 @@ func main() {
}

func engineFromFlags() (pwengine.Engine, *gorm.DB, pwsso.Client, func(), error) {
// initialize database
// init database
db, err := gorm.Open("mysql", *engineDBURN)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("failed to initialize database: %w", err)
return nil, nil, nil, nil, fmt.Errorf("init database: %w", err)
}
sfn, err := snowflake.NewNode(1)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("init snowflake: %w", err)
}
dbOpts := pwdb.Opts{
Logger: logger.Named("gorm"),
}
db, err = pwdb.Configure(db, dbOpts)
db, err = pwdb.Configure(db, sfn, dbOpts)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("failed to configure database: %w", err)
return nil, nil, nil, nil, fmt.Errorf("configure database: %w", err)
}

// initialize SSO
// init SSO
ssoOpts := pwsso.Opts{
AllowUnsafe: *engineSSOAllowUnsafe,
Logger: logger.Named("sso"),
Expand All @@ -418,29 +423,29 @@ func engineFromFlags() (pwengine.Engine, *gorm.DB, pwsso.Client, func(), error)
}
sso, err := pwsso.New(*engineSSOPubkey, *engineSSORealm, ssoOpts)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("failed to initialize SSO client: %w", err)
return nil, nil, nil, nil, fmt.Errorf("init SSO client: %w", err)
}

// initialize engine
// init engine
engineOpts := pwengine.Opts{
Logger: logger.Named("engine"),
}

engine, err := pwengine.New(db, sso, engineOpts)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("failed to initialize engine: %w", err)
return nil, nil, nil, nil, fmt.Errorf("init engine: %w", err)
}

closeFunc := func() {
if err := engine.Close(); err != nil {
logger.Warn("failed to close engine", zap.Error(err))
logger.Warn("close engine", zap.Error(err))
}
if err := db.Close(); err != nil {
logger.Warn("failed to closed database", zap.Error(err))
logger.Warn("closed database", zap.Error(err))
}
}

logger.Debug("engine initialized", zap.Any("db", db), zap.Any("opts", engineOpts))
logger.Debug("engine initd", zap.Any("db", db), zap.Any("opts", engineOpts))
return engine, db, sso, closeFunc, nil
}

Expand All @@ -455,7 +460,7 @@ func ssoFromFlags() (pwsso.Client, error) {
}
sso, err := pwsso.New(*ssoPubkey, *ssoRealm, ssoOpts)
if err != nil {
return nil, fmt.Errorf("failed to initialize SSO client: %w", err)
return nil, fmt.Errorf("init SSO client: %w", err)
}
return sso, nil
}
4 changes: 2 additions & 2 deletions go/gen.sum

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

3 changes: 1 addition & 2 deletions go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module pathwar.land/go

require (
github.com/brianvoe/gofakeit v3.18.0+incompatible
github.com/bwmarrin/snowflake v0.3.0
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/go-chi/chi v4.0.2+incompatible
github.com/go-sql-driver/mysql v1.4.1
Expand All @@ -15,9 +16,7 @@ require (
github.com/keycloak/kcinit v0.0.0-20181010192927-f85c3c5390ea
github.com/oklog/run v1.0.0
github.com/peterbourgon/ff v1.6.1-0.20190916204019-6cd704ec2eeb
github.com/pkg/errors v0.8.1
github.com/rs/cors v1.7.0
github.com/satori/go.uuid v1.2.0
github.com/treastech/logger v0.0.0-20180705232552-e381e9ecf2e3
go.uber.org/multierr v1.2.0 // indirect
go.uber.org/zap v1.10.0
Expand Down
4 changes: 2 additions & 2 deletions go/go.sum

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

1 change: 1 addition & 0 deletions go/internal/testutil/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package testutil // import "pathwar.land/go/internal/testutil"
29 changes: 29 additions & 0 deletions go/internal/testutil/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package testutil

import (
"flag"
"testing"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var debug = flag.Bool("debug", false, "more verbose logging")

func Logger(t *testing.T) *zap.Logger {
t.Helper()
if !*debug {
return zap.NewNop()
}

config := zap.NewDevelopmentConfig()
config.DisableStacktrace = true
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
config.Level.SetLevel(zap.DebugLevel)
logger, err := config.Build()
if err != nil {
t.Errorf("debug logger: %v", err)
return zap.NewNop()
}
return logger
}
30 changes: 12 additions & 18 deletions go/pkg/pwdb/db.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package pwdb

import (
"encoding/base64"
"fmt"

"github.com/bwmarrin/snowflake"
"github.com/jinzhu/gorm"
uuid "github.com/satori/go.uuid"
"go.uber.org/zap"
"moul.io/zapgorm"
)
Expand All @@ -16,33 +16,27 @@ type Opts struct {
skipFK bool
}

func Configure(db *gorm.DB, opts Opts) (*gorm.DB, error) {
func Configure(db *gorm.DB, sfn *snowflake.Node, opts Opts) (*gorm.DB, error) {
db.SetLogger(zapgorm.New(opts.Logger))
//db.Callback().Create().Remove("gorm:update_time_stamp")
//db.Callback().Update().Remove("gorm:update_time_stamp")
db.Callback().Create().Before("gorm:create").Register("pathwar_before_create", beforeCreate)
db.Callback().Create().Before("gorm:create").Register("pathwar_before_create", beforeCreate(sfn))
db = db.Set("gorm:auto_preload", false)
db = db.Set("gorm:association_autoupdate", false)
db.BlockGlobalUpdate(true)
db.SingularTable(true)
db.LogMode(true)
if err := migrate(db, opts); err != nil {
return nil, err
if err := migrate(db, sfn, opts); err != nil {
return nil, fmt.Errorf("run db migrations: %w", err)
}
return db, nil
}

func beforeCreate(scope *gorm.Scope) {
switch scope.TableName() {
case "user":
return
}
id, err := uuid.NewV4().MarshalBinary()
if err != nil {
panic(err)
}
out := base64.StdEncoding.EncodeToString(id)
if err := scope.SetColumn("ID", out); err != nil {
panic(err)
func beforeCreate(sfn *snowflake.Node) func(*gorm.Scope) {
return func(scope *gorm.Scope) {
id := sfn.Generate().Int64()
if err := scope.SetColumn("ID", id); err != nil {
panic(err)
}
}
}
11 changes: 11 additions & 0 deletions go/pkg/pwdb/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pwdb

import (
"errors"

"github.com/jinzhu/gorm"
)

func IsRecordNotFoundError(err error) bool {
return gorm.IsRecordNotFoundError(errors.Unwrap(err))
}
Loading

0 comments on commit 787a229

Please sign in to comment.