Skip to content

Commit

Permalink
fix: wildcards in database name (#1666)
Browse files Browse the repository at this point in the history
* wildcards in database name

* wildcards in database name

* wildcards in database name

* remove support for solaris

* remove support for solaris

* fix importer func
  • Loading branch information
sfc-gh-swinkler authored Mar 28, 2023
1 parent ea23020 commit 54bf74c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
1 change: 0 additions & 1 deletion .goreleaser.prerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ builds:
- env:
- CGO_ENABLED=0
goos:
- solaris
- windows
- linux
- darwin
Expand Down
1 change: 0 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ builds:
- env:
- CGO_ENABLED=0
goos:
- solaris
- windows
- linux
- darwin
Expand Down
1 change: 0 additions & 1 deletion pkg/datasources/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func Database() *schema.Resource {
func ReadDatabase(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
dbx := sqlx.NewDb(db, "snowflake")
log.Printf("[DEBUG] database: %v", d.Get("name"))
dbData, err := snowflake.ListDatabase(dbx, d.Get("name").(string))
if err != nil {
log.Println("[DEBUG] list database failed to decode")
Expand Down
28 changes: 13 additions & 15 deletions pkg/resources/database.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resources

import (
"context"
"database/sql"
"errors"
"fmt"
Expand All @@ -9,6 +10,7 @@ import (
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jmoiron/sqlx"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/snowflake"
)
Expand Down Expand Up @@ -93,7 +95,12 @@ func Database() *schema.Resource {

Schema: databaseSchema,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
StateContext: func(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
if err := d.Set("name", d.Id()); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
},
},
}
}
Expand Down Expand Up @@ -219,22 +226,13 @@ func createDatabaseFromReplica(d *schema.ResourceData, meta interface{}) error {

func ReadDatabase(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
name := d.Id()

stmt := snowflake.NewDatabaseBuilder(name).Show()
row := snowflake.QueryRow(db, stmt)

database, err := snowflake.ScanDatabase(row)
dbx := sqlx.NewDb(db, "snowflake")
database, err := snowflake.ListDatabase(dbx, d.Get("name").(string))
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
// If not found, mark resource to be removed from state file during apply or refresh
log.Printf("[DEBUG] database (%s) not found", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("unable to scan row for SHOW DATABASES")
log.Println("[DEBUG] list database failed to decode")
d.SetId("")
return nil
}

if err := d.Set("name", database.DBName.String); err != nil {
return err
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/resources/database_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAcc_DatabaseWithUnderscore(t *testing.T) {
if _, ok := os.LookupEnv("SKIP_DATABASE_TESTS"); ok {
t.Skip("Skipping TestAccDatabase")
}

prefix := "_" + strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
resource.ParallelTest(t, resource.TestCase{
Providers: providers(),
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: dbConfig(prefix),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_database.db", "name", prefix),
resource.TestCheckResourceAttr("snowflake_database.db", "comment", "test comment"),
resource.TestCheckResourceAttrSet("snowflake_database.db", "data_retention_time_in_days"),
),
},
},
})
}

func TestAcc_Database(t *testing.T) {
if _, ok := os.LookupEnv("SKIP_DATABASE_TESTS"); ok {
t.Skip("Skipping TestAccDatabase")
Expand Down

0 comments on commit 54bf74c

Please sign in to comment.