Skip to content

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

feat: load preview of a sheet by default (#3953)

Danny Xuca21c8a133a0
diff --git a/api/sheet.go b/api/sheet.go
index cf09a53221..df4127d554 100644
--- a/api/sheet.go
+++ b/api/sheet.go
@@ -78,6 +78,9 @@ type Sheet struct {
Payload string `jsonapi:"attr,payload"`
Starred bool `jsonapi:"attr,starred"`
Pinned bool `jsonapi:"attr,pinned"`
+
+ // Size is the size of statement in bytes.
+ Size int64 `jsonapi:"attr,size"`
}
// SheetCreate is the API message for creating a sheet.
@@ -125,6 +128,8 @@ type SheetFind struct {
// Used to find the creator's sheet list.
// When finding shared PROJECT/PUBLIC sheets, this value should be empty.
CreatorID *int
+ // LoadFull is used if we want to load the full sheet.
+ LoadFull bool
// Related fields
ProjectID *int
diff --git a/server/runner/taskrun/data_update_executor.go b/server/runner/taskrun/data_update_executor.go
index 9eddbbcdf3..16a6c0ac03 100644
--- a/server/runner/taskrun/data_update_executor.go
+++ b/server/runner/taskrun/data_update_executor.go
@@ -44,7 +44,7 @@ func (exec *DataUpdateExecutor) RunOnce(ctx context.Context, task *api.Task) (te
statement := payload.Statement
if payload.SheetID > 0 {
- sheet, err := exec.store.GetSheet(ctx, &api.SheetFind{ID: &payload.SheetID}, api.SystemBotID)
+ sheet, err := exec.store.GetSheet(ctx, &api.SheetFind{ID: &payload.SheetID, LoadFull: true}, api.SystemBotID)
if err != nil {
return true, nil, err
}
diff --git a/server/runner/taskrun/schema_update_executor.go b/server/runner/taskrun/schema_update_executor.go
index ddc12f4547..0ce39248e5 100644
--- a/server/runner/taskrun/schema_update_executor.go
+++ b/server/runner/taskrun/schema_update_executor.go
@@ -49,7 +49,7 @@ func (exec *SchemaUpdateExecutor) RunOnce(ctx context.Context, task *api.Task) (
statement := payload.Statement
if payload.SheetID > 0 {
- sheet, err := exec.store.GetSheet(ctx, &api.SheetFind{ID: &payload.SheetID}, api.SystemBotID)
+ sheet, err := exec.store.GetSheet(ctx, &api.SheetFind{ID: &payload.SheetID, LoadFull: true}, api.SystemBotID)
if err != nil {
return true, nil, err
}
diff --git a/store/sheet.go b/store/sheet.go
index 15e5c1eb94..27c17076c7 100644
--- a/store/sheet.go
+++ b/store/sheet.go
@@ -39,6 +39,7 @@ type sheetRaw struct {
Source api.SheetSource
Type api.SheetType
Payload string
+ Size int64
}
// toSheet creates an instance of Sheet based on the sheetRaw.
@@ -68,6 +69,7 @@ func (raw *sheetRaw) toSheet() *api.Sheet {
Source: raw.Source,
Type: raw.Type,
Payload: raw.Payload,
+ Size: raw.Size,
}
}
@@ -334,7 +336,7 @@ 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, statement, visibility, source, type, payload
+ 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)
`
var sheetRaw sheetRaw
databaseID := sql.NullInt32{}
@@ -364,6 +366,7 @@ func createSheetImpl(ctx context.Context, tx *Tx, create *api.SheetCreate) (*she
&sheetRaw.Source,
&sheetRaw.Type,
&sheetRaw.Payload,
+ &sheetRaw.Size,
); err != nil {
if err == sql.ErrNoRows {
return nil, common.FormatDBErrorEmptyRowWithQuery(query)
@@ -407,7 +410,7 @@ 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, statement, visibility, source, type, payload
+ 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)),
args...,
).Scan(
@@ -425,6 +428,7 @@ func patchSheetImpl(ctx context.Context, tx *Tx, patch *api.SheetPatch) (*sheetR
&sheetRaw.Source,
&sheetRaw.Type,
&sheetRaw.Payload,
+ &sheetRaw.Size,
); err != nil {
if err == sql.ErrNoRows {
return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("sheet ID not found: %d", patch.ID)}
@@ -478,8 +482,12 @@ 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)"
+ if find.LoadFull {
+ statementField = "statement"
+ }
- rows, err := tx.QueryContext(ctx, `
+ rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT
id,
row_status,
@@ -490,13 +498,14 @@ func findSheetImpl(ctx context.Context, tx *Tx, find *api.SheetFind) ([]*sheetRa
project_id,
database_id,
name,
- statement,
+ %s,
visibility,
source,
type,
- payload
+ payload,
+ octet_length(statement)
FROM sheet
- WHERE `+strings.Join(where, " AND "),
+ WHERE %s`, statementField, strings.Join(where, " AND ")),
args...,
)
if err != nil {
@@ -523,6 +532,7 @@ func findSheetImpl(ctx context.Context, tx *Tx, find *api.SheetFind) ([]*sheetRa
&sheetRaw.Source,
&sheetRaw.Type,
&sheetRaw.Payload,
+ &sheetRaw.Size,
); err != nil {
return nil, FormatError(err)
}