Skip to content

Commit

Permalink
Upgrade gorm to v1.21.9 (flyteorg#42)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Rammer <[email protected]>
  • Loading branch information
milton0825 authored and hamersaw committed Sep 28, 2021
1 parent 5d93425 commit 7892458
Show file tree
Hide file tree
Showing 30 changed files with 245 additions and 194 deletions.
6 changes: 4 additions & 2 deletions cmd/entrypoints/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"context"

_ "github.com/jinzhu/gorm/dialects/postgres" // Required to import database driver.
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -73,7 +72,10 @@ var migrateCmd = &cobra.Command{
logger.Infof(ctx, "Created DB connection.")

// TODO: checkpoints for migrations
dbHandle.Migrate()
if err := dbHandle.Migrate(ctx); err != nil {
logger.Errorf(ctx, "Failed to migrate. err: %v", err)
panic(err)
}
logger.Infof(ctx, "Ran DB migration successfully.")
},
}
Expand Down
1 change: 1 addition & 0 deletions datacatalog_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ database:
host: localhost
dbname: datacatalog
options: "sslmode=disable"
log_level: 5
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ require (
github.com/flyteorg/flytestdlib v0.3.13
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/golang/protobuf v1.4.3
github.com/jackc/pgproto3/v2 v2.0.7 // indirect
github.com/jinzhu/gorm v1.9.16
github.com/lib/pq v1.3.0
github.com/mitchellh/mapstructure v1.4.1
github.com/spf13/cobra v1.1.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
golang.org/x/text v0.3.6 // indirect
google.golang.org/grpc v1.36.0
gorm.io/driver/postgres v1.1.0
gorm.io/gorm v1.21.9
Expand Down
43 changes: 5 additions & 38 deletions go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/common/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package common

// Common constants and types for Filtering
const (
DefaultPageOffset = uint32(0)
MaxPageLimit = uint32(50)
DefaultPageOffset = 0
MaxPageLimit = 50
)

// Common Entity types that can be used on any filters
Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/impl/artifact_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (m *artifactManager) ListArtifacts(ctx context.Context, request *datacatalo
artifact.Data = artifactDataList
}

token := strconv.Itoa(int(listInput.Offset) + len(artifactsList))
token := strconv.Itoa(listInput.Offset + len(artifactsList))

logger.Debugf(ctx, "Listed %v matching artifacts successfully", len(artifactsList))
m.systemMetrics.listSuccessCounter.Inc(ctx)
Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/impl/dataset_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (dm *datasetManager) ListDatasets(ctx context.Context, request *datacatalog
return nil, errors.NewCollectedErrors(codes.Internal, transformerErrs)
}

token := strconv.Itoa(int(listInput.Offset) + len(datasetList))
token := strconv.Itoa(listInput.Offset + len(datasetList))

logger.Debugf(ctx, "Listed %v matching datasets successfully", len(datasetList))
dm.systemMetrics.listSuccessCounter.Inc(ctx)
Expand Down
5 changes: 4 additions & 1 deletion pkg/repositories/config/database.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package config

import "gorm.io/gorm/logger"

//go:generate pflags DbConfigSection

// This struct corresponds to the database section of in the config
Expand All @@ -12,7 +14,8 @@ type DbConfigSection struct {
Password string `json:"password"`
PasswordPath string `json:"passwordPath"`
// See http://gorm.io/docs/connecting_to_the_database.html for available options passed, in addition to the above.
ExtraOptions string `json:"options"`
ExtraOptions string `json:"options"`
LogLevel logger.LogLevel `json:"log_level"`
}

// Database config. Contains values necessary to open a database connection.
Expand Down
49 changes: 21 additions & 28 deletions pkg/repositories/config/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,29 @@ package config
import (
"fmt"

"gorm.io/gorm/logger"

"github.com/flyteorg/flytestdlib/promutils"

"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres" // Required to import database driver.
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

const Postgres = "postgres"

// Generic interface for providing a config necessary to open a database connection.
type DbConnectionConfigProvider interface {
// Returns the database type. For instance PostgreSQL or MySQL.
GetType() string
// Returns arguments specific for the database type necessary to open a database connection.
GetArgs() string
// Enables verbose logging.
WithDebugModeEnabled()
// Disables verbose logging.
WithDebugModeDisabled()
// Returns whether verbose logging is enabled or not.
IsDebug() bool
// Returns database dialector
GetDialector() gorm.Dialector

GetDBConfig() DbConfig

GetDSN() string
}

type BaseConfig struct {
IsDebug bool
LogLevel logger.LogLevel `json:"log_level"`
DisableForeignKeyConstraintWhenMigrating bool
}

// PostgreSQL implementation for DbConnectionConfigProvider.
Expand All @@ -43,11 +42,7 @@ func NewPostgresConfigProvider(config DbConfig, scope promutils.Scope) DbConnect
}
}

func (p *PostgresConfigProvider) GetType() string {
return Postgres
}

func (p *PostgresConfigProvider) GetArgs() string {
func (p *PostgresConfigProvider) GetDSN() string {
if p.config.Password == "" {
// Switch for development
return fmt.Sprintf("host=%s port=%d dbname=%s user=%s sslmode=disable",
Expand All @@ -57,25 +52,23 @@ func (p *PostgresConfigProvider) GetArgs() string {
p.config.Host, p.config.Port, p.config.DbName, p.config.User, p.config.Password, p.config.ExtraOptions)
}

func (p *PostgresConfigProvider) WithDebugModeEnabled() {
p.config.IsDebug = true
}

func (p *PostgresConfigProvider) WithDebugModeDisabled() {
p.config.IsDebug = false
func (p *PostgresConfigProvider) GetDialector() gorm.Dialector {
return postgres.Open(p.GetDSN())
}

func (p *PostgresConfigProvider) IsDebug() bool {
return p.config.IsDebug
func (p *PostgresConfigProvider) GetDBConfig() DbConfig {
return p.config
}

// Opens a connection to the database specified in the config.
// You must call CloseDbConnection at the end of your session!
func OpenDbConnection(config DbConnectionConfigProvider) (*gorm.DB, error) {
db, err := gorm.Open(config.GetType(), config.GetArgs())
db, err := gorm.Open(config.GetDialector(), &gorm.Config{
Logger: logger.Default.LogMode(config.GetDBConfig().LogLevel),
DisableForeignKeyConstraintWhenMigrating: config.GetDBConfig().DisableForeignKeyConstraintWhenMigrating,
})
if err != nil {
return nil, err
}
db.LogMode(config.IsDebug())
return db, nil
}
14 changes: 11 additions & 3 deletions pkg/repositories/config/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package config
import (
"testing"

"gorm.io/gorm/logger"

mockScope "github.com/flyteorg/flytestdlib/promutils"

"github.com/stretchr/testify/assert"
Expand All @@ -15,9 +17,15 @@ func TestConstructGormArgs(t *testing.T) {
DbName: "postgres",
User: "postgres",
ExtraOptions: "sslmode=disable",
BaseConfig: BaseConfig{
LogLevel: 3,
DisableForeignKeyConstraintWhenMigrating: true,
},
}, mockScope.NewTestScope())

assert.Equal(t, "host=localhost port=5432 dbname=postgres user=postgres sslmode=disable", postgresConfigProvider.GetArgs())
assert.Equal(t, "host=localhost port=5432 dbname=postgres user=postgres sslmode=disable", postgresConfigProvider.GetDSN())
assert.Equal(t, logger.LogLevel(3), postgresConfigProvider.GetDBConfig().LogLevel)
assert.Equal(t, true, postgresConfigProvider.GetDBConfig().DisableForeignKeyConstraintWhenMigrating)
}

func TestConstructGormArgsWithPassword(t *testing.T) {
Expand All @@ -30,7 +38,7 @@ func TestConstructGormArgsWithPassword(t *testing.T) {
ExtraOptions: "sslmode=enable",
}, mockScope.NewTestScope())

assert.Equal(t, "host=localhost port=5432 dbname=postgres user=postgres password=pass sslmode=enable", postgresConfigProvider.GetArgs())
assert.Equal(t, "host=localhost port=5432 dbname=postgres user=postgres password=pass sslmode=enable", postgresConfigProvider.GetDSN())
}

func TestConstructGormArgsWithPasswordNoExtra(t *testing.T) {
Expand All @@ -42,5 +50,5 @@ func TestConstructGormArgsWithPasswordNoExtra(t *testing.T) {
Password: "pass",
}, mockScope.NewTestScope())

assert.Equal(t, "host=localhost port=5432 dbname=postgres user=postgres password=pass ", postgresConfigProvider.GetArgs())
assert.Equal(t, "host=localhost port=5432 dbname=postgres user=postgres password=pass ", postgresConfigProvider.GetDSN())
}
2 changes: 1 addition & 1 deletion pkg/repositories/errors/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"

"github.com/flyteorg/datacatalog/pkg/errors"
"github.com/jinzhu/gorm"
"github.com/lib/pq"
"google.golang.org/grpc/codes"
"gorm.io/gorm"
)

// Postgres error codes
Expand Down
28 changes: 15 additions & 13 deletions pkg/repositories/gormimpl/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package gormimpl
import (
"context"

"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/datacatalog"

"github.com/flyteorg/datacatalog/pkg/common"
"github.com/flyteorg/datacatalog/pkg/repositories/errors"
"github.com/flyteorg/datacatalog/pkg/repositories/interfaces"
"github.com/flyteorg/datacatalog/pkg/repositories/models"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/datacatalog"
"github.com/flyteorg/flytestdlib/promutils"
"github.com/jinzhu/gorm"
"gorm.io/gorm"
)

type artifactRepo struct {
Expand Down Expand Up @@ -65,19 +66,20 @@ func (h *artifactRepo) Get(ctx context.Context, in models.ArtifactKey) (models.A
)

if result.Error != nil {
if result.Error.Error() == gorm.ErrRecordNotFound.Error() {
return models.Artifact{}, errors.GetMissingEntityError("Artifact", &datacatalog.Artifact{
Dataset: &datacatalog.DatasetID{
Project: in.DatasetProject,
Domain: in.DatasetDomain,
Name: in.DatasetName,
Version: in.DatasetVersion,
},
Id: in.ArtifactID,
})
}

return models.Artifact{}, h.errorTransformer.ToDataCatalogError(result.Error)
}
if result.RecordNotFound() {
return models.Artifact{}, errors.GetMissingEntityError("Artifact", &datacatalog.Artifact{
Dataset: &datacatalog.DatasetID{
Project: in.DatasetProject,
Domain: in.DatasetDomain,
Name: in.DatasetName,
Version: in.DatasetVersion,
},
Id: in.ArtifactID,
})
}

return artifact, nil
}
Expand Down
Loading

0 comments on commit 7892458

Please sign in to comment.