Skip to content

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

fix: task run log for mssql & oracle (#12028)

p0ny6423abb8090f
diff --git a/backend/component/sheet/sheet.go b/backend/component/sheet/sheet.go
index 1c403d9559..08e56fc824 100644
--- a/backend/component/sheet/sheet.go
+++ b/backend/component/sheet/sheet.go
@@ -9,6 +9,8 @@ import (
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/plugin/db/mssql"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
+ "github.com/bytebase/bytebase/backend/plugin/parser/plsql"
+ tsqlbatch "github.com/bytebase/bytebase/backend/plugin/parser/tsql/batch"
"github.com/bytebase/bytebase/backend/store"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
@@ -36,6 +38,8 @@ func getSheetCommands(engine storepb.Engine, statement string) []*storepb.SheetC
}
switch engine {
+ case storepb.Engine_ORACLE:
+ return getSheetCommandsForOracle(statement)
case storepb.Engine_MSSQL:
return getSheetCommandsForMSSQL(statement)
default:
@@ -65,13 +69,32 @@ func getSheetCommandsGeneral(engine storepb.Engine, statement string) []*storepb
return sheetCommands
}
+func getSheetCommandsForOracle(statement string) []*storepb.SheetCommand {
+ singleSQLs, err := plsql.SplitSQL(statement)
+ if err != nil {
+ if !strings.Contains(err.Error(), "not supported") {
+ slog.Warn("failed to get sheet command for oracle", "statement", statement)
+ }
+ return nil
+ }
+
+ var sheetCommands []*storepb.SheetCommand
+ for _, s := range singleSQLs {
+ sheetCommands = append(sheetCommands, &storepb.SheetCommand{
+ Start: int32(s.ByteOffsetStart),
+ End: int32(s.ByteOffsetEnd),
+ })
+ }
+ return sheetCommands
+}
+
func getSheetCommandsForMSSQL(statement string) []*storepb.SheetCommand {
var sheetCommands []*storepb.SheetCommand
p := 0
batch := mssql.NewBatch(statement)
for {
- _, err := batch.Next()
+ command, err := batch.Next()
if err == io.EOF {
np := p + len(batch.String())
sheetCommands = append(sheetCommands, &storepb.SheetCommand{
@@ -84,12 +107,19 @@ func getSheetCommandsForMSSQL(statement string) []*storepb.SheetCommand {
slog.Warn("failed to get sheet commands for mssql", "statement", statement)
return nil
}
- np := p + len(batch.String())
- sheetCommands = append(sheetCommands, &storepb.SheetCommand{
- Start: int32(p),
- End: int32(np),
- })
- p = np
+ if command == nil {
+ continue
+ }
+ switch command.(type) {
+ case *tsqlbatch.GoCommand:
+ np := p + len(batch.String())
+ sheetCommands = append(sheetCommands, &storepb.SheetCommand{
+ Start: int32(p),
+ End: int32(np),
+ })
+ p = np
+ default:
+ }
}
return sheetCommands
}
diff --git a/backend/plugin/db/mssql/mssql.go b/backend/plugin/db/mssql/mssql.go
index b30138bc98..a2b0073efe 100644
--- a/backend/plugin/db/mssql/mssql.go
+++ b/backend/plugin/db/mssql/mssql.go
@@ -109,7 +109,7 @@ func (driver *Driver) Execute(ctx context.Context, statement string, opts db.Exe
batch := NewBatch(statement)
- for idx := 0; ; idx++ {
+ for idx := 0; ; {
command, err := batch.Next()
if err != nil {
if err == io.EOF {
@@ -139,6 +139,7 @@ func (driver *Driver) Execute(ctx context.Context, statement string, opts db.Exe
// Try send the batch to server.
indexes := []int32{int32(idx)}
+ idx++
for i := uint(0); i < v.Count; i++ {
opts.LogCommandExecute(indexes)
rowsAffected, err := execute(ctx, tx, stmt)
diff --git a/backend/plugin/parser/base/base.go b/backend/plugin/parser/base/base.go
index d3d5c6093c..094311807b 100644
--- a/backend/plugin/parser/base/base.go
+++ b/backend/plugin/parser/base/base.go
@@ -26,6 +26,15 @@ type SingleSQL struct {
LastColumn int
// The sql is empty, such as `/* comments */;` or just `;`.
Empty bool
+
+ // ByteOffsetStart is the start position of the sql.
+ // This field may not be present for every engine.
+ // ByteOffsetStart is intended for sql execution log display. It may not represent the actual sql that is sent to the database.
+ ByteOffsetStart int
+ // ByteOffsetEnd is the end position of the sql.
+ // This field may not be present for every engine.
+ // ByteOffsetEnd is intended for sql execution log display. It may not represent the actual sql that is sent to the database.
+ ByteOffsetEnd int
}
// SyntaxError is a syntax error.
diff --git a/backend/plugin/parser/plsql/split.go b/backend/plugin/parser/plsql/split.go
index 333768fc1d..c7d5044ae1 100644
--- a/backend/plugin/parser/plsql/split.go
+++ b/backend/plugin/parser/plsql/split.go
@@ -22,11 +22,19 @@ func SplitSQL(statement string) ([]base.SingleSQL, error) {
return nil, err
}
+ byteOffsetStart := 0
+ prevStopTokenIndex := -1
var result []base.SingleSQL
for _, item := range tree.GetChildren() {
if stmt, ok := item.(parser.IUnit_statementContext); ok {
text := ""
lastLine := 0
+ // tokens looks like
+ if startTokenIndex := stmt.GetStart().GetTokenIndex(); startTokenIndex-1 >= 0 && prevStopTokenIndex+1 <= startTokenIndex-1 {
+ byteOffsetStart += len(tokens.GetTextFromTokens(tokens.Get(prevStopTokenIndex+1), tokens.Get(stmt.GetStart().GetTokenIndex()-1)))
+ }
+ byteOffsetEnd := byteOffsetStart + len(tokens.GetTextFromTokens(stmt.GetStart(), stmt.GetStop()))
+
// The go-ora driver requires semicolon for anonymous block,
// but does not support semicolon for other statements.
if needSemicolon(stmt) {
@@ -48,10 +56,14 @@ func SplitSQL(statement string) ([]base.SingleSQL, error) {
}
result = append(result, base.SingleSQL{
- Text: text,
- LastLine: lastLine,
- Empty: false,
+ Text: text,
+ LastLine: lastLine,
+ Empty: false,
+ ByteOffsetStart: byteOffsetStart,
+ ByteOffsetEnd: byteOffsetEnd,
})
+ byteOffsetStart = byteOffsetEnd
+ prevStopTokenIndex = stmt.GetStop().GetTokenIndex()
}
}
return result, nil
diff --git a/backend/plugin/parser/plsql/split_test.go b/backend/plugin/parser/plsql/split_test.go
index 3dab960c67..ab6de71734 100644
--- a/backend/plugin/parser/plsql/split_test.go
+++ b/backend/plugin/parser/plsql/split_test.go
@@ -31,12 +31,16 @@ func TestOracleSplitMultiSQL(t *testing.T) {
want: resData{
res: []base.SingleSQL{
{
- Text: `select * from t`,
- LastLine: 2,
+ Text: `select * from t`,
+ LastLine: 2,
+ ByteOffsetStart: 5,
+ ByteOffsetEnd: 20,
},
{
- Text: `create table table$1 (id int)`,
- LastLine: 3,
+ Text: `create table table$1 (id int)`,
+ LastLine: 3,
+ ByteOffsetStart: 26,
+ ByteOffsetEnd: 56,
},
},
},
diff --git a/backend/runner/taskrun/executor.go b/backend/runner/taskrun/executor.go
index 3e3c99058d..dd79a14e77 100644
--- a/backend/runner/taskrun/executor.go
+++ b/backend/runner/taskrun/executor.go
@@ -232,7 +232,7 @@ func executeMigration(
switch task.Type {
case api.TaskDatabaseSchemaUpdate, api.TaskDatabaseDataUpdate:
switch instance.Engine {
- case storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_OCEANBASE, storepb.Engine_STARROCKS, storepb.Engine_DORIS, storepb.Engine_POSTGRES, storepb.Engine_REDSHIFT, storepb.Engine_RISINGWAVE, storepb.Engine_ORACLE, storepb.Engine_DM, storepb.Engine_OCEANBASE_ORACLE:
+ case storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_OCEANBASE, storepb.Engine_STARROCKS, storepb.Engine_DORIS, storepb.Engine_POSTGRES, storepb.Engine_REDSHIFT, storepb.Engine_RISINGWAVE, storepb.Engine_ORACLE, storepb.Engine_DM, storepb.Engine_OCEANBASE_ORACLE, storepb.Engine_MSSQL:
opts.ChunkedSubmission = true
opts.UpdateExecutionStatus = func(detail *v1pb.TaskRun_ExecutionDetail) {
stateCfg.TaskRunExecutionStatuses.Store(taskRunUID,