Skip to content

Commit

Permalink
Add ForceNew for table column name changes (#363)
Browse files Browse the repository at this point in the history
* Add ForceNew for table column name changes

* Table column comments updates
  • Loading branch information
bobbyiliev authored Nov 15, 2023
1 parent 15aea38 commit fce621d
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 9 deletions.
84 changes: 84 additions & 0 deletions pkg/provider/acceptance_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,40 @@ func TestAccTable_update(t *testing.T) {
resource.TestCheckResourceAttr("materialize_table.test_role", "ownership_role", roleName),
),
},
{
Config: testAccTableResourceWithUpdates(roleName, tableName, tableRoleName, "mz_system", "new_column_1", ""),
Check: resource.ComposeTestCheckFunc(
testAccCheckTableExists("materialize_table.test"),
resource.TestCheckResourceAttr("materialize_table.test", "name", tableName),
resource.TestCheckResourceAttr("materialize_table.test", "column.0.name", "new_column_1"),
resource.TestCheckResourceAttr("materialize_table.test", "column.0.type", "text"),
resource.TestCheckResourceAttr("materialize_table.test", "column.1.name", "column_2"),
resource.TestCheckResourceAttr("materialize_table.test", "column.1.type", "int"),
resource.TestCheckResourceAttr("materialize_table.test", "column.2.name", "column_3"),
resource.TestCheckResourceAttr("materialize_table.test", "column.2.type", "text"),
resource.TestCheckResourceAttr("materialize_table.test", "schema_name", "public"),
resource.TestCheckResourceAttr("materialize_table.test", "database_name", "materialize"),
resource.TestCheckResourceAttr("materialize_table.test", "ownership_role", "mz_system"),
resource.TestCheckResourceAttr("materialize_table.test", "qualified_sql_name", fmt.Sprintf(`"materialize"."public"."%s"`, tableName)),
resource.TestCheckResourceAttr("materialize_table.test", "column.#", "3"),
testAccCheckTableExists("materialize_table.test_role"),
resource.TestCheckResourceAttr("materialize_table.test_role", "name", tableRoleName),
resource.TestCheckResourceAttr("materialize_table.test_role", "ownership_role", roleName),
),
ResourceName: "materialize_table.test",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccTableResourceWithUpdates(roleName, tableName, tableRoleName, "mz_system", "", "Updated comment"),
Check: resource.ComposeTestCheckFunc(
testAccCheckTableExists("materialize_table.test"),
resource.TestCheckResourceAttr("materialize_table.test", "column.1.comment", "Updated comment"),
resource.TestCheckResourceAttr("materialize_table.test", "name", tableName),
resource.TestCheckResourceAttr("materialize_table.test", "column.0.name", "column_1"),
resource.TestCheckResourceAttr("materialize_table.test", "column.0.type", "text"),
),
},
},
})
}
Expand Down Expand Up @@ -180,3 +214,53 @@ func testAccCheckAllTablesDestroyed(s *terraform.State) error {

return nil
}

func testAccTableResourceWithUpdates(roleName, tableName, tableRoleName, tableOwnership, newColumnName, updatedComment string) string {
columnName1 := "column_1"
if newColumnName != "" {
columnName1 = newColumnName
}

commentColumn2 := "comment"
if updatedComment != "" {
commentColumn2 = updatedComment
}

return fmt.Sprintf(`
resource "materialize_role" "test" {
name = "%s"
}
resource "materialize_table" "test" {
name = "%s"
comment = "Initial table comment"
column {
name = "%s"
type = "text"
}
column {
name = "column_2"
type = "int"
comment = "%s"
}
column {
name = "column_3"
type = "text"
nullable = true
}
ownership_role = "%s"
}
resource "materialize_table" "test_role" {
name = "%s"
ownership_role = "%s"
column {
name = "%s"
type = "text"
}
depends_on = [materialize_role.test]
}
`, roleName, tableName, columnName1, commentColumn2, tableOwnership, tableRoleName, tableOwnership, columnName1)
}
28 changes: 19 additions & 9 deletions pkg/resources/resource_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ var tableSchema = map[string]*schema.Schema{
Description: "The name of the column to be created in the table.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"type": {
Description: "The data type of the column indicated by name.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: func(val any) string {
alias, ok := aliases[val.(string)]
if ok {
Expand All @@ -43,6 +45,7 @@ var tableSchema = map[string]*schema.Schema{
"nullable": {
Description: "Do not allow the column to contain NULL values. Columns without this constraint can contain NULL values.",
Type: schema.TypeBool,
ForceNew: true,
Optional: true,
Default: false,
},
Expand Down Expand Up @@ -232,15 +235,22 @@ func tableUpdate(ctx context.Context, d *schema.ResourceData, meta interface{})
}
}

if d.HasChange("columns") {
_, newColumns := d.GetChange("columns")
columns := materialize.GetTableColumnStruct(newColumns.([]interface{}))
comment := materialize.NewCommentBuilder(meta.(*sqlx.DB), o)

// Reset all comments if change present
for _, c := range columns {
if c.Comment != "" {
if err := comment.Column(c.ColName, c.Comment); err != nil {
if d.HasChange("column") {
oldColumns, newColumns := d.GetChange("column")
oldColumnsList := oldColumns.([]interface{})
newColumnsList := newColumns.([]interface{})

for index, newColMap := range newColumnsList {
newCol := newColMap.(map[string]interface{})
oldCol := oldColumnsList[index].(map[string]interface{})

// Check specifically if the column comment has changed.
if newCol["comment"] != oldCol["comment"] {
// Apply the comment change
comment := materialize.NewCommentBuilder(meta.(*sqlx.DB), o)
colName := newCol["name"].(string)
colComment := newCol["comment"].(string)
if err := comment.Column(colName, colComment); err != nil {
return diag.FromErr(err)
}
}
Expand Down

0 comments on commit fce621d

Please sign in to comment.