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

Commit

Permalink
Gorm upgrade (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmahindrakar-oss authored Dec 17, 2021
1 parent f60baa3 commit 234c6c8
Show file tree
Hide file tree
Showing 57 changed files with 682 additions and 571 deletions.
17 changes: 12 additions & 5 deletions cmd/entrypoints/clusterresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ import (

"github.com/flyteorg/flyteadmin/pkg/clusterresource"
executioncluster "github.com/flyteorg/flyteadmin/pkg/executioncluster/impl"

"github.com/flyteorg/flyteadmin/pkg/runtime"

"github.com/flyteorg/flytestdlib/logger"

"github.com/flyteorg/flyteadmin/pkg/config"
"github.com/flyteorg/flyteadmin/pkg/repositories"
repositoryConfig "github.com/flyteorg/flyteadmin/pkg/repositories/config"
"github.com/flyteorg/flytestdlib/promutils"
_ "github.com/jinzhu/gorm/dialects/postgres" // Required to import database driver.
"github.com/spf13/cobra"
_ "gorm.io/driver/postgres" // Required to import database driver.
gormLogger "gorm.io/gorm/logger"
)

var parentClusterResourceCmd = &cobra.Command{
Expand All @@ -40,9 +39,13 @@ var controllerRunCmd = &cobra.Command{
configuration := runtime.NewConfigurationProvider()
scope := promutils.NewScope(configuration.ApplicationConfiguration().GetTopLevelConfig().MetricsScope).NewSubScope("clusterresource")
dbConfigValues := configuration.ApplicationConfiguration().GetDbConfig()
dbLogLevel := gormLogger.Silent
if dbConfigValues.Debug {
dbLogLevel = gormLogger.Info
}
dbConfig := repositoryConfig.DbConfig{
BaseConfig: repositoryConfig.BaseConfig{
IsDebug: dbConfigValues.Debug,
LogLevel: dbLogLevel,
},
Host: dbConfigValues.Host,
Port: dbConfigValues.Port,
Expand Down Expand Up @@ -76,9 +79,13 @@ var controllerSyncCmd = &cobra.Command{
configuration := runtime.NewConfigurationProvider()
scope := promutils.NewScope(configuration.ApplicationConfiguration().GetTopLevelConfig().MetricsScope).NewSubScope("clusterresource")
dbConfigValues := configuration.ApplicationConfiguration().GetDbConfig()
dbLogLevel := gormLogger.Silent
if dbConfigValues.Debug {
dbLogLevel = gormLogger.Info
}
dbConfig := repositoryConfig.DbConfig{
BaseConfig: repositoryConfig.BaseConfig{
IsDebug: dbConfigValues.Debug,
LogLevel: dbLogLevel,
},
Host: dbConfigValues.Host,
Port: dbConfigValues.Port,
Expand Down
92 changes: 71 additions & 21 deletions cmd/entrypoints/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package entrypoints
import (
"context"

"github.com/flyteorg/flyteadmin/pkg/repositories/config"
"github.com/flyteorg/flyteadmin/pkg/runtime"

"github.com/flyteorg/flytestdlib/promutils"

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

"github.com/flyteorg/flyteadmin/pkg/repositories/config"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres" // Required to import database driver.
"github.com/go-gormigrate/gormigrate/v2"
"github.com/spf13/cobra"
gormigrate "gopkg.in/gormigrate.v1"
"gorm.io/driver/postgres"
_ "gorm.io/driver/postgres" // Required to import database driver.
"gorm.io/gorm"
gormLogger "gorm.io/gorm/logger"
)

var parentMigrateCmd = &cobra.Command{
Expand All @@ -33,9 +33,14 @@ var migrateCmd = &cobra.Command{
ctx := context.Background()
configuration := runtime.NewConfigurationProvider()
databaseConfig := configuration.ApplicationConfiguration().GetDbConfig()
dbLogLevel := gormLogger.Silent
if databaseConfig.Debug {
dbLogLevel = gormLogger.Info
}
postgresConfigProvider := config.NewPostgresConfigProvider(config.DbConfig{
BaseConfig: config.BaseConfig{
IsDebug: databaseConfig.Debug,
LogLevel: dbLogLevel,
DisableForeignKeyConstraintWhenMigrating: true,
},
Host: databaseConfig.Host,
Port: databaseConfig.Port,
Expand All @@ -44,16 +49,28 @@ var migrateCmd = &cobra.Command{
Password: databaseConfig.Password,
ExtraOptions: databaseConfig.ExtraOptions,
}, migrateScope)
db, err := gorm.Open(postgresConfigProvider.GetType(), postgresConfigProvider.GetArgs())
db, err := gorm.Open(postgres.Open(postgresConfigProvider.GetDSN()), &gorm.Config{
Logger: gormLogger.Default.LogMode(postgresConfigProvider.GetDBConfig().LogLevel),
DisableForeignKeyConstraintWhenMigrating: postgresConfigProvider.GetDBConfig().DisableForeignKeyConstraintWhenMigrating,
})
if err != nil {
logger.Fatal(ctx, err)
}
defer db.Close()
db.LogMode(true)
if err = db.DB().Ping(); err != nil {

sqlDB, err := db.DB()
if err != nil {
logger.Fatal(ctx, err)
}

defer func(deferCtx context.Context) {
if err = sqlDB.Close(); err != nil {
logger.Fatal(deferCtx, err)
}
}(ctx)

if err = sqlDB.Ping(); err != nil {
logger.Fatal(ctx, err)
}
m := gormigrate.New(db, gormigrate.DefaultOptions, config.Migrations)
if err = m.Migrate(); err != nil {
logger.Fatalf(ctx, "Could not migrate: %v", err)
Expand All @@ -70,9 +87,13 @@ var rollbackCmd = &cobra.Command{
ctx := context.Background()
configuration := runtime.NewConfigurationProvider()
databaseConfig := configuration.ApplicationConfiguration().GetDbConfig()
dbLogLevel := gormLogger.Silent
if databaseConfig.Debug {
dbLogLevel = gormLogger.Info
}
postgresConfigProvider := config.NewPostgresConfigProvider(config.DbConfig{
BaseConfig: config.BaseConfig{
IsDebug: databaseConfig.Debug,
LogLevel: dbLogLevel,
},
Host: databaseConfig.Host,
Port: databaseConfig.Port,
Expand All @@ -82,13 +103,23 @@ var rollbackCmd = &cobra.Command{
ExtraOptions: databaseConfig.ExtraOptions,
}, rollbackScope)

db, err := gorm.Open(postgresConfigProvider.GetType(), postgresConfigProvider.GetArgs())
db, err := gorm.Open(postgres.Open(postgresConfigProvider.GetDSN()), &gorm.Config{
Logger: gormLogger.Default.LogMode(postgresConfigProvider.GetDBConfig().LogLevel),
})
if err != nil {
logger.Fatal(ctx, err)
}
defer db.Close()
db.LogMode(true)
if err = db.DB().Ping(); err != nil {
sqlDB, err := db.DB()
if err != nil {
logger.Fatal(ctx, err)
}
defer func(deferCtx context.Context) {
if err = sqlDB.Close(); err != nil {
logger.Fatal(deferCtx, err)
}
}(ctx)

if err = sqlDB.Ping(); err != nil {
logger.Fatal(ctx, err)
}

Expand All @@ -109,9 +140,13 @@ var seedProjectsCmd = &cobra.Command{
ctx := context.Background()
configuration := runtime.NewConfigurationProvider()
databaseConfig := configuration.ApplicationConfiguration().GetDbConfig()
dbLogLevel := gormLogger.Silent
if databaseConfig.Debug {
dbLogLevel = gormLogger.Info
}
postgresConfigProvider := config.NewPostgresConfigProvider(config.DbConfig{
BaseConfig: config.BaseConfig{
IsDebug: databaseConfig.Debug,
LogLevel: dbLogLevel,
},
Host: databaseConfig.Host,
Port: databaseConfig.Port,
Expand All @@ -120,12 +155,27 @@ var seedProjectsCmd = &cobra.Command{
Password: databaseConfig.Password,
ExtraOptions: databaseConfig.ExtraOptions,
}, migrateScope)
db, err := gorm.Open(postgresConfigProvider.GetType(), postgresConfigProvider.GetArgs())
db, err := gorm.Open(postgres.Open(postgresConfigProvider.GetDSN()), &gorm.Config{
Logger: gormLogger.Default.LogMode(postgresConfigProvider.GetDBConfig().LogLevel),
})
if err != nil {
logger.Fatal(ctx, err)
}
defer db.Close()
db.LogMode(true)

sqlDB, err := db.DB()
if err != nil {
logger.Fatal(ctx, err)
}

defer func(deferCtx context.Context) {
if err = sqlDB.Close(); err != nil {
logger.Fatal(deferCtx, err)
}
}(ctx)

if err = sqlDB.Ping(); err != nil {
logger.Fatal(ctx, err)
}

if err = config.SeedProjects(db, args); err != nil {
logger.Fatalf(ctx, "Could not add projects to database with err: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/scheduler/entrypoints/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"github.com/flyteorg/flytestdlib/promutils"
"github.com/flyteorg/flytestdlib/promutils/labeled"

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

var schedulerRunCmd = &cobra.Command{
Expand Down
25 changes: 16 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/flyteorg/flytepropeller v0.14.11
github.com/flyteorg/flytestdlib v0.4.7
github.com/ghodss/yaml v1.0.0
github.com/go-gormigrate/gormigrate/v2 v2.0.0
github.com/gogo/protobuf v1.3.2
github.com/golang-jwt/jwt/v4 v4.1.0
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
Expand All @@ -29,17 +30,15 @@ require (
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/gtank/cryptopasta v0.0.0-20170601214702-1f550f6f2f69
github.com/jinzhu/gorm v1.9.16
github.com/jackc/pgconn v1.10.0
github.com/lestrrat-go/jwx v1.1.6
github.com/lib/pq v1.10.0
github.com/magiconair/properties v1.8.4
github.com/mitchellh/mapstructure v1.4.1
github.com/ory/fosite v0.39.0
github.com/ory/x v0.0.162
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.9.0
github.com/prometheus/client_model v0.2.0
github.com/qor/validations v0.0.0-20171228122639-f364bca61b46
github.com/robfig/cron/v3 v3.0.0
github.com/sendgrid/sendgrid-go v3.10.0+incompatible
github.com/spf13/cobra v1.1.3
Expand All @@ -51,7 +50,8 @@ require (
google.golang.org/genproto v0.0.0-20210315173758-2651cd453018
google.golang.org/grpc v1.36.0
google.golang.org/protobuf v1.25.0
gopkg.in/gormigrate.v1 v1.6.0
gorm.io/driver/postgres v1.2.1
gorm.io/gorm v1.22.4
k8s.io/api v0.20.4
k8s.io/apimachinery v0.20.4
k8s.io/client-go v0.20.2
Expand Down Expand Up @@ -95,8 +95,15 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // 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.1.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.8.1 // indirect
github.com/jackc/pgx/v4 v4.13.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.1 // indirect
github.com/jinzhu/now v1.1.3 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.10 // indirect
github.com/jstemmer/go-junit-report v0.9.1 // indirect
Expand All @@ -113,6 +120,7 @@ require (
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/ncw/swift v1.0.53 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/ory/go-acc v0.2.5 // indirect
github.com/ory/go-convenience v0.1.0 // indirect
github.com/ory/viper v1.7.5 // indirect
Expand All @@ -123,7 +131,6 @@ require (
github.com/pquerna/cachecontrol v0.0.0-20201205024021-ac21108117ac // indirect
github.com/prometheus/common v0.19.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/qor/qor v1.2.0 // indirect
github.com/sendgrid/rest v2.6.4+incompatible // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/afero v1.5.1 // indirect
Expand All @@ -133,14 +140,14 @@ require (
github.com/stretchr/objx v0.3.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b // indirect
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa // indirect
golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
golang.org/x/text v0.3.5 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.2 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
Loading

0 comments on commit 234c6c8

Please sign in to comment.