Skip to content
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
go.mod
go.sum
tags
cmd/routing-api/routing-api
/routing-api
Expand Down
22 changes: 19 additions & 3 deletions cmd/routing-api/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ import (
"os"
"time"

"gorm.io/driver/mysql"
"gorm.io/driver/postgres"

routingAPI "code.cloudfoundry.org/routing-api"
"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 @@ -161,7 +164,7 @@ var _ = Describe("Main", func() {
routingAPIConfig := testrunner.GetRoutingAPIConfig(defaultConfig)
connectionString, err := db.ConnectionString(&routingAPIConfig.SqlDB)
Expect(err).NotTo(HaveOccurred())
gormDB, err := gorm.Open(routingAPIConfig.SqlDB.Type, connectionString)
gormDB, err := gorm.Open(getGormDialect(routingAPIConfig.SqlDB.Type, connectionString), &gorm.Config{})
Expect(err).NotTo(HaveOccurred())

getRoutes := func() string {
Expand Down Expand Up @@ -241,7 +244,7 @@ var _ = Describe("Main", func() {
}
connectionString, err := db.ConnectionString(&routingAPIConfig.SqlDB)
Expect(err).NotTo(HaveOccurred())
gormDB, err = gorm.Open(routingAPIConfig.SqlDB.Type, connectionString)
gormDB, err = gorm.Open(getGormDialect(routingAPIConfig.SqlDB.Type, connectionString), &gorm.Config{})
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
Expand All @@ -260,3 +263,16 @@ var _ = Describe("Main", func() {
})
})
})

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
}
24 changes: 16 additions & 8 deletions cmd/routing-api/routing_api_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ var (

func TestRoutingAPI(test *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(test, "Routing API Test Suite")
suiteConfig, reporterConfig := GinkgoConfiguration()
RunSpecs(test, "Routing API Test Suite", suiteConfig, reporterConfig)
}

var _ = SynchronizedBeforeSuite(
Expand All @@ -77,8 +78,9 @@ var _ = SynchronizedBeforeSuite(
grpclog.SetLoggerV2(grpclog.NewLoggerV2(io.Discard, io.Discard, io.Discard))

path := string(binPaths)
routingAPIBinPath = strings.Split(path, ",")[0]
locketBinPath = strings.Split(path, ",")[1]
parts := strings.Split(path, ",")
routingAPIBinPath = parts[0]
locketBinPath = parts[1]

SetDefaultEventuallyTimeout(15 * time.Second)

Expand Down Expand Up @@ -113,13 +115,19 @@ var _ = SynchronizedBeforeSuite(
)

var _ = SynchronizedAfterSuite(func() {
err := dbAllocator.Delete()
Expect(err).NotTo(HaveOccurred())
if dbAllocator != nil {
err := dbAllocator.Delete()
Expect(err).NotTo(HaveOccurred())
}

oAuthServer.Close()
if oAuthServer != nil {
oAuthServer.Close()
}

err = os.Remove(uaaCACertsPath)
Expect(err).NotTo(HaveOccurred())
if uaaCACertsPath != "" {
err := os.Remove(uaaCACertsPath)
Expect(err).NotTo(HaveOccurred())
}
}, func() {
gexec.CleanupBuildArtifacts()
})
Expand Down
3 changes: 2 additions & 1 deletion cmd/routing-api/testrunner/constants.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package testrunner

import (
"code.cloudfoundry.org/routing-api/config"
"os"

"code.cloudfoundry.org/routing-api/config"
)

const (
Expand Down
6 changes: 3 additions & 3 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 Expand Up @@ -63,7 +63,7 @@ func (a *postgresAllocator) Create() (*config.SqlDB, error) {
if err != nil {
return nil, err
}
a.sqlDB, err = sql.Open("postgres", connStr)
a.sqlDB, err = sql.Open("pgx", connStr)
if err != nil {
return nil, err
}
Expand Down
54 changes: 32 additions & 22 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,21 +13,23 @@ 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
ExecWithError(query string, args ...interface{}) error
Rows(tableName string) (*sql.Rows, error)
DropColumn(column string) error
Dialect() gorm.Dialect
Dialect() gorm.Dialector
Migrator() gorm.Migrator
}

type gormClient struct {
Expand All @@ -38,25 +40,25 @@ 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.db.Migrator().DropColumn(c.db.Statement.Table, name)
}
func (c *gormClient) Close() error {
return c.db.Close()
sqlDB, err := c.db.DB()
if err != nil {
return err
}
return sqlDB.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) Dialect() gorm.Dialect {
return c.db.Dialect()
func (c *gormClient) Dialect() gorm.Dialector {
return c.db.Dialector
}

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 @@ -85,8 +87,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 @@ -99,7 +101,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 @@ -117,14 +119,22 @@ 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 {
dbClient := c.db.Exec(query, args)
dbClient := c.db.Exec(query, args...)
return dbClient.RowsAffected
}

func (c *gormClient) ExecWithError(query string, args ...interface{}) error {
return c.db.Exec(query, args...).Error
}

func (c *gormClient) Migrator() gorm.Migrator {
return c.db.Migrator()
}

func (c *gormClient) Rows(tablename string) (*sql.Rows, error) {
tableDb := c.db.Table(tablename)
return tableDb.Rows()
Expand Down
39 changes: 26 additions & 13 deletions db/db_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import (
"sync/atomic"
"time"

"gorm.io/driver/mysql"
"gorm.io/driver/postgres"

"code.cloudfoundry.org/clock"
"code.cloudfoundry.org/eventhub"
"code.cloudfoundry.org/lager/v3"
"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/gorm"
)

//go:generate counterfeiter -o fakes/fake_db.go . DB
Expand Down Expand Up @@ -102,27 +103,38 @@ var DeleteRouterGroupError = DBError{Type: KeyNotFound, Message: "Delete Fails:

func NewSqlDB(cfg *config.SqlDB) (*SqlDB, error) {
if cfg == nil {
return nil, errors.New("SQL configuration cannot be nil")
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{}, fmt.Errorf("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)
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 All @@ -140,7 +152,7 @@ func (s *SqlDB) FindExpiredRoutes(routes interface{}, c clock.Clock) error {
// postgres stores at microsecond precision. we subtract a second from expiry time to give
// us an extra second of buffer to account for rounding issues:
// if we tell the db to save an expiry of 5.3s, and we query at 5.2s, mysql will think it expired,
// as the db will compare 5s against 5.2s. Oops.
// as the db will compare 5s against 5.2s. Oops.
return s.Client.Find(routes, "expires_at < ?", c.Now().Add(-1*time.Second))
}

Expand Down Expand Up @@ -295,7 +307,8 @@ func (s *SqlDB) DeleteRouterGroup(guid string) error {
return DeleteRouterGroupError
}

_, err = s.Client.Delete(&routerGroup)
// Use WHERE clause to delete the specific router group by guid
_, err = s.Client.Where("guid = ?", guid).Delete(&models.RouterGroupDB{})
if err != nil {
return err
}
Expand Down
Loading