Skip to content

Commit 99f0138

Browse files
committed
First version
1 parent 8d0a58a commit 99f0138

7 files changed

Lines changed: 41 additions & 20 deletions

File tree

cmd/routing-api/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import (
1111
"code.cloudfoundry.org/routing-api/cmd/routing-api/testrunner"
1212
"code.cloudfoundry.org/routing-api/db"
1313
"code.cloudfoundry.org/routing-api/models"
14-
"github.com/jinzhu/gorm"
1514
. "github.com/onsi/ginkgo/v2"
1615
. "github.com/onsi/gomega"
1716
. "github.com/onsi/gomega/gbytes"
1817
. "github.com/onsi/gomega/gexec"
1918
"github.com/onsi/gomega/ghttp"
2019
"github.com/tedsuo/ifrit"
2120
ginkgomon "github.com/tedsuo/ifrit/ginkgomon_v2"
21+
"gorm.io/gorm"
2222
)
2323

2424
const (

cmd/routing-api/routing_api_suite_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/onsi/gomega/ghttp"
3333
"google.golang.org/grpc/grpclog"
3434
yaml "gopkg.in/yaml.v2"
35+
"gorm.io/gorm"
3536
)
3637

3738
var (
@@ -63,9 +64,10 @@ var (
6364
mtlsAPIClientCert tls.Certificate
6465
)
6566

66-
func TestMain(t *testing.T) {
67+
func TestMain(m *testing.M) {
6768
RegisterFailHandler(Fail)
68-
RunSpecs(t, "Main Suite")
69+
os.Exit(m.Run())
70+
RunSpecs(m, "Main Suite")
6971
}
7072

7173
var _ = SynchronizedBeforeSuite(

cmd/routing-api/testrunner/db.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"code.cloudfoundry.org/routing-api/db"
1111

1212
"code.cloudfoundry.org/routing-api/config"
13-
_ "github.com/jinzhu/gorm/dialects/mysql"
14-
_ "github.com/jinzhu/gorm/dialects/postgres"
1513
. "github.com/onsi/ginkgo/v2"
14+
_ "gorm.io/driver/mysql"
15+
_ "gorm.io/driver/postgres"
1616
)
1717

1818
type DbAllocator interface {

db/client.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package db
33
import (
44
"database/sql"
55

6-
"github.com/jinzhu/gorm"
6+
"gorm.io/gorm"
77
)
88

99
//go:generate counterfeiter -o fakes/fake_client.go . Client
@@ -37,14 +37,18 @@ func NewGormClient(db *gorm.DB) Client {
3737
return &gormClient{db: db}
3838
}
3939
func (c *gormClient) DropColumn(name string) error {
40-
return c.db.DropColumn(name).Error
40+
return c.DropColumn(name).Error()
4141
}
4242
func (c *gormClient) Close() error {
4343
return c.db.Close()
4444
}
4545
func (c *gormClient) AddUniqueIndex(indexName string, columns ...string) (Client, error) {
4646
var newClient gormClient
47-
newClient.db = c.db.AddUniqueIndex(indexName, columns...)
47+
newClient.db, err := c.AddUniqueIndex(indexName, columns...)
48+
if err != nil {
49+
50+
}
51+
4852
return &newClient, newClient.db.Error
4953
}
5054

db/db_sql.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"gorm.io/driver/mysql"
8+
"gorm.io/driver/postgres"
9+
"io/ioutil"
710
"os"
811
"path/filepath"
912
"runtime"
@@ -16,9 +19,9 @@ import (
1619
"code.cloudfoundry.org/routing-api/config"
1720
"code.cloudfoundry.org/routing-api/models"
1821

19-
"github.com/jinzhu/gorm"
20-
_ "github.com/jinzhu/gorm/dialects/mysql"
21-
_ "github.com/jinzhu/gorm/dialects/postgres"
22+
_ "gorm.io/driver/mysql"
23+
_ "gorm.io/driver/postgres"
24+
"gorm.io/gorm"
2225
)
2326

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

107-
if cfg.Type != "mysql" && cfg.Type != "postgres" {
108-
return &SqlDB{}, fmt.Errorf("Unknown type %s", cfg.Type)
110+
connStr, err := ConnectionString(cfg)
111+
if err != nil {
112+
return nil, err
109113
}
110114

111-
connStr, err := ConnectionString(cfg)
115+
var dialect gorm.Dialector
116+
switch cfg.Type {
117+
case "postgres":
118+
dialect = postgres.Open(connStr)
119+
case "mysql":
120+
dialect = mysql.Open(connStr)
121+
default:
122+
return &SqlDB{}, errors.New(fmt.Sprintf("Unknown type %s", cfg.Type))
123+
}
124+
125+
db, err := gorm.Open(dialect, &gorm.Config{})
112126
if err != nil {
113127
return nil, err
114128
}
115129

116-
db, err := gorm.Open(cfg.Type, connStr)
130+
// Use the connection pool and setup it
131+
sqlDB, err := db.DB()
117132
if err != nil {
118133
return nil, err
119134
}
120135

121-
db.DB().SetMaxIdleConns(cfg.MaxIdleConns)
122-
db.DB().SetMaxOpenConns(cfg.MaxOpenConns)
136+
sqlDB.SetMaxIdleConns(cfg.MaxIdleConns)
137+
sqlDB.SetMaxOpenConns(cfg.MaxOpenConns)
123138
connMaxLifetime := time.Duration(cfg.ConnMaxLifetime) * time.Second
124-
db.DB().SetConnMaxLifetime(connMaxLifetime)
139+
sqlDB.SetConnMaxLifetime(connMaxLifetime)
125140

126141
tcpEventHub := eventhub.NewNonBlocking(1024)
127142
httpEventHub := eventhub.NewNonBlocking(1024)

db/db_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55

66
"code.cloudfoundry.org/routing-api/cmd/routing-api/testrunner"
77
"code.cloudfoundry.org/routing-api/config"
8-
_ "github.com/lib/pq"
8+
_ "github.com/jackc/pgx/v5/stdlib"
99
. "github.com/onsi/ginkgo/v2"
1010
. "github.com/onsi/gomega"
1111
)

migration/migration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55

66
"code.cloudfoundry.org/lager/v3"
77
"code.cloudfoundry.org/routing-api/db"
8-
"github.com/jinzhu/gorm"
8+
"gorm.io/gorm"
99
)
1010

1111
const MigrationKey = "routing-api-migration"

0 commit comments

Comments
 (0)