Skip to content

Commit

Permalink
Fixed handling of comments in databricks_sql_table resource (#2472)
Browse files Browse the repository at this point in the history
* column comments and single quote escape

* Delimiter collision avoidance table comment

* compatible with user single quote escape

* unit tests for parseComment

* corrected fmt

---------

Co-authored-by: Miles Yucht <miles@databricks.com>
  • Loading branch information
karolusz and mgyucht committed Jul 24, 2023
1 parent c2e7a39 commit 2f6a307
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
12 changes: 8 additions & 4 deletions catalog/resource_sql_table.go
Expand Up @@ -57,6 +57,10 @@ func (ti *SqlTableInfo) FullName() string {
return fmt.Sprintf("%s.%s.%s", ti.CatalogName, ti.SchemaName, ti.Name)
}

func parseComment(s string) string {
return strings.ReplaceAll(strings.ReplaceAll(s, `\'`, `'`), `'`, `\'`)
}

// These properties are added automatically
// If we do not customize the diff using these then terraform will constantly try to remove them
// `properties` is essentially a "partially" computed field
Expand Down Expand Up @@ -146,9 +150,9 @@ func (ti *SqlTableInfo) serializeColumnInfo(col SqlColumnInfo) string {

comment := ""
if col.Comment != "" {
comment = fmt.Sprintf(" COMMENT %s", col.Comment)
comment = fmt.Sprintf(" COMMENT '%s'", parseComment(col.Comment))
}
return fmt.Sprintf("%s %s%s%s", col.Name, col.Type, notNull, comment) // id INT NOT NULL COMMENT something
return fmt.Sprintf("%s %s%s%s", col.Name, col.Type, notNull, comment) // id INT NOT NULL COMMENT 'something'
}

func (ti *SqlTableInfo) serializeColumnInfos() string {
Expand Down Expand Up @@ -211,7 +215,7 @@ func (ti *SqlTableInfo) buildTableCreateStatement() string {
}

if ti.Comment != "" {
statements = append(statements, fmt.Sprintf("\nCOMMENT '%s'", ti.Comment)) // COMMENT 'this is a comment'
statements = append(statements, fmt.Sprintf("\nCOMMENT '%s'", parseComment(ti.Comment))) // COMMENT 'this is a comment'
}

if len(ti.Properties) > 0 {
Expand Down Expand Up @@ -249,7 +253,7 @@ func (ti *SqlTableInfo) diff(oldti *SqlTableInfo) ([]string, error) {

// Attributes common to both views and tables
if ti.Comment != oldti.Comment {
statements = append(statements, fmt.Sprintf("COMMENT ON %s %s IS '%s'", typestring, ti.FullName(), ti.Comment))
statements = append(statements, fmt.Sprintf("COMMENT ON %s %s IS '%s'", typestring, ti.FullName(), parseComment(ti.Comment)))
}

if !reflect.DeepEqual(ti.Properties, oldti.Properties) {
Expand Down
24 changes: 24 additions & 0 deletions catalog/resource_sql_table_test.go
Expand Up @@ -485,3 +485,27 @@ var createClusterForSql = append([]qa.HTTPFixture{
func TestResourceSqlTableCornerCases(t *testing.T) {
qa.ResourceCornerCases(t, ResourceSqlTable())
}

func TestParseComment_empty(t *testing.T) {
cmt := ""
prsd := parseComment(cmt)
assert.Equal(t, "", prsd)
}

func TestParseComment_noquote(t *testing.T) {
cmt := "Comment without single quote"
prsd := parseComment(cmt)
assert.Equal(t, "Comment without single quote", prsd)
}

func TestParseComment_escapedquote(t *testing.T) {
cmt := `\'Comment with\'escaped quotes\'`
prsd := parseComment(cmt)
assert.Equal(t, `\'Comment with\'escaped quotes\'`, prsd)
}

func TestParseComment_unescapedquote(t *testing.T) {
cmt := "Comment with' unescaped quotes '"
prsd := parseComment(cmt)
assert.Equal(t, `Comment with\' unescaped quotes \'`, prsd)
}

0 comments on commit 2f6a307

Please sign in to comment.