Skip to content

[WIP | Help needed] Migrate to new Gorm version #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
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
30 changes: 23 additions & 7 deletions cmd/routing-api/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main_test

import (
"fmt"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"net/http"
"os"
"os/exec"
Expand All @@ -11,14 +13,14 @@ import (
"code.cloudfoundry.org/routing-api/cmd/routing-api/testrunner"
"code.cloudfoundry.org/routing-api/db"
"code.cloudfoundry.org/routing-api/models"
"github.com/jinzhu/gorm"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gbytes"
. "github.com/onsi/gomega/gexec"
"github.com/onsi/gomega/ghttp"
"github.com/tedsuo/ifrit"
ginkgomon "github.com/tedsuo/ifrit/ginkgomon_v2"
"gorm.io/gorm"
)

const (
Expand Down Expand Up @@ -162,7 +164,8 @@ var _ = Describe("Main", func() {
rapiConfig := getRoutingAPIConfig(defaultConfig)
connectionString, err := db.ConnectionString(&rapiConfig.SqlDB)
Expect(err).NotTo(HaveOccurred())
gormDB, err := gorm.Open(rapiConfig.SqlDB.Type, connectionString)

gormDB, err := gorm.Open(getGormDialect(rapiConfig.SqlDB.Type, connectionString), &gorm.Config{})
Expect(err).NotTo(HaveOccurred())

getRoutes := func() string {
Expand Down Expand Up @@ -239,13 +242,13 @@ var _ = Describe("Main", func() {
}
connectionString, err := db.ConnectionString(&rapiConfig.SqlDB)
Expect(err).NotTo(HaveOccurred())
gormDB, err = gorm.Open(rapiConfig.SqlDB.Type, connectionString)
gormDB, err = gorm.Open(getGormDialect(rapiConfig.SqlDB.Type, connectionString), &gorm.Config{})
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
gormDB.AutoMigrate(&models.RouterGroupDB{})
Expect(os.Remove(configPath)).To(Succeed())
})
/* AfterEach(func() {
gormDB.AutoMigrate(&models.RouterGroupDB{})
Expect(os.Remove(configPath)).To(Succeed())
})*/
It("should fail with an error", func() {
routingAPIRunner := testrunner.New(routingAPIBinPath, routingAPIArgs)
proc := ifrit.Invoke(routingAPIRunner)
Expand All @@ -265,3 +268,16 @@ func RoutingApi(args ...string) *Session {

return session
}

func getGormDialect(databaseType string, connectionString string) gorm.Dialector {
var dialect gorm.Dialector

switch databaseType {
case "postgres":
dialect = postgres.Open(connectionString)
case "mysql":
dialect = mysql.Open(connectionString)
}

return dialect
}
6 changes: 4 additions & 2 deletions cmd/routing-api/routing_api_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/onsi/gomega/ghttp"
"google.golang.org/grpc/grpclog"
yaml "gopkg.in/yaml.v2"
"gorm.io/gorm"
)

var (
Expand Down Expand Up @@ -63,9 +64,10 @@ var (
mtlsAPIClientCert tls.Certificate
)

func TestMain(t *testing.T) {
func TestMainSuite(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Main Suite")
suiteConfig, reporterConfig := GinkgoConfiguration()
RunSpecs(t, "Main Suite", suiteConfig, reporterConfig)
}

var _ = SynchronizedBeforeSuite(
Expand Down
4 changes: 2 additions & 2 deletions cmd/routing-api/testrunner/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"code.cloudfoundry.org/routing-api/db"

"code.cloudfoundry.org/routing-api/config"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
. "github.com/onsi/ginkgo/v2"
_ "gorm.io/driver/mysql"
_ "gorm.io/driver/postgres"
)

type DbAllocator interface {
Expand Down
32 changes: 14 additions & 18 deletions db/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package db
import (
"database/sql"

"github.com/jinzhu/gorm"
"gorm.io/gorm"
)

//go:generate counterfeiter -o fakes/fake_client.go . Client
Expand All @@ -13,16 +13,16 @@ type Client interface {
Create(value interface{}) (int64, error)
Delete(value interface{}, where ...interface{}) (int64, error)
Save(value interface{}) (int64, error)
Update(attrs ...interface{}) (int64, error)
Update(column string, value interface{}) (int64, error)
First(out interface{}, where ...interface{}) error
Find(out interface{}, where ...interface{}) error
AutoMigrate(values ...interface{}) error
Begin() Client
Rollback() error
Commit() error
HasTable(value interface{}) bool
AddUniqueIndex(indexName string, columns ...string) (Client, error)
RemoveIndex(indexName string) (Client, error)
AddUniqueIndex(indexName string, columns interface{}) error
RemoveIndex(indexName string, columns interface{}) error
Model(value interface{}) Client
Exec(query string, args ...interface{}) int64
Rows(tableName string) (*sql.Rows, error)
Expand All @@ -37,21 +37,17 @@ func NewGormClient(db *gorm.DB) Client {
return &gormClient{db: db}
}
func (c *gormClient) DropColumn(name string) error {
return c.db.DropColumn(name).Error
return c.DropColumn(name)
}
func (c *gormClient) Close() error {
return c.db.Close()
return c.Close()
}
func (c *gormClient) AddUniqueIndex(indexName string, columns ...string) (Client, error) {
var newClient gormClient
newClient.db = c.db.AddUniqueIndex(indexName, columns...)
return &newClient, newClient.db.Error
func (c *gormClient) AddUniqueIndex(indexName string, columns interface{}) error {
return c.db.Migrator().CreateIndex(columns, indexName)
}

func (c *gormClient) RemoveIndex(indexName string) (Client, error) {
var newClient gormClient
newClient.db = c.db.RemoveIndex(indexName)
return &newClient, newClient.db.Error
func (c *gormClient) RemoveIndex(indexName string, columns interface{}) error {
return c.db.Migrator().DropIndex(columns, indexName)
}

func (c *gormClient) Model(value interface{}) Client {
Expand Down Expand Up @@ -80,8 +76,8 @@ func (c *gormClient) Save(value interface{}) (int64, error) {
return newDb.RowsAffected, newDb.Error
}

func (c *gormClient) Update(attrs ...interface{}) (int64, error) {
newDb := c.db.Update(attrs...)
func (c *gormClient) Update(column string, value interface{}) (int64, error) {
newDb := c.db.Update(column, value)
return newDb.RowsAffected, newDb.Error
}

Expand All @@ -94,7 +90,7 @@ func (c *gormClient) Find(out interface{}, where ...interface{}) error {
}

func (c *gormClient) AutoMigrate(values ...interface{}) error {
return c.db.AutoMigrate(values...).Error
return c.db.AutoMigrate(values...)
}

func (c *gormClient) Begin() Client {
Expand All @@ -112,7 +108,7 @@ func (c *gormClient) Commit() error {
}

func (c *gormClient) HasTable(value interface{}) bool {
return c.db.HasTable(value)
return c.db.Migrator().HasTable(value)
}

func (c *gormClient) Exec(query string, args ...interface{}) int64 {
Expand Down
35 changes: 25 additions & 10 deletions db/db_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"context"
"errors"
"fmt"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand All @@ -16,9 +19,9 @@ import (
"code.cloudfoundry.org/routing-api/config"
"code.cloudfoundry.org/routing-api/models"

"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "gorm.io/driver/mysql"
_ "gorm.io/driver/postgres"
"gorm.io/gorm"
)

//go:generate counterfeiter -o fakes/fake_db.go . DB
Expand Down Expand Up @@ -104,24 +107,36 @@ func NewSqlDB(cfg *config.SqlDB) (*SqlDB, error) {
return nil, errors.New("SQL configuration cannot be nil")
}

if cfg.Type != "mysql" && cfg.Type != "postgres" {
return &SqlDB{}, fmt.Errorf("Unknown type %s", cfg.Type)
connStr, err := ConnectionString(cfg)
if err != nil {
return nil, err
}

connStr, err := ConnectionString(cfg)
var dialect gorm.Dialector
switch cfg.Type {
case "postgres":
dialect = postgres.Open(connStr)
case "mysql":
dialect = mysql.Open(connStr)
default:
return &SqlDB{}, errors.New(fmt.Sprintf("Unknown type %s", cfg.Type))
}

db, err := gorm.Open(dialect, &gorm.Config{})
if err != nil {
return nil, err
}

db, err := gorm.Open(cfg.Type, connStr)
// Use the connection pool and setup it
sqlDB, err := db.DB()
if err != nil {
return nil, err
}

db.DB().SetMaxIdleConns(cfg.MaxIdleConns)
db.DB().SetMaxOpenConns(cfg.MaxOpenConns)
sqlDB.SetMaxIdleConns(cfg.MaxIdleConns)
sqlDB.SetMaxOpenConns(cfg.MaxOpenConns)
connMaxLifetime := time.Duration(cfg.ConnMaxLifetime) * time.Second
db.DB().SetConnMaxLifetime(connMaxLifetime)
sqlDB.SetConnMaxLifetime(connMaxLifetime)

tcpEventHub := eventhub.NewNonBlocking(1024)
httpEventHub := eventhub.NewNonBlocking(1024)
Expand Down
2 changes: 1 addition & 1 deletion db/db_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"code.cloudfoundry.org/routing-api/cmd/routing-api/testrunner"
"code.cloudfoundry.org/routing-api/config"
_ "github.com/lib/pq"
_ "github.com/jackc/pgx/v5/stdlib"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
Expand Down
Loading