Skip to content

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

fix(backup): fix position for tidb (#13471)

rebelice6f9603d097ae
diff --git a/backend/plugin/parser/tidb/backup.go b/backend/plugin/parser/tidb/backup.go
index bf75763658..5eebaeabb5 100644
--- a/backend/plugin/parser/tidb/backup.go
+++ b/backend/plugin/parser/tidb/backup.go
@@ -35,10 +35,12 @@ func TransformDMLToSelect(ctx context.Context, tCtx base.TransformContext, state
}
type statementInfo struct {
- offset int
- statement string
- table *TableReference
- tree antlr.ParserRuleContext
+ offset int
+ statement string
+ table *TableReference
+ tree antlr.ParserRuleContext
+ startPosition *store.Position
+ endPosition *store.Position
}
func prepareTransformation(databaseName, statement string) ([]statementInfo, error) {
@@ -48,11 +50,11 @@ func prepareTransformation(databaseName, statement string) ([]statementInfo, err
}
var result []statementInfo
- for i, sql := range list {
- if len(sql.Text) == 0 || sql.Empty {
+ for i, item := range list {
+ if len(item.Text) == 0 || item.Empty {
continue
}
- parseResult, err := mysql.ParseMySQL(sql.Text)
+ parseResult, err := mysql.ParseMySQL(item.Text)
if err != nil {
return nil, errors.Wrap(err, "failed to parse sql")
}
@@ -65,7 +67,22 @@ func prepareTransformation(databaseName, statement string) ([]statementInfo, err
if err != nil {
return nil, errors.Wrap(err, "failed to extract tables")
}
- result = append(result, tables...)
+ for _, table := range tables {
+ result = append(result, statementInfo{
+ offset: i,
+ statement: table.statement,
+ table: table.table,
+ tree: table.tree,
+ startPosition: &store.Position{
+ Line: int32(item.FirstStatementLine) + 1,
+ Column: int32(item.FirstStatementColumn),
+ },
+ endPosition: &store.Position{
+ Line: int32(item.LastLine) + 1,
+ Column: int32(item.LastColumn),
+ },
+ })
+ }
}
}
@@ -166,14 +183,8 @@ func generateSQLForSingleTable(ctx context.Context, tCtx base.TransformContext,
Statement: buf.String(),
SourceTableName: table.Table,
TargetTableName: targetTable,
- StartPosition: &store.Position{
- Line: int32(statementInfoList[0].tree.GetStart().GetLine()),
- Column: int32(statementInfoList[0].tree.GetStart().GetColumn()),
- },
- EndPosition: &store.Position{
- Line: int32(statementInfoList[len(statementInfoList)-1].tree.GetStop().GetLine()),
- Column: int32(statementInfoList[len(statementInfoList)-1].tree.GetStop().GetColumn()),
- },
+ StartPosition: statementInfoList[0].startPosition,
+ EndPosition: statementInfoList[len(statementInfoList)-1].endPosition,
},
}, nil
}
@@ -259,14 +270,8 @@ func generateSQLForMixedDML(ctx context.Context, tCtx base.TransformContext, sta
Statement: buf.String(),
SourceTableName: table.Table,
TargetTableName: targetTable,
- StartPosition: &store.Position{
- Line: int32(statementInfo.tree.GetStart().GetLine()),
- Column: int32(statementInfo.tree.GetStart().GetColumn()),
- },
- EndPosition: &store.Position{
- Line: int32(statementInfo.tree.GetStop().GetLine()),
- Column: int32(statementInfo.tree.GetStop().GetColumn()),
- },
+ StartPosition: statementInfo.startPosition,
+ EndPosition: statementInfo.endPosition,
})
}
return result, nil
diff --git a/backend/plugin/parser/tidb/test-data/test_backup.yaml b/backend/plugin/parser/tidb/test-data/test_backup.yaml
index bc52012e23..9d4801d304 100644
--- a/backend/plugin/parser/tidb/test-data/test_backup.yaml
+++ b/backend/plugin/parser/tidb/test-data/test_backup.yaml
@@ -29,8 +29,8 @@
line: 1
column: 0
endposition:
- line: 2
- column: 44
+ line: 7
+ column: 45
- input: |-
UPDATE db1.t1 SET c1 = 1 WHERE c1 = 2;
UPDATE db2.t2 SET c1 = 1 WHERE c1 = 2;
@@ -46,7 +46,7 @@
column: 0
endposition:
line: 1
- column: 36
+ column: 37
- statement: |-
CREATE TABLE `backupDB`.`_rollback_1_t2` LIKE `db2`.`t2`;
INSERT INTO `backupDB`.`_rollback_1_t2` SELECT `t2`.* FROM db2.t2 WHERE c1 = 2;
@@ -58,7 +58,7 @@
column: 0
endposition:
line: 2
- column: 36
+ column: 37
- input: DELETE test FROM test, test2 as t2 where test.id = t2.id;
result:
- statement: |-
@@ -72,7 +72,7 @@
column: 0
endposition:
line: 1
- column: 53
+ column: 56
- input: DELETE t1, t2 FROM test as t1, test2 as t2 WHERE t1.id = t2.id;
result:
- statement: |-
@@ -86,7 +86,7 @@
column: 0
endposition:
line: 1
- column: 59
+ column: 62
- statement: |-
CREATE TABLE `backupDB`.`_rollback_0_test2` LIKE `db`.`test2`;
INSERT INTO `backupDB`.`_rollback_0_test2` SELECT `t2`.* FROM test as t1, test2 as t2 WHERE t1.id = t2.id;
@@ -98,7 +98,7 @@
column: 0
endposition:
line: 1
- column: 59
+ column: 62
- input: DELETE FROM t1, t2 USING test as t1, test2 as t2 WHERE t1.id = t2.id;
result:
- statement: |-
@@ -112,7 +112,7 @@
column: 0
endposition:
line: 1
- column: 65
+ column: 68
- statement: |-
CREATE TABLE `backupDB`.`_rollback_0_test2` LIKE `db`.`test2`;
INSERT INTO `backupDB`.`_rollback_0_test2` SELECT `t2`.* FROM test as t1, test2 as t2 WHERE t1.id = t2.id;
@@ -124,7 +124,7 @@
column: 0
endposition:
line: 1
- column: 65
+ column: 68
- input: DELETE FROM test as t1 WHERE t1.c1 = 1;
result:
- statement: |-
@@ -138,7 +138,7 @@
column: 0
endposition:
line: 1
- column: 37
+ column: 38
- input: DELETE FROM test WHERE c1 = 1;
result:
- statement: |-
@@ -152,7 +152,7 @@
column: 0
endposition:
line: 1
- column: 28
+ column: 29
- input: UPDATE test x SET x.c1 = 1 WHERE x.c1 = 1
result:
- statement: |-
@@ -180,7 +180,7 @@
column: 0
endposition:
line: 1
- column: 32
+ column: 33
- input: UPDATE test t1, test2 t2 SET t1.c1 = 1 WHERE t1.c1 = t2.c1;
result:
- statement: |-
@@ -194,7 +194,7 @@
column: 0
endposition:
line: 1
- column: 55
+ column: 58
- input: UPDATE test t1, test2 t2 SET t1.c1 = 1, t2.c2 = 2 WHERE t1.c1 = t2.c1;
result:
- statement: |-
@@ -208,7 +208,7 @@
column: 0
endposition:
line: 1
- column: 66
+ column: 69
- statement: |-
CREATE TABLE `backupDB`.`_rollback_0_test2` LIKE `db`.`test2`;
INSERT INTO `backupDB`.`_rollback_0_test2` SELECT `t2`.* FROM test t1, test2 t2 WHERE t1.c1 = t2.c1;
@@ -220,7 +220,7 @@
column: 0
endposition:
line: 1
- column: 66
+ column: 69
- input: |-
UPDATE test t1 SET t1.c1 = 2 WHERE t1.c1 = 1 ;
UPDATE test t2 SET t2.c1 = 3 WHERE t2.c1 = 5 ;
@@ -236,7 +236,7 @@
column: 0
endposition:
line: 1
- column: 43
+ column: 45
- statement: |-
CREATE TABLE `backupDB`.`_rollback_1_test` LIKE `db`.`test`;
INSERT INTO `backupDB`.`_rollback_1_test` SELECT `t2`.* FROM test t2 WHERE t2.c1 = 5;
@@ -248,4 +248,4 @@
column: 0
endposition:
line: 2
- column: 43
+ column: 45