Skip to content

Commit ed1d830

Browse files
committed
feat: Handle the Gorm migration for PostgreSQL
1 parent 7df3ecc commit ed1d830

15 files changed

Lines changed: 112 additions & 65 deletions

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_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
)

migration/V2_update_rg_migration.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ func (v *V2UpdateRgMigration) Run(sqlDB *db.SqlDB) error {
3939
}
4040
}
4141

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")
44-
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
42+
// Remove old index if it exists
43+
dropIndex(sqlDB, "idx_rg_name", "router_groups")
44+
45+
// Create unique index - MySQL requires length prefix for text columns
46+
var indexSQL string
47+
if sqlDB.Client.Dialect().Name() == "mysql" {
48+
indexSQL = "CREATE UNIQUE INDEX idx_rg_name ON router_groups (name(191))"
49+
} else {
50+
indexSQL = "CREATE UNIQUE INDEX idx_rg_name ON router_groups (name)"
4951
}
50-
51-
return nil
52+
return sqlDB.Client.ExecWithError(indexSQL)
5253
}

migration/V4_add_rg_uniq_idx_tcp_route_migration.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@ func (v *V4AddRgUniqIdxTCPRoute) Version() int {
1717
}
1818

1919
func (v *V4AddRgUniqIdxTCPRoute) Run(sqlDB *db.SqlDB) error {
20-
// Drop existing index if it exists - using correct MySQL syntax with table name
21-
sqlDB.Client.Exec("DROP INDEX IF EXISTS idx_tcp_route ON tcp_routes")
22-
23-
// Create unique index using raw SQL
24-
sqlDB.Client.Exec("CREATE UNIQUE INDEX idx_tcp_route ON tcp_routes (router_group_guid, host_port, host_ip, external_port)")
20+
// Drop existing index if it exists
21+
dropIndex(sqlDB, "idx_tcp_route", "tcp_routes")
22+
23+
// Create unique index - MySQL requires length prefixes for text columns
24+
var indexSQL string
25+
if sqlDB.Client.Dialect().Name() == "mysql" {
26+
indexSQL = "CREATE UNIQUE INDEX idx_tcp_route ON tcp_routes (router_group_guid(191), host_port, host_ip(191), external_port)"
27+
} else {
28+
indexSQL = "CREATE UNIQUE INDEX idx_tcp_route ON tcp_routes (router_group_guid, host_port, host_ip, external_port)"
29+
}
30+
sqlDB.Client.Exec(indexSQL)
2531

2632
return nil
2733
}

migration/V5_sni_hostname_migration.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ func (v *V5SniHostnameMigration) Run(sqlDB *db.SqlDB) error {
2424
}
2525

2626
// Drop old index if it exists (ignore errors since it might not exist)
27-
_ = sqlDB.Client.ExecWithError("DROP INDEX idx_tcp_route ON tcp_routes")
28-
29-
// Create unique index for SNI hostname constraint with column length specifications
30-
// This allows routes with same fingerprint but different SNI hostnames
31-
err = sqlDB.Client.ExecWithError("CREATE UNIQUE INDEX idx_tcp_route ON tcp_routes (router_group_guid(191), host_port, host_ip(191), external_port, sni_hostname(191))")
32-
if err != nil {
33-
return err
27+
dropIndex(sqlDB, "idx_tcp_route", "tcp_routes")
28+
29+
// Create unique index - MySQL requires length prefixes for text columns
30+
var indexSQL string
31+
if sqlDB.Client.Dialect().Name() == "mysql" {
32+
indexSQL = "CREATE UNIQUE INDEX idx_tcp_route ON tcp_routes (router_group_guid(191), host_port, host_ip(191), external_port, sni_hostname(191))"
33+
} else {
34+
indexSQL = "CREATE UNIQUE INDEX idx_tcp_route ON tcp_routes (router_group_guid, host_port, host_ip, external_port, sni_hostname)"
3435
}
35-
36-
return nil
36+
return sqlDB.Client.ExecWithError(indexSQL)
3737
}

migration/V6_tls_tcp_route.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ func (v *V6TCPTLSRoutes) Run(sqlDB *db.SqlDB) error {
2323
return err
2424
}
2525

26-
// Drop existing index if it exists - using correct MySQL syntax with table name
27-
_ = sqlDB.Client.ExecWithError("DROP INDEX idx_tcp_route ON tcp_routes")
28-
29-
// Create unique index using raw SQL with column length specifications
30-
err = sqlDB.Client.ExecWithError("CREATE UNIQUE INDEX idx_tcp_route ON tcp_routes (router_group_guid(191), host_port, host_ip(191), external_port, sni_hostname(191), host_tls_port)")
31-
if err != nil {
32-
return err
26+
// Drop existing index if it exists
27+
dropIndex(sqlDB, "idx_tcp_route", "tcp_routes")
28+
29+
// Create unique index - MySQL requires length prefixes for text columns
30+
var indexSQL string
31+
if sqlDB.Client.Dialect().Name() == "mysql" {
32+
indexSQL = "CREATE UNIQUE INDEX idx_tcp_route ON tcp_routes (router_group_guid(191), host_port, host_ip(191), external_port, sni_hostname(191), host_tls_port)"
33+
} else {
34+
indexSQL = "CREATE UNIQUE INDEX idx_tcp_route ON tcp_routes (router_group_guid, host_port, host_ip, external_port, sni_hostname, host_tls_port)"
3335
}
34-
35-
return nil
36+
return sqlDB.Client.ExecWithError(indexSQL)
3637
}

migration/V7_instance_id_defaults.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,28 @@ func (v *V7TCPTLSRoutes) Version() int {
1515
}
1616

1717
func (v *V7TCPTLSRoutes) Run(sqlDB *db.SqlDB) error {
18-
// Update the instance_id column to allow NULL values
19-
err := sqlDB.Client.ExecWithError("ALTER TABLE tcp_routes MODIFY COLUMN instance_id varchar(255) DEFAULT NULL")
20-
if err != nil {
21-
return err
18+
// Update the instance_id column to allow NULL values - syntax differs by database
19+
if sqlDB.Client.Dialect().Name() == "mysql" {
20+
err := sqlDB.Client.ExecWithError("ALTER TABLE tcp_routes MODIFY COLUMN instance_id varchar(255) DEFAULT NULL")
21+
if err != nil {
22+
return err
23+
}
24+
} else {
25+
err := sqlDB.Client.ExecWithError("ALTER TABLE tcp_routes ALTER COLUMN instance_id SET DEFAULT NULL")
26+
if err != nil {
27+
return err
28+
}
2229
}
2330

24-
// Drop existing index if it exists - using correct MySQL syntax with table name
25-
_ = sqlDB.Client.ExecWithError("DROP INDEX idx_tcp_route ON tcp_routes")
31+
// Drop existing index if it exists
32+
dropIndex(sqlDB, "idx_tcp_route", "tcp_routes")
2633

27-
// Create unique index using raw SQL with column length specifications
28-
err = sqlDB.Client.ExecWithError("CREATE UNIQUE INDEX idx_tcp_route ON tcp_routes (router_group_guid(191), host_port, host_ip(191), external_port, sni_hostname(191), host_tls_port)")
29-
if err != nil {
30-
return err
34+
// Create unique index - MySQL requires length prefixes for text columns
35+
var indexSQL string
36+
if sqlDB.Client.Dialect().Name() == "mysql" {
37+
indexSQL = "CREATE UNIQUE INDEX idx_tcp_route ON tcp_routes (router_group_guid(191), host_port, host_ip(191), external_port, sni_hostname(191), host_tls_port)"
38+
} else {
39+
indexSQL = "CREATE UNIQUE INDEX idx_tcp_route ON tcp_routes (router_group_guid, host_port, host_ip, external_port, sni_hostname, host_tls_port)"
3140
}
32-
33-
return nil
41+
return sqlDB.Client.ExecWithError(indexSQL)
3442
}

migration/V8_host_tls_port_tcp_default_zero.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ func (v *V8HostTLSPortTCPDefaultZero) Run(sqlDB *db.SqlDB) error {
2121
return err
2222
}
2323

24-
// Try to remove the old index if it exists - using correct MySQL syntax with table name
25-
_ = sqlDB.Client.ExecWithError("DROP INDEX IF EXISTS idx_tcp_route ON tcp_routes")
26-
27-
// Set the DEFAULT 0 on the host_tls_port column
28-
err = sqlDB.Client.ExecWithError("ALTER TABLE tcp_routes MODIFY COLUMN host_tls_port int DEFAULT 0")
29-
if err != nil {
30-
return err
24+
// Try to remove the old index if it exists
25+
dropIndex(sqlDB, "idx_tcp_route", "tcp_routes")
26+
27+
// Set the DEFAULT 0 on the host_tls_port column - syntax differs by database
28+
if sqlDB.Client.Dialect().Name() == "mysql" {
29+
err = sqlDB.Client.ExecWithError("ALTER TABLE tcp_routes MODIFY COLUMN host_tls_port int DEFAULT 0")
30+
} else {
31+
err = sqlDB.Client.ExecWithError("ALTER TABLE tcp_routes ALTER COLUMN host_tls_port SET DEFAULT 0")
3132
}
32-
33-
return nil
33+
return err
3434
}

migration/migration.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
package migration
22

33
import (
4+
"fmt"
45
"os"
56

67
"code.cloudfoundry.org/lager/v3"
78
"code.cloudfoundry.org/routing-api/db"
89
"gorm.io/gorm"
910
)
1011

12+
// dropIndex drops an index by name. MySQL requires "DROP INDEX name ON table",
13+
// PostgreSQL uses "DROP INDEX IF EXISTS name".
14+
func dropIndex(sqlDB *db.SqlDB, indexName, tableName string) {
15+
if sqlDB.Client.Dialect().Name() == "mysql" {
16+
_ = sqlDB.Client.ExecWithError(fmt.Sprintf("DROP INDEX %s ON %s", indexName, tableName))
17+
} else {
18+
_ = sqlDB.Client.ExecWithError(fmt.Sprintf("DROP INDEX IF EXISTS %s", indexName))
19+
}
20+
}
21+
1122
const MigrationKey = "routing-api-migration"
1223

1324
type MigrationData struct {

0 commit comments

Comments
 (0)