From 368dc8fb3f7e5156d16caed1e03792654d49f3d4 Mon Sep 17 00:00:00 2001 From: Relativity74205 <34404734+Relativity74205@users.noreply.github.com> Date: Fri, 24 Feb 2023 00:27:50 +0100 Subject: [PATCH] fix: schema read now checks first if the corresponding database exists (#1568) Co-authored-by: Arkadius Schuchhardt --- pkg/resources/schema.go | 11 ++++++++- pkg/resources/schema_test.go | 46 +++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/pkg/resources/schema.go b/pkg/resources/schema.go index 53922c6c7f..17aaf6b34d 100644 --- a/pkg/resources/schema.go +++ b/pkg/resources/schema.go @@ -177,8 +177,17 @@ func ReadSchema(d *schema.ResourceData, meta interface{}) error { dbName := schemaID.DatabaseName schema := schemaID.SchemaName - q := snowflake.NewSchemaBuilder(schema).WithDB(dbName).Show() + // Checks if the corresponding database still exists; if not, than the schema also cannot exist + q := snowflake.NewDatabaseBuilder(dbName).Show() row := snowflake.QueryRow(db, q) + _, err = snowflake.ScanDatabase(row) + if errors.Is(err, sql.ErrNoRows) { + d.SetId("") + return nil + } + + q = snowflake.NewSchemaBuilder(schema).WithDB(dbName).Show() + row = snowflake.QueryRow(db, q) s, err := snowflake.ScanSchema(row) if errors.Is(err, sql.ErrNoRows) { diff --git a/pkg/resources/schema_test.go b/pkg/resources/schema_test.go index 981fe273e9..30395d8db6 100644 --- a/pkg/resources/schema_test.go +++ b/pkg/resources/schema_test.go @@ -57,6 +57,27 @@ func TestSchemaRead(t *testing.T) { 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()) + expectReadSchema(mock) + err := resources.ReadSchema(d, db) + r.NotEmpty(d.State()) + r.Nil(err) + }) +} + +func TestSchemaReadNotExists(t *testing.T) { + r := require.New(t) + + in := map[string]interface{}{ + "name": "good_name", + "database": "test_db", + } + + d := schema.TestResourceDataRaw(t, resources.Schema().Schema, in) + d.SetId("test_db|good_name") + + WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) { + // Test when schema resource is not found, checking if state will be empty + r.NotEmpty(d.State()) q := snowflake.NewSchemaBuilder("good_name").WithDB("test_db").Show() mock.ExpectQuery(q).WillReturnError(sql.ErrNoRows) err := resources.ReadSchema(d, db) @@ -65,10 +86,33 @@ func TestSchemaRead(t *testing.T) { }) } +func TestSchemaReadDatabaseDoesNotExist(t *testing.T) { + r := require.New(t) + + in := map[string]interface{}{ + "name": "good_name", + "database": "test_db", + } + + d := schema.TestResourceDataRaw(t, resources.Schema().Schema, in) + d.SetId("test_db|good_name") + + WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) { + // Test when database resource is not found, checking if state will be empty + r.NotEmpty(d.State()) + q := snowflake.NewDatabaseBuilder("test_db").Show() + mock.ExpectQuery(q).WillReturnError(sql.ErrNoRows) + err := resources.ReadSchema(d, db) + r.Empty(d.State()) + r.Nil(err) + }) +} + func expectReadSchema(mock sqlmock.Sqlmock) { rows := sqlmock.NewRows([]string{ "created_on", "name", "is_default", "is_current", "database_name", "owner", "comment", "options", "retention_time", }, ).AddRow("2019-05-19 16:55:36.530 -0700", "good_name", "N", "Y", "test_db", "admin", "great comment", "TRANSIENT, MANAGED ACCESS", 1) - mock.ExpectQuery(`^SHOW SCHEMAS LIKE 'good_name' IN DATABASE "test_db"$`).WillReturnRows(rows) + q := snowflake.NewSchemaBuilder("good_name").WithDB("test_db").Show() + mock.ExpectQuery(q).WillReturnRows(rows) }