diff --git a/.goreleaser.prerelease.yml b/.goreleaser.prerelease.yml index 59b9d9a348..39e968894b 100644 --- a/.goreleaser.prerelease.yml +++ b/.goreleaser.prerelease.yml @@ -2,7 +2,6 @@ builds: - env: - CGO_ENABLED=0 goos: - - solaris - windows - linux - darwin diff --git a/.goreleaser.yml b/.goreleaser.yml index a08cc17933..d03a7f0aec 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -2,7 +2,6 @@ builds: - env: - CGO_ENABLED=0 goos: - - solaris - windows - linux - darwin diff --git a/pkg/datasources/database.go b/pkg/datasources/database.go index 8ce0f05002..9a2915fd49 100644 --- a/pkg/datasources/database.go +++ b/pkg/datasources/database.go @@ -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") diff --git a/pkg/resources/database.go b/pkg/resources/database.go index ac27cd8f83..a8b6bb829a 100644 --- a/pkg/resources/database.go +++ b/pkg/resources/database.go @@ -1,6 +1,7 @@ package resources import ( + "context" "database/sql" "errors" "fmt" @@ -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" ) @@ -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 + }, }, } } @@ -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 } diff --git a/pkg/resources/database_acceptance_test.go b/pkg/resources/database_acceptance_test.go index ea5351b876..6098ac25ca 100644 --- a/pkg/resources/database_acceptance_test.go +++ b/pkg/resources/database_acceptance_test.go @@ -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")