Skip to content

Commit c2626b7

Browse files
committed
feat: Handle the Gorm migration for PostgreSQL
1 parent f6742e3 commit c2626b7

23 files changed

+109
-82
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
go.mod
2+
go.sum
23
tags
34
cmd/routing-api/routing-api
45
/routing-api

cmd/routing-api/routing_api_suite_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ var _ = SynchronizedBeforeSuite(
7070
Expect(err).NotTo(HaveOccurred())
7171

7272
locketPath, err := gexec.Build("code.cloudfoundry.org/locket/cmd/locket", "-race")
73-
if err != nil {
74-
// If building locket fails due to missing dependencies, skip the test
75-
Skip(fmt.Sprintf("Skipping test suite: locket dependency issue - %v", err))
76-
}
73+
Expect(err).NotTo(HaveOccurred())
7774

7875
return []byte(strings.Join([]string{routingAPIBin, locketPath}, ","))
7976
},
@@ -82,9 +79,6 @@ var _ = SynchronizedBeforeSuite(
8279

8380
path := string(binPaths)
8481
parts := strings.Split(path, ",")
85-
if len(parts) < 2 || parts[1] == "" {
86-
Skip("Skipping test suite: locket binary not available")
87-
}
8882
routingAPIBinPath = parts[0]
8983
locketBinPath = parts[1]
9084

cmd/routing-api/testrunner/constants.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package testrunner
22

33
import (
4-
"code.cloudfoundry.org/routing-api/config"
54
"os"
5+
6+
"code.cloudfoundry.org/routing-api/config"
67
)
78

89
const (

cmd/routing-api/testrunner/db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (a *postgresAllocator) Create() (*config.SqlDB, error) {
6363
if err != nil {
6464
return nil, err
6565
}
66-
a.sqlDB, err = sql.Open("postgres", connStr)
66+
a.sqlDB, err = sql.Open("pgx", connStr)
6767
if err != nil {
6868
return nil, err
6969
}

db/db_sql.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ func NewSqlDB(cfg *config.SqlDB) (*SqlDB, error) {
128128
return nil, err
129129
}
130130

131-
// Use the connection pool and setup it
132131
sqlDB, err := db.DB()
133132
if err != nil {
134133
return nil, err

db/db_sql_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,13 +2085,10 @@ var _ = Describe("SqlDB", func() {
20852085
It("eventually resolves the issue", func() {
20862086
timeout := 10.0
20872087

2088-
// Verify successful pruning starts working
20892088
Eventually(logger, timeout).Should(gbytes.Say(`"prune.successfully-finished-pruning-tcp-routes","log_level":1,"data":{"rowsAffected":1}`))
20902089

2091-
// Verify errors occur
20922090
Eventually(logger, timeout).Should(gbytes.Say(`failed-to-prune-tcp-routes","log_level":2,"data":{"error":"temp-error"}`))
20932091

2094-
// Verify recovery with more rows
20952092
Eventually(logger, timeout).Should(gbytes.Say(`"prune.successfully-finished-pruning-tcp-routes","log_level":1,"data":{"rowsAffected":111}`))
20962093
})
20972094
})

db/db_suite_test.go

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

66
"code.cloudfoundry.org/routing-api/cmd/routing-api/testrunner"
77
"code.cloudfoundry.org/routing-api/config"
8-
_ "github.com/jackc/pgx/v5/stdlib"
98
. "github.com/onsi/ginkgo/v2"
109
. "github.com/onsi/gomega"
1110
)

db/fakes/fake_client.go

Lines changed: 0 additions & 2 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: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ func (v *V2UpdateRgMigration) Run(sqlDB *db.SqlDB) error {
2727
return err
2828
}
2929

30-
// Check for duplicates
3130
nameMap := make(map[string]int)
3231
for _, rg := range rgs {
3332
nameMap[rg.Name]++
@@ -39,14 +38,14 @@ func (v *V2UpdateRgMigration) Run(sqlDB *db.SqlDB) error {
3938
}
4039
}
4140

42-
// Remove old index if it exists - using correct MySQL syntax with table name
43-
_ = sqlDB.Client.ExecWithError("DROP INDEX idx_rg_name ON router_groups")
41+
dropIndex(sqlDB, "idx_rg_name", "router_groups")
4442

45-
// Create unique index using raw SQL with column length specification
46-
err = sqlDB.Client.ExecWithError("CREATE UNIQUE INDEX idx_rg_name ON router_groups (name(191))")
47-
if err != nil {
48-
return err
43+
// Create unique index - MySQL requires length prefix for text columns
44+
var indexSQL string
45+
if sqlDB.Client.Dialect().Name() == "mysql" {
46+
indexSQL = "CREATE UNIQUE INDEX idx_rg_name ON router_groups (name(191))"
47+
} else {
48+
indexSQL = "CREATE UNIQUE INDEX idx_rg_name ON router_groups (name)"
4949
}
50-
51-
return nil
50+
return sqlDB.Client.ExecWithError(indexSQL)
5251
}

migration/V2_update_rg_migration_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ var _ = Describe("V2UpdateRgMigration", func() {
5555
})
5656

5757
It("does not allow duplicate router group names", func() {
58-
// Verify that the unique index was created
5958
migrator := sqlDB.Client.Migrator()
6059
hasIndex := migrator.HasIndex("router_groups", "idx_rg_name")
6160
Expect(hasIndex).To(BeTrue(), "Index idx_rg_name should exist on router_groups table")

0 commit comments

Comments
 (0)