Skip to content

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

chore: truncate statement for records (#3963)

Danny Xuece47a3ae402
diff --git a/common/util.go b/common/util.go
index 4cf52a117a..e34844f68d 100644
--- a/common/util.go
+++ b/common/util.go
@@ -13,6 +13,9 @@ import (
"time"
)
+// MaxSheetSize is the maximum size of a sheet for displaying.
+const MaxSheetSize = 10240
+
// FindString returns the search index of sorted strings.
func FindString(stringList []string, search string) int {
sort.Strings(stringList)
@@ -117,8 +120,8 @@ func GetBinlogRelativeDir(binlogDir string) string {
return filepath.Join("backup", "instance", instanceID)
}
-// truncateString truncates the string to have a maximum length of `limit` characters.
-func truncateString(str string, limit int) (string, bool) {
+// TruncateString truncates the string to have a maximum length of `limit` characters.
+func TruncateString(str string, limit int) (string, bool) {
chars := 0
// The string may contain unicode characters, so we iterate here.
for i := range str {
@@ -133,7 +136,7 @@ func truncateString(str string, limit int) (string, bool) {
// TruncateStringWithDescription tries to truncate the string and append "... (view details in Bytebase)" if truncated.
func TruncateStringWithDescription(str string) string {
const limit = 450
- if truncatedStr, truncated := truncateString(str, limit); truncated {
+ if truncatedStr, truncated := TruncateString(str, limit); truncated {
return fmt.Sprintf("%s... (view details in Bytebase)", truncatedStr)
}
return str
diff --git a/common/util_test.go b/common/util_test.go
index 2b3eed979b..4f1adc1e75 100644
--- a/common/util_test.go
+++ b/common/util_test.go
@@ -111,7 +111,7 @@ func TestTruncateString(t *testing.T) {
for i := range tests {
test := tests[i]
t.Run(test.name, func(t *testing.T) {
- got, truncated := truncateString(test.str, test.limit)
+ got, truncated := TruncateString(test.str, test.limit)
a.Equal(test.want, got)
a.Equal(test.truncated, truncated)
})
diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.go
index a17d832667..002cf144ea 100644
--- a/plugin/db/util/driverutil.go
+++ b/plugin/db/util/driverutil.go
@@ -284,7 +284,8 @@ func BeginMigration(ctx context.Context, executor MigrationExecutor, m *db.Migra
// MySQL runs DDL in its own transaction, so we can't commit migration history together with DDL in a single transaction.
// Thus we sort of doing a 2-phase commit, where we first write a PENDING migration record, and after migration completes, we then
// update the record to DONE together with the updated schema.
- if insertedID, err = executor.InsertPendingHistory(ctx, tx, largestSequence+1, prevSchema, m, storedVersion, statement); err != nil {
+ statementRecord, _ := common.TruncateString(statement, common.MaxSheetSize)
+ if insertedID, err = executor.InsertPendingHistory(ctx, tx, largestSequence+1, prevSchema, m, storedVersion, statementRecord); err != nil {
return -1, err
}
diff --git a/server/runner/taskrun/executor.go b/server/runner/taskrun/executor.go
index 1118dda40f..2ff8670b98 100644
--- a/server/runner/taskrun/executor.go
+++ b/server/runner/taskrun/executor.go
@@ -137,12 +137,13 @@ func executeMigration(ctx context.Context, store *store.Store, dbFactory *dbfact
}
defer driver.Close(ctx)
+ statementRecord, _ := common.TruncateString(statement, common.MaxSheetSize)
log.Debug("Start migration...",
zap.String("instance", task.Instance.Name),
zap.String("database", databaseName),
zap.String("source", string(mi.Source)),
zap.String("type", string(mi.Type)),
- zap.String("statement", statement),
+ zap.String("statement", statementRecord),
)
setup, err := driver.NeedsSetupMigration(ctx)
diff --git a/store/sheet.go b/store/sheet.go
index 27c17076c7..48d3ac28ae 100644
--- a/store/sheet.go
+++ b/store/sheet.go
@@ -322,7 +322,7 @@ func createSheetImpl(ctx context.Context, tx *Tx, create *api.SheetCreate) (*she
create.Payload = "{}"
}
- query := `
+ query := fmt.Sprintf(`
INSERT INTO sheet (
creator_id,
updater_id,
@@ -336,8 +336,8 @@ func createSheetImpl(ctx context.Context, tx *Tx, create *api.SheetCreate) (*she
payload
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
- RETURNING id, row_status, creator_id, created_ts, updater_id, updated_ts, project_id, database_id, name, LEFT(statement, 10240), visibility, source, type, payload, octet_length(statement)
- `
+ RETURNING id, row_status, creator_id, created_ts, updater_id, updated_ts, project_id, database_id, name, LEFT(statement, %d), visibility, source, type, payload, octet_length(statement)
+ `, common.MaxSheetSize)
var sheetRaw sheetRaw
databaseID := sql.NullInt32{}
if err := tx.QueryRowContext(ctx, query,
@@ -410,8 +410,8 @@ func patchSheetImpl(ctx context.Context, tx *Tx, patch *api.SheetPatch) (*sheetR
UPDATE sheet
SET `+strings.Join(set, ", ")+`
WHERE id = $%d
- RETURNING id, row_status, creator_id, created_ts, updater_id, updated_ts, project_id, database_id, name, LEFT(statement, 10240), visibility, source, type, payload, octet_length(statement)
- `, len(args)),
+ RETURNING id, row_status, creator_id, created_ts, updater_id, updated_ts, project_id, database_id, name, LEFT(statement, %d), visibility, source, type, payload, octet_length(statement)
+ `, len(args), common.MaxSheetSize),
args...,
).Scan(
&sheetRaw.ID,
@@ -482,7 +482,7 @@ func findSheetImpl(ctx context.Context, tx *Tx, find *api.SheetFind) ([]*sheetRa
if v := find.Type; v != nil {
where, args = append(where, fmt.Sprintf("type = $%d", len(args)+1)), append(args, *v)
}
- statementField := "LEFT(statement, 10240)"
+ statementField := fmt.Sprintf("LEFT(statement, %d)", common.MaxSheetSize)
if find.LoadFull {
statementField = "statement"
}