Skip to content

Commit

Permalink
Fix not quote db name in file data ddl (#961) (#967)
Browse files Browse the repository at this point in the history
  • Loading branch information
sre-bot authored May 29, 2020
1 parent 332f3e5 commit c9e8ea4
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
11 changes: 10 additions & 1 deletion drainer/translator/pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package translator
import (
"fmt"
"io"
"strings"
"time"

"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -48,7 +49,7 @@ func TiBinlogToPbBinlog(infoGetter TableInfoGetter, schema string, table string,
if isCreateDatabase {
sql += ";"
} else {
sql = fmt.Sprintf("use %s; %s;", schema, sql)
sql = fmt.Sprintf("use %s; %s;", quoteName(schema), sql)
}

pbBinlog.Tp = pb.BinlogType_DDL
Expand Down Expand Up @@ -303,3 +304,11 @@ func packEvent(schemaName, tableName string, tp pb.EventType, rowData [][]byte)

return event
}

func escapeName(name string) string {
return strings.Replace(name, "`", "``", -1)
}

func quoteName(name string) string {
return "`" + escapeName(name) + "`"
}
2 changes: 1 addition & 1 deletion drainer/translator/pb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (t *testPbSuite) TestDDL(c *check.C) {
c.Assert(err, check.IsNil)

c.Log("get ddl: ", string(pbBinog.GetDdlQuery()))
expected := fmt.Sprintf("use %s; %s;", t.Schema, string(t.TiBinlog.GetDdlQuery()))
expected := fmt.Sprintf("use `%s`; %s;", t.Schema, string(t.TiBinlog.GetDdlQuery()))
c.Assert(pbBinog, check.DeepEquals, &pb.Binlog{
Tp: pb.BinlogType_DDL,
CommitTs: t.TiBinlog.GetCommitTs(),
Expand Down
2 changes: 1 addition & 1 deletion tests/reparo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ batch = 10
host = "127.0.0.1"
user = "root"
password = ""
name = "reparo_test"
name = "reparo-test"
port = 4000
6 changes: 4 additions & 2 deletions tests/reparo/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ run_drainer "$args" &

GO111MODULE=on go build -o out

run_sql "CREATE DATABASE IF NOT EXISTS \`reparo_test\`"
sleep 5

run_sql "CREATE DATABASE IF NOT EXISTS \`reparo-test\`"

./out -config ./config.toml > ${OUT_DIR-/tmp}/$TEST_NAME.out 2>&1

Expand All @@ -27,6 +29,6 @@ sleep 15
check_data ./sync_diff_inspector.toml

# clean up
run_sql "DROP DATABASE IF EXISTS \`reparo_test\`"
run_sql "DROP DATABASE IF EXISTS \`reparo-test\`"

killall drainer
2 changes: 1 addition & 1 deletion tests/reparo/sync_diff_inspector.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ tidb-instance-id = "source-1"
use-checksum=true

[[check-tables]]
schema = "reparo_test"
schema = "reparo-test"
tables = ["~.*"]

[target-db]
Expand Down

0 comments on commit c9e8ea4

Please sign in to comment.