Skip to content

Commit

Permalink
fix: schema read now checks first if the corresponding database exists (
Browse files Browse the repository at this point in the history
#1568)

Co-authored-by: Arkadius Schuchhardt <[email protected]>
  • Loading branch information
Relativity74205 and AS-auxmoney authored Feb 23, 2023
1 parent 174355d commit 368dc8f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
11 changes: 10 additions & 1 deletion pkg/resources/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
46 changes: 45 additions & 1 deletion pkg/resources/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

0 comments on commit 368dc8f

Please sign in to comment.