Skip to content

Commit

Permalink
fix: stop file format failure when does not exist (#1399)
Browse files Browse the repository at this point in the history
* fix: stop file format failure when does not exist

* Update team-jira-issues.yml

* Update team-jira-issues.yml

* fix: file format plan refresh fails after manual deletion #1395

Co-authored-by: John Love <[email protected]>
  • Loading branch information
WobblyRobbly and sfc-gh-jlove authored Dec 7, 2022
1 parent 7a8cf75 commit 3611ff5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/resources/file_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"encoding/csv"
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -536,6 +537,12 @@ func ReadFileFormat(d *schema.ResourceData, meta interface{}) error {
row := snowflake.QueryRow(db, ff)

f, err := snowflake.ScanFileFormatShow(row)
if err == sql.ErrNoRows {
// If not found, mark resource to be removed from statefile during apply or refresh
log.Printf("[DEBUG] file_format (%s) not found", d.Id())
d.SetId("")
return nil
}
if err != nil {
return err
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/resources/file_format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sqlmock "github.com/DATA-DOG/go-sqlmock"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/snowflake"
. "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/testhelpers"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -71,3 +72,30 @@ func expectReadFileFormat(mock sqlmock.Sqlmock) {
).AddRow("2019-12-23 17:20:50.088 +0000", "test_file_format", "test_db", "test_schema", "CSV", "test", "great comment", `{"TYPE":"CSV","RECORD_DELIMITER":"\n","FIELD_DELIMITER":",","FILE_EXTENSION":null,"SKIP_HEADER":0,"DATE_FORMAT":"AUTO","TIME_FORMAT":"AUTO","TIMESTAMP_FORMAT":"AUTO","BINARY_FORMAT":"HEX","ESCAPE":"NONE","ESCAPE_UNENCLOSED_FIELD":"\\","TRIM_SPACE":false,"FIELD_OPTIONALLY_ENCLOSED_BY":"NONE","NULL_IF":["\\N"],"COMPRESSION":"AUTO","ERROR_ON_COLUMN_COUNT_MISMATCH":false,"FIELD_DELIMETER":",","SKIP_BLANK_LINES":false,"REPLACE_INVALID_CHARACTERS":false,"EMPTY_FIELD_AS_NULL":false,"SKIP_BYTE_ORDER_MARK":false,"ENCODING":"UTF8"}`)
mock.ExpectQuery(`^SHOW FILE FORMATS LIKE 'test_file_format' IN SCHEMA "test_db"."test_schema"$`).WillReturnRows(rows)
}


func TestFileFormatWhenMissing(t *testing.T) {
r := require.New(t)

in := map[string]interface{}{
"name": "test_file_format",
"database": "test_db",
"schema": "test_schema",
"format_type": "CSV",
"null_if": []interface{}{"NULL"},
"error_on_column_count_mismatch": true,
"comment": "great comment",
}
d := schema.TestResourceDataRaw(t, resources.FileFormat().Schema, in)
d.SetId("test_db|test_schema|test_file_format")

WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
// Test when resource is not found, checking if state will be empty
r.NotEmpty(d.State())
q := snowflake.FileFormat("test_file_format", "test_db", "test_schema").Show()
mock.ExpectQuery(q).WillReturnError(sql.ErrNoRows)
err := resources.ReadFileFormat(d, db)
r.Empty(d.State())
r.Nil(err)
})
}

0 comments on commit 3611ff5

Please sign in to comment.