Skip to content

Commit

Permalink
roachprod: use csv when upserting virtual cluster metadata
Browse files Browse the repository at this point in the history
Previously, we would run a query with `roachprod sql --format
json`. However, that makes this logic impossible to backport in the
23.1 release branch, since JSON support is not available there.

In this commit, we switch the format to CSV, making this behaviour
more widely supported across branches.

Epic: none

Release note: None
  • Loading branch information
renatolabs authored and DarrylWong committed Jan 16, 2024
1 parent e87392b commit db07e0d
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions pkg/roachprod/install/cockroach.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ package install
import (
"context"
_ "embed" // required for go:embed
"encoding/json"
"encoding/csv"
"fmt"
"net/url"
"os"
Expand Down Expand Up @@ -1255,7 +1255,7 @@ func (c *SyncedCluster) upsertVirtualClusterMetadata(
) (int, error) {
runSQL := func(stmt string) (string, error) {
results, err := startOpts.KVCluster.ExecSQL(ctx, l, startOpts.KVCluster.Nodes[:1], "", 0, []string{
"--format", "json", "-e", stmt,
"--format", "csv", "-e", stmt,
})
if err != nil {
return "", err
Expand All @@ -1268,10 +1268,6 @@ func (c *SyncedCluster) upsertVirtualClusterMetadata(
}

virtualClusterIDByName := func(name string) (int, error) {
type tenantRow struct {
ID string `json:"id"`
}

query := fmt.Sprintf(
"SELECT id FROM system.tenants WHERE name = '%s'", startOpts.VirtualClusterName,
)
Expand All @@ -1281,16 +1277,17 @@ func (c *SyncedCluster) upsertVirtualClusterMetadata(
return -1, err
}

var tenants []tenantRow
if err := json.Unmarshal([]byte(existsOut), &tenants); err != nil {
return -1, fmt.Errorf("failed to unmarshal system.tenants output: %w\n%s", err, existsOut)
rows, err := csv.NewReader(strings.NewReader(existsOut)).ReadAll()
if err != nil {
return -1, fmt.Errorf("failed to parse system.tenants output: %w\n%s", err, existsOut)
}

if len(tenants) == 0 {
records := rows[1:] // skip header
if len(records) == 0 {
return -1, nil
}

n, err := strconv.Atoi(tenants[0].ID)
n, err := strconv.Atoi(records[0][0])
if err != nil {
return -1, fmt.Errorf("failed to parse virtual cluster ID: %w", err)
}
Expand Down

0 comments on commit db07e0d

Please sign in to comment.