Skip to content

Commit

Permalink
Fix data type diff recognition for column types in table
Browse files Browse the repository at this point in the history
It will be addressed in detail as part of table rework

References: #2733
  • Loading branch information
sfc-gh-asawicki committed May 6, 2024
1 parent 402a194 commit 0677183
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 34 deletions.
5 changes: 5 additions & 0 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ This document is meant to help you migrate your Terraform config to the new newe
describe deprecations or breaking changes and help you to change your configuration to keep the same (or similar) behavior
across different versions.

## v0.89.0 ➞ v0.90.0
### snowflake_table resource changes
#### *(behavior change)* Validation to column type added
While solving issue [#2733](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2733) we have introduced diff suppression for `column.type`. To make it work correctly we have also added a validation to it. It should not cause any problems, but it's worth noting in case of any data types used that the provider is not aware of.

## v0.88.0 ➞ v0.89.0
#### *(behavior change)* ForceNew removed
The `ForceNew` field was removed in favor of in-place Update for `name` parameter in:
Expand Down
14 changes: 5 additions & 9 deletions pkg/resources/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"log"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -56,14 +55,11 @@ var tableSchema = map[string]*schema.Schema{
Description: "Column name",
},
"type": {
Type: schema.TypeString,
Required: true,
Description: "Column type, e.g. VARIANT",
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
// these are all equivalent as per https://docs.snowflake.com/en/sql-reference/data-types-text.html
varcharType := []string{"VARCHAR(16777216)", "VARCHAR", "text", "string", "NVARCHAR", "NVARCHAR2", "CHAR VARYING", "NCHAR VARYING"}
return slices.Contains(varcharType, new) && slices.Contains(varcharType, old)
},
Type: schema.TypeString,
Required: true,
Description: "Column type, e.g. VARIANT",
ValidateFunc: dataTypeValidateFunc,
DiffSuppressFunc: dataTypeDiffSuppressFunc,
},
"nullable": {
Type: schema.TypeBool,
Expand Down
64 changes: 64 additions & 0 deletions pkg/resources/table_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1948,3 +1948,67 @@ func setTableDataRetentionTime(t *testing.T, id sdk.SchemaObjectIdentifier, days
require.NoError(t, err)
}
}

// proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2733 is fixed
func TestAcc_Table_gh2733(t *testing.T) {
name := acc.TestClient().Ids.Alpha()

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: acc.CheckDestroy(t, resources.Table),
Steps: []resource.TestStep{
{
Config: tableConfigGh2733(acc.TestDatabaseName, acc.TestSchemaName, name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_table.test_table", "name", name),
resource.TestCheckResourceAttr("snowflake_table.test_table", "column.0.name", "MY_INT"),
resource.TestCheckResourceAttr("snowflake_table.test_table", "column.0.type", "NUMBER(38,0)"),
resource.TestCheckResourceAttr("snowflake_table.test_table", "column.1.name", "MY_STRING"),
resource.TestCheckResourceAttr("snowflake_table.test_table", "column.1.type", "VARCHAR(16777216)"),
resource.TestCheckResourceAttr("snowflake_table.test_table", "column.2.name", "MY_DATE"),
resource.TestCheckResourceAttr("snowflake_table.test_table", "column.2.type", "TIMESTAMP_NTZ(9)"),
resource.TestCheckResourceAttr("snowflake_table.test_table", "column.3.name", "MY_DATE2"),
resource.TestCheckResourceAttr("snowflake_table.test_table", "column.3.type", "TIMESTAMP_NTZ(9)"),
),
},
},
})
}

func tableConfigGh2733(database string, schema string, name string) string {
return fmt.Sprintf(`
resource "snowflake_table" "test_table" {
database = "%[1]s"
schema = "%[2]s"
name = "%[3]s"
column {
name = "MY_INT"
type = "int"
# type = "NUMBER(38,0)" # Should be equivalent
}
column {
name = "MY_STRING"
type = "VARCHAR(16777216)"
# type = "STRING" # Should be equivalent
}
column {
name = "MY_DATE"
type = "TIMESTAMP_NTZ"
# type = "TIMESTAMP_NTZ(9)" # Should be equivalent
}
column {
name = "MY_DATE2"
type = "DATETIME"
# type = "TIMESTAMP_NTZ" # Equivalent to TIMESTAMP_NTZ
}
}
`, database, schema, name)
}
Loading

0 comments on commit 0677183

Please sign in to comment.