Skip to content

Commit

Permalink
fix: FileFormat not detecting changes correctly (#2436)
Browse files Browse the repository at this point in the history
ref
#2385

I detected 3 bugs:

1. `d.Get("format_type")` has to be casted first to type
`sdk.FileFormatType` in order to be compared with another
FileFormatType, if not, the comparison is always `False`.

2. The attribute `comment` was not part of the Update() function

3. The default init value of a pointer is `null`, this means we need to
account for this case


Apart from that:
My trial account expired today and I could not run the Acceptance Tests,
but I manually tested it and it seems to work

---------

Co-authored-by: Raül Bonet <[email protected]>
Co-authored-by: Artur Sawicki <[email protected]>
  • Loading branch information
3 people authored Feb 8, 2024
1 parent 5865655 commit 018bb74
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
10 changes: 8 additions & 2 deletions pkg/resources/file_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,9 +781,9 @@ func UpdateFileFormat(d *schema.ResourceData, meta interface{}) error {
}

runSet := false
opts := sdk.AlterFileFormatOptions{}
opts := sdk.AlterFileFormatOptions{Set: &sdk.FileFormatTypeOptions{}}

switch d.Get("format_type") {
switch sdk.FileFormatType(d.Get("format_type").(string)) {
case sdk.FileFormatTypeCSV:
if d.HasChange("compression") {
v := sdk.CSVCompression(d.Get("compression").(string))
Expand Down Expand Up @@ -1092,6 +1092,12 @@ func UpdateFileFormat(d *schema.ResourceData, meta interface{}) error {
}
}

if d.HasChange("comment") {
v := d.Get("comment").(string)
opts.Set.Comment = &v
runSet = true
}

if runSet {
err = client.FileFormats.Alter(ctx, id, &opts)
if err != nil {
Expand Down
18 changes: 13 additions & 5 deletions pkg/resources/file_format_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestAcc_FileFormatCSV(t *testing.T) {
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: fileFormatConfigCSV(accName, acc.TestDatabaseName, acc.TestSchemaName),
Config: fileFormatConfigCSV(accName, acc.TestDatabaseName, acc.TestSchemaName, ";", "Terraform acceptance test"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_file_format.test", "name", accName),
resource.TestCheckResourceAttr("snowflake_file_format.test", "database", acc.TestDatabaseName),
Expand Down Expand Up @@ -50,6 +50,14 @@ func TestAcc_FileFormatCSV(t *testing.T) {
resource.TestCheckResourceAttr("snowflake_file_format.test", "comment", "Terraform acceptance test"),
),
},
// UPDATE
{
Config: fileFormatConfigCSV(accName, acc.TestDatabaseName, acc.TestSchemaName, ",", "Terraform acceptance test 2"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_file_format.test", "field_delimiter", ","),
resource.TestCheckResourceAttr("snowflake_file_format.test", "comment", "Terraform acceptance test 2"),
),
},
// IMPORT
{
ResourceName: "snowflake_file_format.test",
Expand Down Expand Up @@ -398,7 +406,7 @@ func TestAcc_FileFormat_issue1947(t *testing.T) {
})
}

func fileFormatConfigCSV(n string, databaseName string, schemaName string) string {
func fileFormatConfigCSV(n string, databaseName string, schemaName string, fieldDelimiter string, comment string) string {
return fmt.Sprintf(`
resource "snowflake_file_format" "test" {
name = "%v"
Expand All @@ -407,7 +415,7 @@ resource "snowflake_file_format" "test" {
format_type = "CSV"
compression = "GZIP"
record_delimiter = "\r"
field_delimiter = ";"
field_delimiter = "%s"
file_extension = ".ssv"
parse_header = true
skip_blank_lines = true
Expand All @@ -425,9 +433,9 @@ resource "snowflake_file_format" "test" {
empty_field_as_null = false
skip_byte_order_mark = false
encoding = "UTF-16"
comment = "Terraform acceptance test"
comment = "%s"
}
`, n, databaseName, schemaName)
`, n, databaseName, schemaName, fieldDelimiter, comment)
}

func fileFormatConfigJSON(n string, databaseName string, schemaName string) string {
Expand Down

0 comments on commit 018bb74

Please sign in to comment.