Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

[wip] Move db logic to stdlib #86

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ require (
github.com/flyteorg/flytestdlib v1.0.11
github.com/golang/glog v1.0.0
github.com/golang/protobuf v1.5.2
github.com/jackc/pgconn v1.10.1
github.com/jackc/pgconn v1.13.0
github.com/mitchellh/mapstructure v1.4.3
github.com/satori/go.uuid v1.2.0
github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.1
github.com/stretchr/testify v1.8.0
google.golang.org/grpc v1.46.0
gorm.io/driver/postgres v1.2.3
gorm.io/driver/postgres v1.4.5
gorm.io/driver/sqlite v1.1.1
gorm.io/gorm v1.22.4
gorm.io/gorm v1.24.1-0.20221019064659-5dd2bb482755
)

require (
Expand Down Expand Up @@ -46,6 +46,7 @@ require (
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-logr/logr v0.4.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/gofrs/uuid v4.2.0+incompatible // indirect
github.com/golang-jwt/jwt/v4 v4.4.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand All @@ -57,12 +58,12 @@ require (
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.2.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.9.0 // indirect
github.com/jackc/pgx/v4 v4.14.0 // indirect
github.com/jackc/pgtype v1.12.0 // indirect
github.com/jackc/pgx/v4 v4.17.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.4 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
Expand All @@ -83,10 +84,10 @@ require (
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.11.0 // indirect
github.com/stretchr/objx v0.3.0 // indirect
github.com/stretchr/objx v0.4.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect
Expand All @@ -99,8 +100,11 @@ require (
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/mysql v1.4.4 // indirect
k8s.io/apimachinery v0.20.2 // indirect
k8s.io/client-go v0.0.0-20210217172142-7279fc64d847 // indirect
k8s.io/klog/v2 v2.5.0 // indirect
)

replace github.com/flyteorg/flytestdlib => ../flytestdlib
380 changes: 114 additions & 266 deletions go.sum

Large diffs are not rendered by default.

81 changes: 0 additions & 81 deletions pkg/repositories/config/postgres.go

This file was deleted.

52 changes: 0 additions & 52 deletions pkg/repositories/config/postgres_test.go

This file was deleted.

28 changes: 28 additions & 0 deletions pkg/repositories/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package repositories

import (
"github.com/flyteorg/flytestdlib/database"
stdlibLogger "github.com/flyteorg/flytestdlib/logger"
"gorm.io/gorm"

"context"
)

const (
Postgres = "postgres"
Sqlite = "sqlite"
)

// OpenDbConnection opens a connection to the database specified in the config.
// You must call CloseDbConnection at the end of your session!
func OpenDbConnection(ctx context.Context, dbConfig database.DbConfig) (*gorm.DB, error) {
gormConfig := &gorm.Config{
Logger: database.GetGormLogger(ctx, stdlibLogger.GetConfig()),
DisableForeignKeyConstraintWhenMigrating: !dbConfig.EnableForeignKeyConstraintWhenMigrating,
}
db, err := NewDBHandle(ctx, dbConfig, gormConfig)
if err != nil {
return nil, err
}
return db.db, nil
}
37 changes: 37 additions & 0 deletions pkg/repositories/errors/generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package errors

import (
"errors"
"reflect"

"github.com/flyteorg/flytestdlib/logger"

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

type genericErrorTransformer struct {
}

func (p *genericErrorTransformer) fromGormError(err error) error {
switch err.Error() {
case gorm.ErrRecordNotFound.Error():
return catalogErrors.NewDataCatalogErrorf(codes.NotFound, "entry not found")
default:
logger.InfofNoCtx("Generic error detected. Error type: [%v]", reflect.TypeOf(err))
return catalogErrors.NewDataCatalogErrorf(codes.Internal, unexpectedType, err)
}
}

func (p *genericErrorTransformer) ToDataCatalogError(err error) error {
if unwrappedErr := errors.Unwrap(err); unwrappedErr != nil {
err = unwrappedErr
}

return p.fromGormError(err)
}

func NewGenericErrorTransformer() ErrorTransformer {
return &genericErrorTransformer{}
}
5 changes: 0 additions & 5 deletions pkg/repositories/errors/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,3 @@ func (p *postgresErrorTransformer) ToDataCatalogError(err error) error {
func NewPostgresErrorTransformer() ErrorTransformer {
return &postgresErrorTransformer{}
}

type ConnectError interface {
Unwrap() error
Error() string
}
38 changes: 12 additions & 26 deletions pkg/repositories/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,13 @@ package repositories

import (
"context"
"fmt"

"github.com/flyteorg/flytestdlib/database"

"github.com/flyteorg/datacatalog/pkg/repositories/config"
"github.com/flyteorg/datacatalog/pkg/repositories/errors"
"github.com/flyteorg/datacatalog/pkg/repositories/interfaces"
"github.com/flyteorg/flytestdlib/promutils"
)

type RepoConfig int32

const (
POSTGRES RepoConfig = 0
)

var RepositoryConfigurationName = map[RepoConfig]string{
POSTGRES: "POSTGRES",
}

// The RepositoryInterface indicates the methods that each Repository must support.
// A Repository indicates a Database which is collection of Tables/models.
// The goal is allow databases to be Plugged in easily.
Expand All @@ -32,18 +19,17 @@ type RepositoryInterface interface {
ReservationRepo() interfaces.ReservationRepo
}

func GetRepository(ctx context.Context, repoType RepoConfig, dbConfig database.DbConfig, scope promutils.Scope) RepositoryInterface {
switch repoType {
case POSTGRES:
db, err := config.OpenDbConnection(ctx, config.NewPostgresConfigProvider(dbConfig, scope.NewSubScope("postgres")))
if err != nil {
panic(err)
}
return NewPostgresRepo(
db,
errors.NewPostgresErrorTransformer(),
scope.NewSubScope("repositories"))
default:
panic(fmt.Sprintf("Invalid repoType %v", repoType))
func GetRepository(ctx context.Context, dbConfig database.DbConfig, scope promutils.Scope) RepositoryInterface {
db, err := OpenDbConnection(ctx, dbConfig)
if err != nil {
panic(err)
}

var errTransformer errors.ErrorTransformer
if !dbConfig.Postgres.IsEmpty() {
errTransformer = errors.NewPostgresErrorTransformer()
} else {
errTransformer = errors.NewGenericErrorTransformer()
}
return NewPostgresRepo(db, errTransformer, scope.NewSubScope("repositories"))
}
Loading