Skip to content

Commit

Permalink
Upgrades gorm to v2.0
Browse files Browse the repository at this point in the history
Signed-off-by: Shivam Mukhade <smukhade@redhat.com>
  • Loading branch information
SM43 committed Nov 19, 2020
1 parent f56d79d commit 4d1bf72
Show file tree
Hide file tree
Showing 38 changed files with 405 additions and 314 deletions.
2 changes: 1 addition & 1 deletion api/cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func main() {
fmt.Fprintf(os.Stderr, "FATAL: failed to initialise: %s", err)
os.Exit(1)
}
api.DB().LogMode(true)

logger = api.Logger("main")
defer api.Cleanup()
}
Expand Down
1 change: 0 additions & 1 deletion api/cmd/db/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func main() {
}
defer api.Cleanup()

api.DB().LogMode(true)
logger := api.Logger("main")
if err = migration.Migrate(api); err != nil {
logger.Errorf("DB initialisation failed !!")
Expand Down
6 changes: 3 additions & 3 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ go 1.14

require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/go-gormigrate/gormigrate/v2 v2.0.0
github.com/go-testfixtures/testfixtures/v3 v3.2.0
github.com/google/go-github v17.0.0+incompatible
github.com/ikawaha/goahttpcheck v1.3.1
github.com/jinzhu/gorm v1.9.12
github.com/joho/godotenv v1.3.0
github.com/lib/pq v1.3.0
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.7.0
Expand All @@ -22,8 +21,9 @@ require (
golang.org/x/sys v0.0.0-20200812155832-6a926be9bd1d // indirect
golang.org/x/tools v0.0.0-20200811215021-48a8ffc5b207 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/gormigrate.v1 v1.6.0
gopkg.in/h2non/gock.v1 v1.0.15
gorm.io/driver/postgres v1.0.2
gorm.io/gorm v1.20.7
gotest.tools/v3 v3.0.2
k8s.io/apimachinery v0.17.6
k8s.io/client-go v11.0.1-0.20190805182717-6502b5e7b1b5+incompatible
Expand Down
125 changes: 100 additions & 25 deletions api/go.sum

Large diffs are not rendered by default.

35 changes: 18 additions & 17 deletions api/pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@ import (
"os"
"strings"

"github.com/jinzhu/gorm"
"github.com/joho/godotenv"
"github.com/spf13/viper"
"github.com/tektoncd/hub/api/gen/log"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/oauth2"
"golang.org/x/oauth2/github"

// Blank for package side effect: loads postgres drivers
_ "github.com/lib/pq"

"github.com/tektoncd/hub/api/gen/log"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

// BaseConfig defines methods on APIBase
Expand All @@ -49,7 +46,7 @@ type BaseConfig interface {

// APIBase defines the base configuration every service requires
type APIBase struct {
mode EnvMode
env EnvMode
dbConf *Database
db *gorm.DB
logger *log.Logger
Expand Down Expand Up @@ -110,7 +107,7 @@ func (db Database) ConnectionString() string {

// Environment returns the EnvMode server would be running
func (ab *APIBase) Environment() EnvMode {
return ab.mode
return ab.env
}

// DB returns gorm db object
Expand All @@ -119,9 +116,8 @@ func (ab *APIBase) DB() *gorm.DB {
}

// DBWithLogger returns gorm db object initialised with logger
func DBWithLogger(db *gorm.DB, logger *log.Logger) *gorm.DB {
db = db.New()
db.SetLogger(&gormLogger{logger})
func DBWithLogger(env EnvMode, db *gorm.DB, logger *log.Logger) *gorm.DB {
db = db.Session(&gorm.Session{Logger: newGormLogger(env, logger)})
return db
}

Expand All @@ -145,6 +141,7 @@ func (ab *APIBase) Service(name string) Service {
return &BaseService{
logger: l,
db: ab.DB(),
env: ab,
}
}

Expand Down Expand Up @@ -188,7 +185,8 @@ func (ab *APIBase) ReloadData() error {
// Cleanup flushes any buffered log entries & closes the db connection
func (ab *APIBase) Cleanup() {
ab.logger.Sync()
ab.db.Close()
db, _ := ab.db.DB()
db.Close()
}

// OAuthConfig returns oauth2 config object
Expand Down Expand Up @@ -256,24 +254,27 @@ func APIBaseFromEnvFile(file string) (*APIBase, error) {
// NOTE: DO NOT move this line; viper must be initialized before reading ENV variables
viper.AutomaticEnv()

mode := Environment()
env := Environment()

var err error
var l *log.Logger
if l, err = initLogger(mode); err != nil {
if l, err = initLogger(env); err != nil {
return nil, err
}

ab := &APIBase{mode: mode, logger: l}
ab := &APIBase{env: env, logger: l}
log := ab.logger.With("app", "hub")

log.Infof("in %q mode ", mode)
log.Infof("in %q mode ", env)

if ab.dbConf, err = initDB(); err != nil {
log.Errorf("failed to obtain database configuration: %v", err)
return nil, err
}
ab.db, err = gorm.Open(DBDialect, ab.dbConf.ConnectionString())

ab.db, err = gorm.Open(postgres.Open(ab.dbConf.ConnectionString()), &gorm.Config{
Logger: newGormLogger(env, ab.logger),
})
if err != nil {
log.Errorf("failed to establish database connection: [%s]: %s", ab.dbConf, err)
return nil, err
Expand Down
85 changes: 85 additions & 0 deletions api/pkg/app/gorm_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright © 2020 The Tekton Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package app

import (
"fmt"
"strings"
"time"

"github.com/tektoncd/hub/api/gen/log"
glog "gorm.io/gorm/logger"
)

func newGormLogger(mode EnvMode, l *log.Logger) glog.Interface {
if mode == Production {
return glog.New(&prodWriter{l}, glog.Config{
SlowThreshold: 100 * time.Millisecond,
LogLevel: glog.Info,
Colorful: false,
})
}

return glog.New(&devWriter{l}, glog.Config{
SlowThreshold: 100 * time.Millisecond,
LogLevel: glog.Info,
Colorful: true,
})
}

// adaptor for gorm logger interface
type prodWriter struct {
log *log.Logger
}

func (w *prodWriter) Printf(format string, data ...interface{}) {

fields := strings.Fields(strings.Replace(format, "\n", " ", -1))
log := w.log.With("file", data[0])

data = data[1:]
fields = fields[1:]

msg := ""

for i, d := range data {
switch d.(type) {
case error:
log = log.With("db-error", d)
case float64:
log = log.With("duration", fmt.Sprintf(fields[i], d))
case int64:
log = log.With("rows", d)
case string:
if i == len(data)-1 {
msg = d.(string)
} else {
log = log.With("unknown", d)
}
default:
log = log.With("unknown", d)
}
}
log.Info(msg)
}

// adaptor for gorm logger interface
type devWriter struct {
log *log.Logger
}

func (w *devWriter) Printf(msg string, data ...interface{}) {
w.log.SugaredLogger.Infof(strings.Replace(msg, "%s ", "%s\n", 1), data...)
}
30 changes: 12 additions & 18 deletions api/pkg/app/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ package app
import (
"context"

"github.com/jinzhu/gorm"
"goa.design/goa/v3/middleware"

"github.com/tektoncd/hub/api/gen/log"
"goa.design/goa/v3/middleware"
"gorm.io/gorm"
)

// Service defines methods on BaseService
Expand All @@ -30,22 +29,18 @@ type Service interface {
DB(ctx context.Context) *gorm.DB
}

type environmenter interface {
Environment() EnvMode
}

// BaseService defines configuraition for creating logger and
// db object with http request id
type BaseService struct {
env environmenter
logger *log.Logger
db *gorm.DB
}

// adaptor for gorm logger interface
type gormLogger struct {
*log.Logger
}

func (l *gormLogger) Print(v ...interface{}) {
l.Info(v...)
}

// Logger looks for http request id in passed context and append it to
// logger. This would help in filtering logs with request id.
func (s *BaseService) Logger(ctx context.Context) *log.Logger {
Expand All @@ -63,11 +58,10 @@ func (s *BaseService) LoggerWith(ctx context.Context, args ...interface{}) *log.
}

// DB gets logger initialized with http request id and creates a gorm db
// object with replacing default logger with created one, so that each
// gorm log will have http request id.
// session replacing writer for gorm logger with log.Logger so that gorm log
// will have http request id.
func (s *BaseService) DB(ctx context.Context) *gorm.DB {
logger := s.Logger(ctx)
db := s.db.New()
db.SetLogger(&gormLogger{logger})
return db
return s.db.Session(&gorm.Session{
Logger: newGormLogger(s.env.Environment(), s.Logger(ctx)),
})
}
29 changes: 14 additions & 15 deletions api/pkg/db/initializer/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ import (
"context"
"strings"

"github.com/jinzhu/gorm"

"github.com/tektoncd/hub/api/gen/log"
"github.com/tektoncd/hub/api/pkg/app"
"github.com/tektoncd/hub/api/pkg/db/model"
"gorm.io/gorm"
)

// Initializer defines the configuration required for initailizer
Expand All @@ -46,16 +45,16 @@ func (i *Initializer) Run(ctx context.Context) (*model.Config, error) {
db := i.DB(ctx)
logger := i.Logger(ctx)

config := &model.Config{}
if err := db.Model(config).FirstOrInit(config).Error; err != nil {
config := model.Config{}
if err := db.Model(&model.Config{}).FirstOrInit(&config).Error; err != nil {
logger.Error(err)
return nil, err
}

data := i.api.Data()
if config.Checksum == data.Checksum {
logger.Info("SKIP: Config refresh as config file has not changed")
return config, nil
return &config, nil
}

updateConfig := func(db *gorm.DB, log *log.Logger, data *app.Data) error {
Expand All @@ -76,22 +75,22 @@ func (i *Initializer) Run(ctx context.Context) (*model.Config, error) {
return nil, err
}

return config, nil
return &config, nil
}

type initFn func(*gorm.DB, *log.Logger, *app.Data) error

func addCategories(db *gorm.DB, log *log.Logger, data *app.Data) error {

for _, c := range data.Categories {
cat := &model.Category{Name: c.Name}
if err := db.Where(cat).FirstOrCreate(cat).Error; err != nil {
cat := model.Category{Name: c.Name}
if err := db.Where(cat).FirstOrCreate(&cat).Error; err != nil {
log.Error(err)
return err
}
for _, t := range c.Tags {
tag := &model.Tag{Name: t, CategoryID: cat.ID}
if err := db.Where(tag).FirstOrCreate(tag).Error; err != nil {
tag := model.Tag{Name: t, CategoryID: cat.ID}
if err := db.Where(tag).FirstOrCreate(&tag).Error; err != nil {
log.Error(err)
return err
}
Expand All @@ -103,15 +102,15 @@ func addCategories(db *gorm.DB, log *log.Logger, data *app.Data) error {
func addCatalogs(db *gorm.DB, log *log.Logger, data *app.Data) error {

for _, c := range data.Catalogs {
cat := &model.Catalog{
cat := model.Catalog{
Name: c.Name,
Org: c.Org,
Type: c.Type,
URL: c.URL,
Revision: c.Revision,
ContextDir: c.ContextDir,
}
if err := db.Where(&model.Catalog{Name: c.Name, Org: c.Org}).FirstOrCreate(cat).Error; err != nil {
if err := db.Where(&model.Catalog{Name: c.Name, Org: c.Org}).FirstOrCreate(&cat).Error; err != nil {
log.Error(err)
return err
}
Expand All @@ -126,7 +125,7 @@ func addUsers(db *gorm.DB, log *log.Logger, data *app.Data) error {
// Check if scopes exist or create it
q := db.Where(&model.Scope{Name: s.Name})

scope := &model.Scope{}
scope := model.Scope{}
if err := q.FirstOrCreate(&scope).Error; err != nil {
log.Error(err)
return err
Expand All @@ -137,10 +136,10 @@ func addUsers(db *gorm.DB, log *log.Logger, data *app.Data) error {
// Checks if user exists
q := db.Where("LOWER(github_login) = ?", strings.ToLower(userID))

user := &model.User{}
user := model.User{}
if err := q.First(&user).Error; err != nil {
// If user not found then log and continue
if gorm.IsRecordNotFoundError(err) {
if err == gorm.ErrRecordNotFound {
log.Warnf("user %s not found: %s", userID, err)
continue
}
Expand Down
Loading

0 comments on commit 4d1bf72

Please sign in to comment.