Skip to content

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

fix: use UTF-8 safe TruncateString for string truncation (#19102)

p0ny37408637e42b
diff --git a/backend/api/mcp/tool_search.go b/backend/api/mcp/tool_search.go
index fb451e9eb5..b4a9152791 100644
--- a/backend/api/mcp/tool_search.go
+++ b/backend/api/mcp/tool_search.go
@@ -6,6 +6,8 @@ import (
"strings"
"github.com/modelcontextprotocol/go-sdk/mcp"
+
+ "github.com/bytebase/bytebase/backend/common"
)
// SearchInput is the input for the search_api tool.
@@ -235,8 +237,8 @@ func (*Server) formatProperty(sb *strings.Builder, prop PropertyInfo) {
cleanDesc := strings.ReplaceAll(prop.Description, "\n", " ")
cleanDesc = strings.ReplaceAll(cleanDesc, "\r", "")
// Truncate at 100 chars
- if len(cleanDesc) > 100 {
- cleanDesc = cleanDesc[:97] + "..."
+ if truncated, ok := common.TruncateString(cleanDesc, 97); ok {
+ cleanDesc = truncated + "..."
}
desc = fmt.Sprintf(" // %s", cleanDesc)
}
diff --git a/backend/migrator/migrator.go b/backend/migrator/migrator.go
index f2efa94c7a..ad0ca4d04a 100644
--- a/backend/migrator/migrator.go
+++ b/backend/migrator/migrator.go
@@ -16,6 +16,7 @@ import (
"github.com/lib/pq"
"github.com/pkg/errors"
+ "github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/store"
)
@@ -209,9 +210,9 @@ func executeMigration(ctx context.Context, conn *sql.Conn, statement string, ver
}
// Truncate statement for readability in error message
- stmtPreview := statement
- if len(stmtPreview) > 100 {
- stmtPreview = stmtPreview[:100] + "..."
+ stmtPreview, truncated := common.TruncateString(statement, 100)
+ if truncated {
+ stmtPreview += "..."
}
return errors.Errorf("migration %s failed\n"+
diff --git a/backend/plugin/db/elasticsearch/sync.go b/backend/plugin/db/elasticsearch/sync.go
index ddcd83cc33..110d2099e0 100644
--- a/backend/plugin/db/elasticsearch/sync.go
+++ b/backend/plugin/db/elasticsearch/sync.go
@@ -13,6 +13,7 @@ import (
"github.com/opensearch-project/opensearch-go/v4/opensearchapi"
"github.com/pkg/errors"
+ "github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/db"
)
@@ -96,9 +97,9 @@ func (d *Driver) getVersion() (string, error) {
err = json.Unmarshal(bytes, &result)
if err != nil {
// Include response body to help debug parsing issues
- bodyPreview := string(bytes)
- if len(bodyPreview) > 500 {
- bodyPreview = bodyPreview[:500] + "..."
+ bodyPreview, truncated := common.TruncateString(string(bytes), 500)
+ if truncated {
+ bodyPreview += "..."
}
return "", errors.Wrapf(err, "failed to parse version response: %s", bodyPreview)
}
@@ -158,9 +159,9 @@ func (d *Driver) getIndices() ([]*storepb.TableMetadata, error) {
var results []IndicesResult
if err := json.Unmarshal(bytes, &results); err != nil {
// Include response body to help debug parsing issues
- bodyPreview := string(bytes)
- if len(bodyPreview) > 500 {
- bodyPreview = bodyPreview[:500] + "..."
+ bodyPreview, truncated := common.TruncateString(string(bytes), 500)
+ if truncated {
+ bodyPreview += "..."
}
return nil, errors.Wrapf(err, "failed to parse Elasticsearch indices response: %s", bodyPreview)
}
@@ -204,9 +205,9 @@ func (d *Driver) getIndices() ([]*storepb.TableMetadata, error) {
var results []IndicesResult
if err := json.Unmarshal(bytes, &results); err != nil {
// Include response body to help debug parsing issues
- bodyPreview := string(bytes)
- if len(bodyPreview) > 500 {
- bodyPreview = bodyPreview[:500] + "..."
+ bodyPreview, truncated := common.TruncateString(string(bytes), 500)
+ if truncated {
+ bodyPreview += "..."
}
return nil, errors.Wrapf(err, "failed to parse indices response: %s", bodyPreview)
}