Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
WangXiangUSTC committed Jun 26, 2019
1 parent 65aed5e commit d87c959
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
6 changes: 5 additions & 1 deletion loader/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
30 changes: 21 additions & 9 deletions loader/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit d87c959

Please sign in to comment.