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
use string builder
  • Loading branch information
grahamplata committed Aug 6, 2025
commit 84cd3f1e14fa303f6fef513490c57dad6a257a4e
38 changes: 21 additions & 17 deletions admin/provisioner/clickhousestatic/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,26 +330,30 @@ func generateDatabaseName(resourceID string, annotations map[string]string) stri
orgName := sanitizeName(getAnnotationValue(annotations, "organization_name"))
projectName := sanitizeName(getAnnotationValue(annotations, "project_name"))

// Limit component lengths to avoid overly long database names
if len(orgName) > 20 {
orgName = orgName[:20]
}
if len(projectName) > 20 {
projectName = projectName[:20]
var builder strings.Builder
builder.WriteString("rill")

// Build database name based on available components for human readability
if orgName != "" {
builder.WriteString("_")
builder.WriteString(orgName)
}
if len(resourceID) > 20 {
resourceID = resourceID[:20]
if projectName != "" {
builder.WriteString("_")
builder.WriteString(projectName)
}
builder.WriteString("_")
builder.WriteString(resourceID)

// Build database name based on available components for human readability
if orgName != "" && projectName != "" {
return fmt.Sprintf("rill_%s_%s_%s", orgName, projectName, resourceID)
} else if orgName != "" {
return fmt.Sprintf("rill_%s_%s", orgName, resourceID)
} else if projectName != "" {
return fmt.Sprintf("rill_%s_%s", projectName, resourceID)
dbName := builder.String()

// Ensure the total length doesn't exceed 63 characters
if len(dbName) > 63 {
dbName = dbName[:63]
}

// Fallback to simple naming
return fmt.Sprintf("rill_%s", resourceID)
// Remove any trailing underscores
dbName = strings.TrimRight(dbName, "_")

return dbName
}
44 changes: 39 additions & 5 deletions admin/provisioner/clickhousestatic/provisioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,20 +430,54 @@ func TestSanitizeName(t *testing.T) {

func TestGenerateDatabaseName(t *testing.T) {
tests := []struct {
name string
id string
annotations map[string]string
expected string
}{
{"12345", nil, "rill_12345"},
{"12345", map[string]string{"organization_name": "Acme-Corp"}, "rill_acme_corp_12345"},
{"12345", map[string]string{"project_name": "My-Project"}, "rill_my_project_12345"},
{"12345", map[string]string{"organization_name": "Acme-Corp", "project_name": "My-Project"}, "rill_acme_corp_my_project_12345"},
{
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",
},
{
name: "with org only",
id: "12345",
annotations: map[string]string{"organization_name": "acme-corp"},
expected: "rill_acme_corp_12345",
},
{
name: "with project only",
id: "12345",
annotations: map[string]string{"project_name": "my-project"},
expected: "rill_my_project_12345",
},
{
name: "no annotations",
id: "12345",
annotations: map[string]string{},
expected: "rill_12345",
},
{
name: "nil annotations",
id: "12345",
annotations: nil,
expected: "rill_12345",
},
{
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",
},
}

for _, tt := range tests {
t.Run(tt.id, func(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
result := generateDatabaseName(tt.id, tt.annotations)
require.Equal(t, tt.expected, result)
require.LessOrEqual(t, len(result), 63, "database name should not exceed 63 characters")
})
}
}
Loading