From d87c9591b3e8676567cb2997f23cde56b9918707 Mon Sep 17 00:00:00 2001 From: WangXiangUSTC Date: Wed, 26 Jun 2019 13:40:37 +0800 Subject: [PATCH] address comment --- loader/util.go | 6 +++++- loader/util_test.go | 30 +++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/loader/util.go b/loader/util.go index 88ca597e32..a2c2e420ba 100644 --- a/loader/util.go +++ b/loader/util.go @@ -84,6 +84,10 @@ func generateSchemaCreateFile(dir string, schema string) error { } defer file.Close() - _, err = fmt.Fprintf(file, "CREATE DATABASE `%s`;\n", schema) + _, err = fmt.Fprintf(file, "CREATE DATABASE `%s`;\n", escapeName(schema)) return errors.Trace(err) } + +func escapeName(name string) string { + return strings.Replace(name, "`", "``", -1) +} diff --git a/loader/util_test.go b/loader/util_test.go index 679d602a0e..f35f103c8a 100644 --- a/loader/util_test.go +++ b/loader/util_test.go @@ -68,15 +68,27 @@ func (t *testUtilSuite) TestShortSha1(c *C) { func (t *testUtilSuite) TestGenerateSchemaCreateFile(c *C) { dir := c.MkDir() - schema := "loader_test" - - err := generateSchemaCreateFile(dir, schema) - c.Assert(err, IsNil) + testCases := []struct { + schema string + createSQL string + }{ + { + "loader_test", + "CREATE DATABASE `loader_test`;\n", + }, { + "loader`test", + "CREATE DATABASE `loader``test`;\n", + }, + } + for _, testCase := range testCases { + err := generateSchemaCreateFile(dir, testCase.schema) + c.Assert(err, IsNil) - file, err := os.Open(path.Join(dir, fmt.Sprintf("%s-schema-create.sql", schema))) - c.Assert(err, IsNil) + file, err := os.Open(path.Join(dir, fmt.Sprintf("%s-schema-create.sql", testCase.schema))) + c.Assert(err, IsNil) - data, err := ioutil.ReadAll(file) - c.Assert(err, IsNil) - c.Assert(string(data), Equals, "CREATE DATABASE `loader_test`;\n") + data, err := ioutil.ReadAll(file) + c.Assert(err, IsNil) + c.Assert(string(data), Equals, testCase.createSQL) + } }