Skip to content

Managed database name generation #7741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
review
  • Loading branch information
grahamplata committed Aug 7, 2025
commit dca1f7b0682f3692be7e29d06fdf50819fef8b58
51 changes: 17 additions & 34 deletions admin/provisioner/clickhousestatic/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"net/url"
"os"
"regexp"
"strings"

"github.com/ClickHouse/clickhouse-go/v2"
Expand All @@ -17,6 +18,8 @@ import (
"go.uber.org/zap"
)

var nonAlphanumericRegexp = regexp.MustCompile(`[^a-zA-Z0-9]+`)

func init() {
provisioner.Register("clickhouse-static", New)
}
Expand Down Expand Up @@ -130,16 +133,8 @@ func (p *Provisioner) Provision(ctx context.Context, r *provisioner.Resource, op
}

// Prepare for creating the schema and user.
id := strings.ReplaceAll(r.ID, "-", "")
user := fmt.Sprintf("rill_%s", id)
dbName := fmt.Sprintf("rill_%s", id)

// Use org and project names to create a more human-readable database name.
orgName := sanitizeName(getAnnotationValue(opts.Annotations, "organization_name"))
projectName := sanitizeName(getAnnotationValue(opts.Annotations, "project_name"))
if orgName != "" && projectName != "" {
dbName = fmt.Sprintf("rill_%s_%s_%s", orgName, projectName, id)
}
user := fmt.Sprintf("rill_%s", nonAlphanumericRegexp.ReplaceAllString(r.ID, ""))
dbName := generateDatabaseName(r.ID, opts.Annotations)

password := newPassword()
annotationsJSON, err := json.Marshal(opts.Annotations)
Expand Down Expand Up @@ -329,31 +324,19 @@ func newPassword() string {
return fmt.Sprintf("1Rr!%x", b[:])
}

// Helper function to get annotation value
func getAnnotationValue(annotations map[string]string, key string) string {
if annotations == nil {
return ""
func generateDatabaseName(resourceID string, annotations map[string]string) string {
name := "rill"
if org, ok := annotations["organization_name"]; ok {
name += "_" + nonAlphanumericRegexp.ReplaceAllString(org, "")
}
return annotations[key]
}

// Helper function to sanitize names for ClickHouse identifiers
func sanitizeName(name string) string {
if name == "" {
return ""
if proj, ok := annotations["project_name"]; ok {
name += "_" + nonAlphanumericRegexp.ReplaceAllString(proj, "")
}

// Replace invalid characters with underscores
name = strings.ReplaceAll(name, "-", "_")
name = strings.ReplaceAll(name, " ", "_")

// Remove any characters that aren't alphanumeric or underscore
var result strings.Builder
for _, r := range name {
if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '_' {
result.WriteRune(r)
}
name += "_" + nonAlphanumericRegexp.ReplaceAllString(resourceID, "")
// Optionally, trim to 63 chars and remove trailing underscores if needed
if len(name) > 63 {
name = name[:63]
}

return strings.ToLower(result.String())
name = strings.TrimRight(name, "_")
return name
}
42 changes: 23 additions & 19 deletions admin/provisioner/clickhousestatic/provisioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,13 @@ func TestClickHouseStaticHumanReadableNaming(t *testing.T) {
opts2, err := clickhouse.ParseDSN(cfg.DSN)
require.NoError(t, err)
// Check that the database name follows the expected format
expectedID := sanitizeName(resourceID)
expectedDBName := fmt.Sprintf("rill_acme_corp_my_project_%s", expectedID)
expectedUser := fmt.Sprintf("rill_%s", expectedID) // User name uses sanitized format
expectedDBName := fmt.Sprintf(
"rill_%s_%s_%s",
nonAlphanumericRegexp.ReplaceAllString(opts.Annotations["organization_name"], ""),
nonAlphanumericRegexp.ReplaceAllString(opts.Annotations["project_name"], ""),
nonAlphanumericRegexp.ReplaceAllString(resourceID, ""),
)
expectedUser := fmt.Sprintf("rill_%s", nonAlphanumericRegexp.ReplaceAllString(resourceID, ""))

require.Equal(t, expectedDBName, opts2.Auth.Database)
require.Equal(t, expectedUser, opts2.Auth.Username)
Expand Down Expand Up @@ -377,9 +381,8 @@ func TestClickHouseStaticFallbackNaming(t *testing.T) {
opts2, err := clickhouse.ParseDSN(cfg.DSN)
require.NoError(t, err)
// Check that the database name follows the fallback format
expectedID := sanitizeName(resourceID)
expectedDBName := fmt.Sprintf("rill_%s", expectedID)
expectedUser := fmt.Sprintf("rill_%s", expectedID) // User name uses sanitized format
expectedDBName := fmt.Sprintf("rill_%s", nonAlphanumericRegexp.ReplaceAllString(resourceID, ""))
expectedUser := fmt.Sprintf("rill_%s", nonAlphanumericRegexp.ReplaceAllString(resourceID, ""))

require.Equal(t, expectedDBName, opts2.Auth.Database)
require.Equal(t, expectedUser, opts2.Auth.Username)
Expand Down Expand Up @@ -408,21 +411,22 @@ func TestSanitizeName(t *testing.T) {
}{
{"", ""},
{"simple", "simple"},
{"Simple", "simple"},
{"UPPERCASE", "uppercase"},
{"with-dashes", "with_dashes"},
{"with spaces", "with_spaces"},
{"Simple", "Simple"},
{"UPPERCASE", "UPPERCASE"},
{"with-dashes", "withdashes"},
{"with spaces", "withspaces"},
{"with@special!chars", "withspecialchars"},
{"mixed-Case_Name", "mixed_case_name"},
{"mixed-Case_Name", "mixedCaseName"},
{"123numbers", "123numbers"},
{"_underscore_", "_underscore_"},
{"Acme-Corp", "acme_corp"},
{"My-Project", "my_project"},
{"_underscore_", "underscore"},
{"Acme-Corp", "AcmeCorp"},
{"My-Project", "MyProject"},
{"name_with_special_characters_1234567890!@#$%^&*()_+{}|:\"<>?[];',./`~", "namewithspecialcharacters1234567890"},
}

for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := sanitizeName(tt.input)
result := nonAlphanumericRegexp.ReplaceAllString(tt.input, "")
require.Equal(t, tt.expected, result)
})
}
Expand All @@ -439,19 +443,19 @@ func TestGenerateDatabaseName(t *testing.T) {
name: "with org and project",
id: "77cf2b72_65ab_4bbe_a10e_627bcff4915e",
annotations: map[string]string{"organization_name": "rilldata", "project_name": "dev-project-1"},
expected: "rill_rilldata_dev_project_1_77cf2b72_65ab_4bbe_a10e_627bcff4915",
expected: "rill_rilldata_devproject1_77cf2b7265ab4bbea10e627bcff4915e",
},
{
name: "with org only",
id: "12345",
annotations: map[string]string{"organization_name": "acme-corp"},
expected: "rill_acme_corp_12345",
expected: "rill_acmecorp_12345",
},
{
name: "with project only",
id: "12345",
annotations: map[string]string{"project_name": "my-project"},
expected: "rill_my_project_12345",
expected: "rill_myproject_12345",
},
{
name: "no annotations",
Expand All @@ -469,7 +473,7 @@ func TestGenerateDatabaseName(t *testing.T) {
name: "long name truncated",
id: "very_long_resource_id_that_will_cause_truncation_12345678",
annotations: map[string]string{"organization_name": "very_long_organization_name", "project_name": "very_long_project_name"},
expected: "rill_very_long_organization_name_very_long_project_name_very_lo",
expected: "rill_verylongorganizationname_verylongprojectname_verylongresou",
},
}

Expand Down
Loading