Skip to content

Commit 71ad368

Browse files
Add backticks around column names (Fixes #219)
1 parent 5be87ac commit 71ad368

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

mysql/resource_grant.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,10 +1065,16 @@ func normalizeColumnOrder(perm string) string {
10651065

10661066
parts := strings.Split(m[2], ",")
10671067
for i := range parts {
1068+
// erase spaces and backticks, if any
10681069
parts[i] = strings.Trim(parts[i], "` ")
10691070
}
10701071
sort.Strings(parts)
10711072
precursor := strings.Trim(m[1], " ")
1073+
for i := range parts {
1074+
// put backticks around the column names
1075+
parts[i] = fmt.Sprintf("`%s`", parts[i])
1076+
}
1077+
// build comma separated string from the parts, using strictly one space after comma
10721078
partsTogether := strings.Join(parts, ", ")
10731079
return fmt.Sprintf("%s(%s)", precursor, partsTogether)
10741080
}

mysql/resource_grant_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ func TestAccGrantComplex(t *testing.T) {
189189
resource.TestCheckResourceAttr("mysql_grant.test", "table", "tbl"),
190190
),
191191
},
192+
{
193+
Config: testAccGrantConfigWithPrivs(dbName, "\"SELECT (`id`,`key`,`order`,`first`,`last`)\"", false),
194+
Check: resource.ComposeTestCheckFunc(
195+
resource.TestCheckResourceAttr("mysql_grant.test", "user", fmt.Sprintf("jdoe-%s", dbName)),
196+
resource.TestCheckResourceAttr("mysql_grant.test", "host", "example.com"),
197+
resource.TestCheckResourceAttr("mysql_grant.test", "database", dbName),
198+
resource.TestCheckResourceAttr("mysql_grant.test", "table", "tbl"),
199+
),
200+
},
192201
{
193202
Config: testAccGrantConfigWithPrivs(dbName, `"DROP", "SELECT (c1)", "INSERT(c3, c4)", "REFERENCES(c5)"`, false),
194203
Check: resource.ComposeTestCheckFunc(
@@ -484,7 +493,7 @@ func prepareTable(dbname string, tableName string) resource.TestCheckFunc {
484493
if err != nil {
485494
return err
486495
}
487-
if _, err := db.Exec(fmt.Sprintf("CREATE TABLE `%s`.`%s`(c1 INT, c2 INT, c3 INT,c4 INT,c5 INT);", dbname, tableName)); err != nil {
496+
if _, err := db.Exec(fmt.Sprintf("CREATE TABLE `%s`.`%s`(c1 INT, c2 INT, c3 INT, c4 INT, c5 INT, `id` INT, `key` INT, `order` INT, `type` INT, `status` INT, `created` INT, `updated` INT, `user` INT, `name` INT, `group` INT, `value` INT, `index` INT, `source` INT, `date` INT, `state` INT, `last` INT, `first` INT, `email` INT, `phone` INT, `amount` INT, `reference` INT, `reason` INT, `hash` INT, `team` INT, `case` INT, `uid` INT, `path` INT);", dbname, tableName)); err != nil {
488497
return fmt.Errorf("error reading grant: %s", err)
489498
}
490499
return nil

0 commit comments

Comments
 (0)