Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: correct position step 2 (#15819)
h3n4l74bc73cbb90a
diff --git a/backend/plugin/advisor/mysql/advisor_builtin_prior_backup_check.go b/backend/plugin/advisor/mysql/advisor_builtin_prior_backup_check.goindex a3a0940549..d567721811 100644--- a/backend/plugin/advisor/mysql/advisor_builtin_prior_backup_check.go+++ b/backend/plugin/advisor/mysql/advisor_builtin_prior_backup_check.go@@ -44,13 +44,11 @@ func (*StatementPriorBackupCheckAdvisor) Check(ctx context.Context, checkCtx advif len(checkCtx.Statements) > common.MaxSheetCheckSize {adviceList = append(adviceList, &storepb.Advice{- Status: level,- Title: title,- Content: fmt.Sprintf("The size of the SQL statements exceeds the maximum limit of %d bytes for backup", common.MaxSheetCheckSize),- Code: advisor.BuiltinPriorBackupCheck.Int32(),- StartPosition: &storepb.Position{- Line: 1,- },+ Status: level,+ Title: title,+ Content: fmt.Sprintf("The size of the SQL statements exceeds the maximum limit of %d bytes for backup", common.MaxSheetCheckSize),+ Code: advisor.BuiltinPriorBackupCheck.Int32(),+ StartPosition: advisor.DefaultPosition,})}@@ -72,13 +70,11 @@ func (*StatementPriorBackupCheckAdvisor) Check(ctx context.Context, checkCtx advdatabaseName := extractDatabaseName(checkCtx.PreUpdateBackupDetail.Database)if !advisor.DatabaseExists(ctx, checkCtx, databaseName) {adviceList = append(adviceList, &storepb.Advice{- Status: level,- Title: title,- Content: fmt.Sprintf("Need database %q to do prior backup but it does not exist", checkCtx.PreUpdateBackupDetail.Database),- Code: advisor.DatabaseNotExists.Int32(),- StartPosition: &storepb.Position{- Line: 1,- },+ Status: level,+ Title: title,+ Content: fmt.Sprintf("Need database %q to do prior backup but it does not exist", checkCtx.PreUpdateBackupDetail.Database),+ Code: advisor.DatabaseNotExists.Int32(),+ StartPosition: advisor.DefaultPosition,})}@@ -100,13 +96,11 @@ func (*StatementPriorBackupCheckAdvisor) Check(ctx context.Context, checkCtx adv}if stmtType != item.StatementType {adviceList = append(adviceList, &storepb.Advice{- Status: level,- Title: title,- Content: fmt.Sprintf("Prior backup cannot handle mixed DML statements on the same table %q", key),- Code: advisor.BuiltinPriorBackupCheck.Int32(),- StartPosition: &storepb.Position{- Line: 1,- },+ Status: level,+ Title: title,+ Content: fmt.Sprintf("Prior backup cannot handle mixed DML statements on the same table %q", key),+ Code: advisor.BuiltinPriorBackupCheck.Int32(),+ StartPosition: advisor.DefaultPosition,})break}diff --git a/backend/plugin/advisor/mysql/advisor_naming_identifier_no_keyword.go b/backend/plugin/advisor/mysql/advisor_naming_identifier_no_keyword.goindex 7809a9a03f..11a602ba72 100644--- a/backend/plugin/advisor/mysql/advisor_naming_identifier_no_keyword.go+++ b/backend/plugin/advisor/mysql/advisor_naming_identifier_no_keyword.go@@ -71,9 +71,7 @@ func (checker *namingIdentifierNoKeywordChecker) EnterPureIdentifier(ctx *mysql.identifier := trimBackTicks(textNode.GetText())advice := checker.checkIdentifier(identifier)if advice != nil {- advice.StartPosition = &storepb.Position{- Line: int32(checker.baseLine + ctx.GetStart().GetLine()),- }+ advice.StartPosition = advisor.ConvertANTLRLineToPosition(checker.baseLine + ctx.GetStart().GetLine())checker.adviceList = append(checker.adviceList, advice)}}@@ -83,9 +81,7 @@ func (checker *namingIdentifierNoKeywordChecker) EnterIdentifierKeyword(ctx *mysidentifier := ctx.GetText()advice := checker.checkIdentifier(identifier)if advice != nil {- advice.StartPosition = &storepb.Position{- Line: int32(checker.baseLine + ctx.GetStart().GetLine()),- }+ advice.StartPosition = advisor.ConvertANTLRLineToPosition(checker.baseLine + ctx.GetStart().GetLine())checker.adviceList = append(checker.adviceList, advice)}}diff --git a/backend/plugin/advisor/mysql/advisor_online_migration.go b/backend/plugin/advisor/mysql/advisor_online_migration.goindex 02b4963832..4e7ffeeeed 100644--- a/backend/plugin/advisor/mysql/advisor_online_migration.go+++ b/backend/plugin/advisor/mysql/advisor_online_migration.go@@ -62,13 +62,13 @@ func (*OnlineMigrationAdvisor) Check(ctx context.Context, checkCtx advisor.Conteif checkCtx.ChangeType == storepb.PlanCheckRunConfig_DDL_GHOST {if !advisor.DatabaseExists(ctx, checkCtx, ghostDatabaseName) {return []*storepb.Advice{- {Status: level,- Title: title,- Content: fmt.Sprintf("Needs database %q to save temporary data for online migration but it does not exist", ghostDatabaseName),- Code: advisor.DatabaseNotExists.Int32(),- StartPosition: &storepb.Position{- Line: 0,- }},+ {+ Status: level,+ Title: title,+ Content: fmt.Sprintf("Needs database %q to save temporary data for online migration but it does not exist", ghostDatabaseName),+ Code: advisor.DatabaseNotExists.Int32(),+ StartPosition: advisor.DefaultPosition,+ },}, nil}}diff --git a/backend/plugin/advisor/mysql/test/naming_identifier_no_keyword.yaml b/backend/plugin/advisor/mysql/test/naming_identifier_no_keyword.yamlindex b75a309249..0ae486c7bf 100644--- a/backend/plugin/advisor/mysql/test/naming_identifier_no_keyword.yaml+++ b/backend/plugin/advisor/mysql/test/naming_identifier_no_keyword.yaml@@ -19,7 +19,7 @@title: naming.identifier.no-keywordcontent: Identifier "execute" is a keyword and should be avoidedstartposition:- line: 1+ line: 0column: 0endposition: null- statement: CREATE TABLE `execute`(id int)@@ -30,7 +30,7 @@title: naming.identifier.no-keywordcontent: Identifier "execute" is a keyword and should be avoidedstartposition:- line: 1+ line: 0column: 0endposition: null- statement: CREATE TABLE `interval`(id int);@@ -41,7 +41,7 @@title: naming.identifier.no-keywordcontent: Identifier "interval" is a keyword and should be avoidedstartposition:- line: 1+ line: 0column: 0endposition: null- statement: "CREATE TABLE t(\n id int, \n `current_timestamp` int\n);\n"@@ -52,7 +52,7 @@title: naming.identifier.no-keywordcontent: Identifier "current_timestamp" is a keyword and should be avoidedstartposition:- line: 3+ line: 2column: 0endposition: null- statement: CREATE TABLE `test`.`interval`(id int);@@ -63,6 +63,6 @@title: naming.identifier.no-keywordcontent: Identifier "interval" is a keyword and should be avoidedstartposition:- line: 1+ line: 0column: 0endposition: nulldiff --git a/backend/plugin/advisor/oracle/advisor_builtin_prior_backup_check.go b/backend/plugin/advisor/oracle/advisor_builtin_prior_backup_check.goindex 6fae13f196..b9c3421874 100644--- a/backend/plugin/advisor/oracle/advisor_builtin_prior_backup_check.go+++ b/backend/plugin/advisor/oracle/advisor_builtin_prior_backup_check.go@@ -46,26 +46,22 @@ func (*StatementPriorBackupCheckAdvisor) Check(ctx context.Context, checkCtx advif len(checkCtx.Statements) > common.MaxSheetCheckSize {adviceList = append(adviceList, &storepb.Advice{- Status: level,- Title: title,- Content: fmt.Sprintf("The size of the SQL statements exceeds the maximum limit of %d bytes for backup", common.MaxSheetCheckSize),- Code: advisor.BuiltinPriorBackupCheck.Int32(),- StartPosition: &storepb.Position{- Line: 1,- },+ Status: level,+ Title: title,+ Content: fmt.Sprintf("The size of the SQL statements exceeds the maximum limit of %d bytes for backup", common.MaxSheetCheckSize),+ Code: advisor.BuiltinPriorBackupCheck.Int32(),+ StartPosition: advisor.DefaultPosition,})}databaseName := extractDatabaseName(checkCtx.PreUpdateBackupDetail.Database)if !advisor.DatabaseExists(ctx, checkCtx, databaseName) {adviceList = append(adviceList, &storepb.Advice{- Status: level,- Title: title,- Content: fmt.Sprintf("Need database %q to do prior backup but it does not exist", checkCtx.PreUpdateBackupDetail.Database),- Code: advisor.DatabaseNotExists.Int32(),- StartPosition: &storepb.Position{- Line: 0,- },+ Status: level,+ Title: title,+ Content: fmt.Sprintf("Need database %q to do prior backup but it does not exist", checkCtx.PreUpdateBackupDetail.Database),+ Code: advisor.DatabaseNotExists.Int32(),+ StartPosition: advisor.DefaultPosition,})return adviceList, nil}@@ -75,13 +71,11 @@ func (*StatementPriorBackupCheckAdvisor) Check(ctx context.Context, checkCtx advif checker.hasDDL {adviceList = append(adviceList, &storepb.Advice{- Status: level,- Title: title,- Content: "Prior backup cannot deal with mixed DDL and DML statements",- Code: int32(advisor.BuiltinPriorBackupCheck),- StartPosition: &storepb.Position{- Line: 0,- },+ Status: level,+ Title: title,+ Content: "Prior backup cannot deal with mixed DDL and DML statements",+ Code: int32(advisor.BuiltinPriorBackupCheck),+ StartPosition: advisor.DefaultPosition,})}@@ -105,13 +99,11 @@ func (*StatementPriorBackupCheckAdvisor) Check(ctx context.Context, checkCtx adv}if statementType != item.table.StatementType {adviceList = append(adviceList, &storepb.Advice{- Status: level,- Title: title,- Content: fmt.Sprintf("Prior backup cannot handle mixed DML statements on the same table %q", key),- Code: advisor.BuiltinPriorBackupCheck.Int32(),- StartPosition: &storepb.Position{- Line: 1,- },+ Status: level,+ Title: title,+ Content: fmt.Sprintf("Prior backup cannot handle mixed DML statements on the same table %q", key),+ Code: advisor.BuiltinPriorBackupCheck.Int32(),+ StartPosition: advisor.DefaultPosition,})break}diff --git a/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go b/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.goindex 4abb85fbd5..8aa4c3eb93 100644--- a/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go+++ b/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go@@ -68,13 +68,11 @@ func (*BuiltinPriorBackupCheckAdvisor) Check(_ context.Context, checkCtx advisorname := extractDatabaseName(checkCtx.PreUpdateBackupDetail.Database)if !checkCtx.Catalog.Origin.HasSchema(name) {adviceList = append(adviceList, &storepb.Advice{- Status: level,- Title: title,- Content: fmt.Sprintf("Need schema %q to do prior backup but it does not exist", name),- Code: advisor.SchemaNotExists.Int32(),- StartPosition: &storepb.Position{- Line: 0,- },+ Status: level,+ Title: title,+ Content: fmt.Sprintf("Need schema %q to do prior backup but it does not exist", name),+ Code: advisor.SchemaNotExists.Int32(),+ StartPosition: advisor.DefaultPosition,})}@@ -98,13 +96,11 @@ func (*BuiltinPriorBackupCheckAdvisor) Check(_ context.Context, checkCtx advisor}if statementType != item.table.StatementType {adviceList = append(adviceList, &storepb.Advice{- Status: level,- Title: title,- Content: fmt.Sprintf("The statement type is not the same for all statements on the same table %q", key),- Code: advisor.BuiltinPriorBackupCheck.Int32(),- StartPosition: &storepb.Position{- Line: 1,- },+ Status: level,+ Title: title,+ Content: fmt.Sprintf("The statement type is not the same for all statements on the same table %q", key),+ Code: advisor.BuiltinPriorBackupCheck.Int32(),+ StartPosition: advisor.DefaultPosition,})break}diff --git a/backend/plugin/advisor/pg/advisor_statement_check_set_role_variable.go b/backend/plugin/advisor/pg/advisor_statement_check_set_role_variable.goindex 0d01dbf48a..5a992ee761 100644--- a/backend/plugin/advisor/pg/advisor_statement_check_set_role_variable.go+++ b/backend/plugin/advisor/pg/advisor_statement_check_set_role_variable.go@@ -49,13 +49,11 @@ func (*StatementCheckSetRoleVariable) Check(_ context.Context, checkCtx advisor.if !hasSetRole {return []*storepb.Advice{{- Status: level,- Code: advisor.StatementCheckSetRoleVariable.Int32(),- Title: string(checkCtx.Rule.Type),- Content: "No SET ROLE statement found.",- StartPosition: &storepb.Position{- Line: 1,- },+ Status: level,+ Code: advisor.StatementCheckSetRoleVariable.Int32(),+ Title: string(checkCtx.Rule.Type),+ Content: "No SET ROLE statement found.",+ StartPosition: advisor.DefaultPosition,}}, nil}diff --git a/backend/plugin/advisor/pg/test/statement_check_set_role_variable.yaml b/backend/plugin/advisor/pg/test/statement_check_set_role_variable.yamlindex cde90234f0..93efe84749 100644--- a/backend/plugin/advisor/pg/test/statement_check_set_role_variable.yaml+++ b/backend/plugin/advisor/pg/test/statement_check_set_role_variable.yaml@@ -8,6 +8,6 @@title: statement.check-set-role-variablecontent: No SET ROLE statement found.startposition:- line: 1+ line: 0column: 0endposition: nulldiff --git a/backend/plugin/advisor/tidb/advisor_builtin_prior_backup_check.go b/backend/plugin/advisor/tidb/advisor_builtin_prior_backup_check.goindex 65a9116e05..e231ff57a1 100644--- a/backend/plugin/advisor/tidb/advisor_builtin_prior_backup_check.go+++ b/backend/plugin/advisor/tidb/advisor_builtin_prior_backup_check.go@@ -79,25 +79,21 @@ func (*StatementPriorBackupCheckAdvisor) Check(ctx context.Context, checkCtx advif !advisor.DatabaseExists(ctx, checkCtx, extractDatabaseName(checkCtx.PreUpdateBackupDetail.Database)) {adviceList = append(adviceList, &storepb.Advice{- Status: level,- Title: title,- Content: fmt.Sprintf("Need database %q to do prior backup but it does not exist", checkCtx.PreUpdateBackupDetail.Database),- Code: advisor.DatabaseNotExists.Int32(),- StartPosition: &storepb.Position{- Line: 0,- },+ Status: level,+ Title: title,+ Content: fmt.Sprintf("Need database %q to do prior backup but it does not exist", checkCtx.PreUpdateBackupDetail.Database),+ Code: advisor.DatabaseNotExists.Int32(),+ StartPosition: advisor.DefaultPosition,})}if len(updateStatements)+len(deleteStatements) > maxMixedDMLCount && !updateForOneTableWithUnique(checkCtx.DBSchema, updateStatements, deleteStatements) {adviceList = append(adviceList, &storepb.Advice{- Status: level,- Title: title,- Content: fmt.Sprintf("Prior backup is feasible only with up to %d statements that are either UPDATE or DELETE, or if all UPDATEs target the same table with a PRIMARY or UNIQUE KEY in the WHERE clause", maxMixedDMLCount),- Code: advisor.BuiltinPriorBackupCheck.Int32(),- StartPosition: &storepb.Position{- Line: 0,- },+ Status: level,+ Title: title,+ Content: fmt.Sprintf("Prior backup is feasible only with up to %d statements that are either UPDATE or DELETE, or if all UPDATEs target the same table with a PRIMARY or UNIQUE KEY in the WHERE clause", maxMixedDMLCount),+ Code: advisor.BuiltinPriorBackupCheck.Int32(),+ StartPosition: advisor.DefaultPosition,})}diff --git a/backend/plugin/advisor/utils.go b/backend/plugin/advisor/utils.goindex e195826cac..5d94096487 100644--- a/backend/plugin/advisor/utils.go+++ b/backend/plugin/advisor/utils.go@@ -13,6 +13,12 @@ import (storepb "github.com/bytebase/bytebase/proto/generated-go/store")+var (+ DefaultPosition = &storepb.Position{+ Line: 0,+ }+)+// NormalizeStatement limit the max length of the statements.func NormalizeStatement(statement string) string {maxLength := 1000