Skip to content

Commit

Permalink
Merge pull request #239 from MaterializeInc/fix-quote-ident
Browse files Browse the repository at this point in the history
Fix identifier quoting
  • Loading branch information
benesch authored Jul 15, 2023
2 parents f982d3c + 005cda6 commit 78f93c8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 31 deletions.
14 changes: 4 additions & 10 deletions pkg/materialize/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@ import (
"strings"
)

func QuoteString(input string) (output string) {
output = "'" + strings.Replace(input, "'", "''", -1) + "'"
return
func QuoteString(input string) string {
return "'" + strings.Replace(input, "'", "''", -1) + "'"
}

func QuoteIdentifier(input string) (output string) {
parts := strings.Split(input, ".")
for i, p := range parts {
parts[i] = `"` + strings.Replace(p, `"`, `""`, -1) + `"`
}
output = strings.Join(parts, ".")
return
func QuoteIdentifier(input string) string {
return `"` + strings.Replace(input, `"`, `""`, -1) + `"`
}

func QualifiedName(fields ...string) string {
Expand Down
48 changes: 27 additions & 21 deletions pkg/provider/acceptance_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,33 @@ import (
)

func TestAccDatabase_basic(t *testing.T) {
databaseName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
database2Name := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
roleName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testAccDatabaseResource(roleName, databaseName, database2Name, roleName),
Check: resource.ComposeTestCheckFunc(
testAccCheckDatabaseExists("materialize_database.test"),
resource.TestCheckResourceAttr("materialize_database.test", "name", databaseName),
resource.TestCheckResourceAttr("materialize_database.test", "ownership_role", "mz_system"),
testAccCheckDatabaseExists("materialize_database.test_role"),
resource.TestCheckResourceAttr("materialize_database.test_role", "name", database2Name),
resource.TestCheckResourceAttr("materialize_database.test_role", "ownership_role", roleName),
),
},
},
})
for _, roleName := range []string{
acctest.RandStringFromCharSet(10, acctest.CharSetAlpha),
acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) + "@materialize.com",
} {
t.Run(fmt.Sprintf("roleName=%s", roleName), func(t *testing.T) {
databaseName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
database2Name := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testAccDatabaseResource(roleName, databaseName, database2Name, roleName),
Check: resource.ComposeTestCheckFunc(
testAccCheckDatabaseExists("materialize_database.test"),
resource.TestCheckResourceAttr("materialize_database.test", "name", databaseName),
resource.TestCheckResourceAttr("materialize_database.test", "ownership_role", "mz_system"),
testAccCheckDatabaseExists("materialize_database.test_role"),
resource.TestCheckResourceAttr("materialize_database.test_role", "name", database2Name),
resource.TestCheckResourceAttr("materialize_database.test_role", "ownership_role", roleName),
),
},
},
})
})
}
}

func TestAccDatabase_disappears(t *testing.T) {
Expand Down
19 changes: 19 additions & 0 deletions pkg/resources/resource_grant_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ func TestResourceGrantDatabaseCreate(t *testing.T) {
})
}

func TestResourceGrantDatabaseCreateEmail(t *testing.T) {
r := require.New(t)

in := map[string]interface{}{
"role_name": "[email protected]",
"privilege": "CREATE",
"database_name": "materialize",
}
d := schema.TestResourceDataRaw(t, GrantDatabase().Schema, in)
r.NotNil(d)

testhelpers.WithMockDb(t, func(db *sqlx.DB, mock sqlmock.Sqlmock) {
// Create
mock.ExpectExec(
`GRANT CREATE ON DATABASE "materialize" TO "[email protected]";`,
).WillReturnResult(sqlmock.NewResult(1, 1))
})
}

func TestResourceGrantDatabaseDelete(t *testing.T) {
r := require.New(t)

Expand Down

0 comments on commit 78f93c8

Please sign in to comment.