Skip to content

Commit

Permalink
fix: alter table column data type (#2607)
Browse files Browse the repository at this point in the history
Fix for #2588
In the alter column call collate was always added which caused the error
non-text (and probably text too) data types

## Test Plan
<!-- detail ways in which this PR has been tested or needs to be tested
-->
* [x] test case for data type alter

## References
* [ALTER
TABLE](https://docs.snowflake.com/en/sql-reference/sql/alter-table)
  • Loading branch information
sfc-gh-jcieslak authored Mar 11, 2024
1 parent a2be7b9 commit 538b6dc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/resources/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,11 @@ func UpdateTable(d *schema.ResourceData, meta interface{}) error {
}
for _, cA := range changed {
if cA.changedDataType || cA.changedCollate {
err := client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithColumnAction(sdk.NewTableColumnActionRequest().WithAlter([]sdk.TableColumnAlterActionRequest{*sdk.NewTableColumnAlterActionRequest(fmt.Sprintf("\"%s\"", cA.newColumn.name)).WithType(sdk.Pointer(sdk.DataType(cA.newColumn.dataType))).WithCollate(sdk.String(cA.newColumn.collate))})))
var newCollation *string
if sdk.IsStringType(cA.newColumn.dataType) && cA.newColumn.collate != "" {
newCollation = sdk.String(cA.newColumn.collate)
}
err := client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithColumnAction(sdk.NewTableColumnActionRequest().WithAlter([]sdk.TableColumnAlterActionRequest{*sdk.NewTableColumnAlterActionRequest(fmt.Sprintf("\"%s\"", cA.newColumn.name)).WithType(sdk.Pointer(sdk.DataType(cA.newColumn.dataType))).WithCollate(newCollation)})))
if err != nil {
return fmt.Errorf("error changing property on %v: err %w", d.Id(), err)
}
Expand Down
51 changes: 51 additions & 0 deletions pkg/resources/table_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,57 @@ resource "snowflake_table" "test_table" {
`, name, databaseName, schemaName)
}

// proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2588 is fixed
func TestAcc_ColumnTypeChangeWithNonTextType(t *testing.T) {
accName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: testAccCheckTableDestroy,
Steps: []resource.TestStep{
{
Config: tableConfigWithNumberColumnType(accName, acc.TestDatabaseName, acc.TestSchemaName, "NUMBER(38,0)"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_table.test_table", "name", accName),
resource.TestCheckResourceAttr("snowflake_table.test_table", "database", acc.TestDatabaseName),
resource.TestCheckResourceAttr("snowflake_table.test_table", "schema", acc.TestSchemaName),
resource.TestCheckResourceAttr("snowflake_table.test_table", "column.0.name", "id"),
resource.TestCheckResourceAttr("snowflake_table.test_table", "column.0.type", "NUMBER(38,0)"),
),
},
{
Config: tableConfigWithNumberColumnType(accName, acc.TestDatabaseName, acc.TestSchemaName, "NUMBER(36,0)"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_table.test_table", "name", accName),
resource.TestCheckResourceAttr("snowflake_table.test_table", "database", acc.TestDatabaseName),
resource.TestCheckResourceAttr("snowflake_table.test_table", "schema", acc.TestSchemaName),
resource.TestCheckResourceAttr("snowflake_table.test_table", "column.0.name", "id"),
resource.TestCheckResourceAttr("snowflake_table.test_table", "column.0.type", "NUMBER(36,0)"),
),
},
},
})
}

func tableConfigWithNumberColumnType(name string, databaseName string, schemaName string, columnType string) string {
return fmt.Sprintf(`
resource "snowflake_table" "test_table" {
name = "%[1]s"
database = "%[2]s"
schema = "%[3]s"
column {
name = "id"
type = "%[4]s"
}
}
`, name, databaseName, schemaName, columnType)
}

func checkDatabaseSchemaAndTableDataRetentionTime(id sdk.SchemaObjectIdentifier, expectedDatabaseRetentionDays int, expectedSchemaRetentionDays int, expectedTableRetentionsDays int) func(state *terraform.State) error {
return func(state *terraform.State) error {
client := acc.TestAccProvider.Meta().(*provider.Context).Client
Expand Down

0 comments on commit 538b6dc

Please sign in to comment.