Skip to content

Commit 242b314

Browse files
committed
feat: Start the Gorm migration
1 parent 99f0138 commit 242b314

6 files changed

Lines changed: 66 additions & 62 deletions

File tree

cmd/routing-api/main_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package main_test
22

33
import (
44
"fmt"
5+
"gorm.io/driver/mysql"
6+
"gorm.io/driver/postgres"
57
"net/http"
68
"os"
79
"os/exec"
@@ -162,7 +164,8 @@ var _ = Describe("Main", func() {
162164
rapiConfig := getRoutingAPIConfig(defaultConfig)
163165
connectionString, err := db.ConnectionString(&rapiConfig.SqlDB)
164166
Expect(err).NotTo(HaveOccurred())
165-
gormDB, err := gorm.Open(rapiConfig.SqlDB.Type, connectionString)
167+
168+
gormDB, err := gorm.Open(getGormDialect(rapiConfig.SqlDB.Type, connectionString), &gorm.Config{})
166169
Expect(err).NotTo(HaveOccurred())
167170

168171
getRoutes := func() string {
@@ -239,7 +242,7 @@ var _ = Describe("Main", func() {
239242
}
240243
connectionString, err := db.ConnectionString(&rapiConfig.SqlDB)
241244
Expect(err).NotTo(HaveOccurred())
242-
gormDB, err = gorm.Open(rapiConfig.SqlDB.Type, connectionString)
245+
gormDB, err = gorm.Open(getGormDialect(rapiConfig.SqlDB.Type, connectionString), &gorm.Config{})
243246
Expect(err).NotTo(HaveOccurred())
244247
})
245248
AfterEach(func() {
@@ -265,3 +268,16 @@ func RoutingApi(args ...string) *Session {
265268

266269
return session
267270
}
271+
272+
func getGormDialect(databaseType string, connectionString string) gorm.Dialector {
273+
var dialect gorm.Dialector
274+
275+
switch databaseType {
276+
case "postgres":
277+
dialect = postgres.Open(connectionString)
278+
case "mysql":
279+
dialect = mysql.Open(connectionString)
280+
}
281+
282+
return dialect
283+
}

cmd/routing-api/routing_api_suite_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ var (
6767
func TestMain(m *testing.M) {
6868
RegisterFailHandler(Fail)
6969
os.Exit(m.Run())
70+
//TODO Adapt the test execution
7071
RunSpecs(m, "Main Suite")
7172
}
7273

@@ -81,6 +82,7 @@ var _ = SynchronizedBeforeSuite(
8182
return []byte(strings.Join([]string{routingAPIBin, locketPath}, ","))
8283
},
8384
func(binPaths []byte) {
85+
//TODO Adapt the logger
8486
grpclog.SetLoggerV2(grpclog.NewLoggerV2(io.Discard, io.Discard, io.Discard))
8587

8688
path := string(binPaths)

db/client.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ type Client interface {
2121
Rollback() error
2222
Commit() error
2323
HasTable(value interface{}) bool
24-
AddUniqueIndex(indexName string, columns ...string) (Client, error)
25-
RemoveIndex(indexName string) (Client, error)
24+
AddUniqueIndex(indexName string, columns ...string) error
25+
RemoveIndex(indexName string) error
2626
Model(value interface{}) Client
2727
Exec(query string, args ...interface{}) int64
2828
Rows(tableName string) (*sql.Rows, error)
@@ -37,25 +37,21 @@ func NewGormClient(db *gorm.DB) Client {
3737
return &gormClient{db: db}
3838
}
3939
func (c *gormClient) DropColumn(name string) error {
40-
return c.DropColumn(name).Error()
40+
return c.DropColumn(name)
4141
}
4242
func (c *gormClient) Close() error {
43-
return c.db.Close()
43+
return c.Close()
4444
}
45-
func (c *gormClient) AddUniqueIndex(indexName string, columns ...string) (Client, error) {
46-
var newClient gormClient
47-
newClient.db, err := c.AddUniqueIndex(indexName, columns...)
48-
if err != nil {
49-
50-
}
51-
52-
return &newClient, newClient.db.Error
45+
func (c *gormClient) AddUniqueIndex(indexName string, columns ...string) error {
46+
//TODO Add the interface abstraction layer
47+
var value interface{}
48+
return c.db.Migrator().CreateIndex(value, indexName)
5349
}
5450

55-
func (c *gormClient) RemoveIndex(indexName string) (Client, error) {
56-
var newClient gormClient
57-
newClient.db = c.db.RemoveIndex(indexName)
58-
return &newClient, newClient.db.Error
51+
func (c *gormClient) RemoveIndex(indexName string) error {
52+
//TODO Add the interface abstraction layer
53+
var value interface{}
54+
return c.db.Migrator().DropIndex(value, indexName)
5955
}
6056

6157
func (c *gormClient) Model(value interface{}) Client {
@@ -85,6 +81,7 @@ func (c *gormClient) Save(value interface{}) (int64, error) {
8581
}
8682

8783
func (c *gormClient) Update(attrs ...interface{}) (int64, error) {
84+
//TODO Adapt the functionality
8885
newDb := c.db.Update(attrs...)
8986
return newDb.RowsAffected, newDb.Error
9087
}
@@ -98,7 +95,7 @@ func (c *gormClient) Find(out interface{}, where ...interface{}) error {
9895
}
9996

10097
func (c *gormClient) AutoMigrate(values ...interface{}) error {
101-
return c.db.AutoMigrate(values...).Error
98+
return c.db.AutoMigrate(values...)
10299
}
103100

104101
func (c *gormClient) Begin() Client {
@@ -116,7 +113,7 @@ func (c *gormClient) Commit() error {
116113
}
117114

118115
func (c *gormClient) HasTable(value interface{}) bool {
119-
return c.db.HasTable(value)
116+
return c.db.Migrator().HasTable(value)
120117
}
121118

122119
func (c *gormClient) Exec(query string, args ...interface{}) int64 {

db/fakes/fake_client.go

Lines changed: 28 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

migration/V2_update_rg_migration.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,5 @@ func (v *V2UpdateRgMigration) Version() int {
1818
}
1919

2020
func (v *V2UpdateRgMigration) Run(sqlDB *db.SqlDB) error {
21-
_, err := sqlDB.Client.Model(&models.RouterGroup{}).AddUniqueIndex("idx_rg_name", "name")
22-
return err
21+
return sqlDB.Client.Model(&models.RouterGroup{}).AddUniqueIndex("idx_rg_name", "name")
2322
}

migration/V4_add_rg_uniq_idx_tcp_route_migration.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ func (v *V4AddRgUniqIdxTCPRoute) Version() int {
1818
}
1919

2020
func (v *V4AddRgUniqIdxTCPRoute) Run(sqlDB *db.SqlDB) error {
21-
_, err := sqlDB.Client.Model(&models.TcpRouteMapping{}).RemoveIndex("idx_tcp_route")
21+
err := sqlDB.Client.Model(&models.TcpRouteMapping{}).RemoveIndex("idx_tcp_route")
2222
if err != nil {
2323
return err
2424
}
25-
_, err = sqlDB.Client.Model(&models.TcpRouteMapping{}).AddUniqueIndex("idx_tcp_route", "router_group_guid", "host_port", "host_ip", "external_port")
25+
err = sqlDB.Client.Model(&models.TcpRouteMapping{}).AddUniqueIndex("idx_tcp_route", "router_group_guid", "host_port", "host_ip", "external_port")
2626
return err
2727
}

0 commit comments

Comments
 (0)