Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: handle routing id for cockroachdb (#13723)

h3n4l81ecf2c6d750
diff --git a/backend/plugin/db/cockroachdb/cockroachdb.go b/backend/plugin/db/cockroachdb/cockroachdb.go
index f4504e87bd..0052cec396 100644
--- a/backend/plugin/db/cockroachdb/cockroachdb.go
+++ b/backend/plugin/db/cockroachdb/cockroachdb.go
@@ -76,7 +76,7 @@ func (driver *Driver) Open(ctx context.Context, _ storepb.Engine, config db.Conn
case storepb.DataSourceOptions_AWS_RDS_IAM:
pgxConnConfig, err = getRDSConnectionConfig(ctx, config)
default:
- pgxConnConfig, err = getPGConnectionConfig(config)
+ pgxConnConfig, err = getCockroachConnectionConfig(config)
}
if err != nil {
return nil, err
@@ -128,7 +128,21 @@ func (driver *Driver) Open(ctx context.Context, _ storepb.Engine, config db.Conn
return driver, nil
}
-func getPGConnectionConfig(config db.ConnectionConfig) (*pgx.ConnConfig, error) {
+// getRoutingIDFromCockroachCloudURL returns the routing ID from the Cockroach Cloud URL, returns empty string if not found.
+func getRoutingIDFromCockroachCloudURL(host string) string {
+ host = strings.TrimSpace(host)
+ if !strings.HasSuffix(host, "cockroachlabs.cloud") {
+ return ""
+ }
+ parts := strings.Split(host, ".")
+ // routing-id[.xxx].cockroachlabs.cloud
+ if len(parts) > 2 {
+ return parts[0]
+ }
+ return ""
+}
+
+func getCockroachConnectionConfig(config db.ConnectionConfig) (*pgx.ConnConfig, error) {
// Require username for Postgres, as the guessDSN 1st guess is to use the username as the connecting database
// if database name is not explicitly specified.
if config.Username == "" {
@@ -152,6 +166,11 @@ func getPGConnectionConfig(config db.ConnectionConfig) (*pgx.ConnConfig, error)
sslMode := getSSLMode(config.TLSConfig, config.SSHConfig)
connStr += fmt.Sprintf(" sslmode=%s", sslMode)
+ routingID := getRoutingIDFromCockroachCloudURL(config.Host)
+ if routingID != "" {
+ connStr += fmt.Sprintf(" options='--cluster=%s'", routingID)
+ }
+
connConfig, err := pgx.ParseConfig(connStr)
if err != nil {
return nil, err
diff --git a/backend/plugin/db/cockroachdb/cockroachdb_test.go b/backend/plugin/db/cockroachdb/cockroachdb_test.go
index 89cdcd791f..425dad857c 100644
--- a/backend/plugin/db/cockroachdb/cockroachdb_test.go
+++ b/backend/plugin/db/cockroachdb/cockroachdb_test.go
@@ -49,3 +49,40 @@ func TestGetDatabaseInCreateDatabaseStatement(t *testing.T) {
require.Equal(t, test.want, got)
}
}
+
+func TestGetRoutingIDFromCockroachCloudURL(t *testing.T) {
+ tests := []struct {
+ host string
+ expected string
+ }{
+ {
+ host: "routing-id.cockroachlabs.cloud",
+ expected: "routing-id",
+ },
+ {
+ host: "bytebase-cluster-7749.6xw.aws-ap-southeast-1.cockroachlabs.cloud",
+ expected: "bytebase-cluster-7749",
+ },
+ {
+ host: "subdomain.routing-id.cockroachlabs.cloud",
+ expected: "subdomain",
+ },
+ {
+ host: "subdomain.routing-id.cockroachlabs.cloud",
+ expected: "subdomain",
+ },
+ {
+ host: "cockroachlabs.cloud",
+ expected: "",
+ },
+ {
+ host: "example.com",
+ expected: "",
+ },
+ }
+
+ for _, test := range tests {
+ got := getRoutingIDFromCockroachCloudURL(test.host)
+ require.Equal(t, test.expected, got, "host: %s", test.host)
+ }
+}