Skip to content

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

chore: set role for backup and restore (#14578)

rebelicee79870bc5593
diff --git a/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go b/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go
index 697f793396..f96cfb92ab 100644
--- a/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go
+++ b/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go
@@ -52,7 +52,7 @@ func (*BuiltinPriorBackupCheckAdvisor) Check(ctx advisor.Context, _ string) ([]*
var needBackup []ast.Node
for _, stmt := range stmtList {
- switch n := stmt.(type) {
+ switch stmt.(type) {
case ast.DDLNode:
adviceList = append(adviceList, &storepb.Advice{
Status: level,
@@ -65,18 +65,6 @@ func (*BuiltinPriorBackupCheckAdvisor) Check(ctx advisor.Context, _ string) ([]*
})
case *ast.UpdateStmt, *ast.DeleteStmt:
needBackup = append(needBackup, stmt)
- case *ast.VariableSetStmt:
- if n.Name == "role" {
- adviceList = append(adviceList, &storepb.Advice{
- Status: level,
- Title: title,
- Content: "Backup cannot deal with SET ROLE statement",
- Code: advisor.BuiltinPriorBackupCheck.Int32(),
- StartPosition: &storepb.Position{
- Line: int32(stmt.LastLine()),
- },
- })
- }
}
}
diff --git a/backend/plugin/advisor/pg/test/builtin_prior_backup_check.yaml b/backend/plugin/advisor/pg/test/builtin_prior_backup_check.yaml
index 229929fa2a..ca1ed63e69 100644
--- a/backend/plugin/advisor/pg/test/builtin_prior_backup_check.yaml
+++ b/backend/plugin/advisor/pg/test/builtin_prior_backup_check.yaml
@@ -62,13 +62,3 @@
endposition: null
- statement: SET ROLE role1;DELETE FROM tech_book WHERE a > 1;
changeType: 2
- want:
- - status: 2
- code: 2001
- title: builtin.prior-backup-check
- content: Backup cannot deal with SET ROLE statement
- detail: ""
- startposition:
- line: 1
- column: 0
- endposition: null
diff --git a/backend/plugin/advisor/pg/test/column_comment.yaml b/backend/plugin/advisor/pg/test/column_comment.yaml
index 0e051d3ae9..69f7cc3286 100644
--- a/backend/plugin/advisor/pg/test/column_comment.yaml
+++ b/backend/plugin/advisor/pg/test/column_comment.yaml
@@ -1,43 +1,59 @@
- statement: |-
CREATE TABLE t(a int);
COMMENT ON COLUMN t.a IS 'comments';
-- statement: |-
- CREATE TABLE t(a int);
+ changeType: 0
+- statement: CREATE TABLE t(a int);
+ changeType: 0
want:
- status: 2
code: 1032
title: column.comment
content: Comment is required for column `t.a`
+ detail: ""
startposition:
line: 1
+ column: 0
+ endposition: null
- statement: |-
CREATE TABLE t(a int);
COMMENT ON COLUMN t.a IS 'loooooooong comments';
+ changeType: 0
want:
- status: 2
code: 1301
title: column.comment
content: Column `t.a` comment is too long. The length of comment should be within 10 characters
+ detail: ""
startposition:
line: 2
+ column: 0
+ endposition: null
- statement: |-
CREATE TABLE t(a int, d text);
COMMENT ON COLUMN t.a IS 'comments';
+ changeType: 0
want:
- status: 2
code: 1032
title: column.comment
content: Comment is required for column `t.d`
+ detail: ""
startposition:
line: 1
+ column: 0
+ endposition: null
- statement: |-
CREATE TABLE t(a int);
COMMENT ON COLUMN t.a IS 'loooooooong comments';
COMMENT ON COLUMN t.a IS NULL;
+ changeType: 0
want:
- status: 2
code: 1032
title: column.comment
content: Comment is required for column `t.a`
+ detail: ""
startposition:
line: 1
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/pg/test/table_comment.yaml b/backend/plugin/advisor/pg/test/table_comment.yaml
index 98abb276e6..bd241ec469 100644
--- a/backend/plugin/advisor/pg/test/table_comment.yaml
+++ b/backend/plugin/advisor/pg/test/table_comment.yaml
@@ -1,55 +1,75 @@
- statement: |-
CREATE TABLE t(a int);
COMMENT ON TABLE t IS 'comments';
-- statement: |-
- CREATE TABLE t(a int);
+ changeType: 0
+- statement: CREATE TABLE t(a int);
+ changeType: 0
want:
- status: 2
code: 1032
title: table.comment
content: Comment is required for table `t`
+ detail: ""
startposition:
line: 1
+ column: 0
+ endposition: null
- statement: |-
CREATE TABLE t(a int);
CREATE TABLE d(a int);
COMMENT ON TABLE t IS 'comments';
+ changeType: 0
want:
- status: 2
code: 1032
title: table.comment
content: Comment is required for table `d`
+ detail: ""
startposition:
line: 2
+ column: 0
+ endposition: null
- statement: |-
CREATE TABLE t(a int);
COMMENT ON TABLE t IS 'comments';
COMMENT ON TABLE t IS '';
+ changeType: 0
want:
- status: 2
code: 1032
title: table.comment
content: Comment is required for table `t`
+ detail: ""
startposition:
line: 1
+ column: 0
+ endposition: null
- statement: |-
CREATE TABLE t(a int);
COMMENT ON TABLE t IS 'comments';
COMMENT ON TABLE t IS NULL;
+ changeType: 0
want:
- status: 2
code: 1032
title: table.comment
content: Comment is required for table `t`
+ detail: ""
startposition:
line: 1
+ column: 0
+ endposition: null
- statement: |-
CREATE TABLE t(a int);
COMMENT ON TABLE t IS 'looooooong comments';
+ changeType: 0
want:
- status: 2
code: 1301
title: table.comment
content: Table `t` comment is too long. The length of comment should be within 10 characters
+ detail: ""
startposition:
line: 2
+ column: 0
+ endposition: null
diff --git a/backend/plugin/parser/pg/restore.go b/backend/plugin/parser/pg/restore.go
index 8c36e31830..5ea5e53b77 100644
--- a/backend/plugin/parser/pg/restore.go
+++ b/backend/plugin/parser/pg/restore.go
@@ -12,6 +12,8 @@ import (
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
+ "github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"
+ pgrawparser "github.com/bytebase/bytebase/backend/plugin/parser/sql/engine/pg"
"github.com/bytebase/bytebase/backend/store/model"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
@@ -43,10 +45,16 @@ func GenerateRestoreSQL(ctx context.Context, rCtx base.RestoreContext, statement
if truncated {
sqlForComment += "..."
}
- return doGenerate(ctx, rCtx, sqlForComment, tree, backupItem)
+
+ preAppendStatements, err := getPreAppendStatements(statement)
+ if err != nil {
+ return "", errors.Wrap(err, "failed to get pre-append statements")
+ }
+
+ return doGenerate(ctx, rCtx, sqlForComment, tree, backupItem, preAppendStatements)
}
-func doGenerate(ctx context.Context, rCtx base.RestoreContext, sqlForComment string, tree *ParseResult, backupItem *storepb.PriorBackupDetail_Item) (string, error) {
+func doGenerate(ctx context.Context, rCtx base.RestoreContext, sqlForComment string, tree *ParseResult, backupItem *storepb.PriorBackupDetail_Item, preAppendStatements string) (string, error) {
_, sourceDatabase, err := common.GetInstanceDatabaseID(backupItem.SourceTable.Database)
if err != nil {
return "", errors.Wrapf(err, "failed to get source database ID for %s", backupItem.SourceTable.Database)
@@ -98,6 +106,10 @@ func doGenerate(ctx context.Context, rCtx base.RestoreContext, sqlForComment str
if g.err != nil {
return "", g.err
}
+
+ if len(preAppendStatements) > 0 {
+ return fmt.Sprintf("%s\n/*\nOriginal SQL:\n%s\n*/\n%s", preAppendStatements, sqlForComment, g.result), nil
+ }
return fmt.Sprintf("/*\nOriginal SQL:\n%s\n*/\n%s", sqlForComment, g.result), nil
}
@@ -234,3 +246,20 @@ func inRange(start, end, targetStart, targetEnd *storepb.Position) bool {
}
return true
}
+
+func getPreAppendStatements(statement string) (string, error) {
+ nodes, err := pgrawparser.Parse(pgrawparser.ParseContext{}, statement)
+ if err != nil {
+ return "", errors.Wrap(err, "failed to parse statement")
+ }
+
+ for _, node := range nodes {
+ if n, ok := node.(*ast.VariableSetStmt); ok {
+ if n.Name == "role" {
+ return n.Text(), nil
+ }
+ }
+ }
+
+ return "", nil
+}
diff --git a/backend/runner/taskrun/data_update_executor.go b/backend/runner/taskrun/data_update_executor.go
index 4a6c261ba6..2619309b8d 100644
--- a/backend/runner/taskrun/data_update_executor.go
+++ b/backend/runner/taskrun/data_update_executor.go
@@ -22,6 +22,8 @@ import (
"github.com/bytebase/bytebase/backend/plugin/db"
"github.com/bytebase/bytebase/backend/plugin/db/oracle"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
+ "github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"
+ pgrawparser "github.com/bytebase/bytebase/backend/plugin/parser/sql/engine/pg"
"github.com/bytebase/bytebase/backend/store"
"github.com/bytebase/bytebase/backend/store/model"
)
@@ -155,7 +157,7 @@ func (exec *DataUpdateExecutor) shouldSkipBackupError(ctx context.Context, task
func (exec *DataUpdateExecutor) backupData(
ctx context.Context,
driverCtx context.Context,
- statement string,
+ originStatement string,
payload *storepb.TaskDatabaseUpdatePayload,
task *store.TaskMessage,
issueN *store.IssueMessage,
@@ -219,24 +221,33 @@ func (exec *DataUpdateExecutor) backupData(
}
}
- if len(statement) > common.MaxSheetCheckSize {
- return nil, errors.Errorf("statement size %d exceeds the limit %d", len(statement), common.MaxSheetCheckSize)
+ if len(originStatement) > common.MaxSheetCheckSize {
+ return nil, errors.Errorf("statement size %d exceeds the limit %d", len(originStatement), common.MaxSheetCheckSize)
}
prefix := "_" + time.Now().Format("20060102150405")
- statements, err := base.TransformDMLToSelect(ctx, instance.Engine, tc, statement, database.DatabaseName, backupDatabaseName, prefix)
+ statements, err := base.TransformDMLToSelect(ctx, instance.Engine, tc, originStatement, database.DatabaseName, backupDatabaseName, prefix)
if err != nil {
return nil, errors.Wrap(err, "failed to transform DML to select")
}
+ preAppendStatements, err := getPreAppendStatements(instance.Engine, originStatement)
+ if err != nil {
+ return nil, errors.Wrap(err, "failed to get pre append statements")
+ }
+
priorBackupDetail := &storepb.PriorBackupDetail{}
bbSource := fmt.Sprintf("task %d", task.ID)
if issueN != nil {
bbSource = fmt.Sprintf("issue %d", issueN.UID)
}
for _, statement := range statements {
- if _, err := driver.Execute(driverCtx, statement.Statement, db.ExecuteOptions{}); err != nil {
- return nil, errors.Wrapf(err, "failed to execute backup statement %q", statement.Statement)
+ backupStatement := statement.Statement
+ if preAppendStatements != "" {
+ backupStatement = preAppendStatements + backupStatement
+ }
+ if _, err := driver.Execute(driverCtx, backupStatement, db.ExecuteOptions{}); err != nil {
+ return nil, errors.Wrapf(err, "failed to execute backup statement %q", backupStatement)
}
switch instance.Engine {
case storepb.Engine_TIDB:
@@ -373,3 +384,24 @@ func BuildListDatabaseNamesFunc(storeInstance *store.Store) base.ListDatabaseNam
return names, nil
}
}
+
+func getPreAppendStatements(engine storepb.Engine, statement string) (string, error) {
+ if engine != storepb.Engine_POSTGRES {
+ return "", nil
+ }
+
+ nodes, err := pgrawparser.Parse(pgrawparser.ParseContext{}, statement)
+ if err != nil {
+ return "", errors.Wrap(err, "failed to parse statement")
+ }
+
+ for _, node := range nodes {
+ if n, ok := node.(*ast.VariableSetStmt); ok {
+ if n.Name == "role" {
+ return n.Text(), nil
+ }
+ }
+ }
+
+ return "", nil
+}