Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wildcards in database name #1666

Merged
merged 8 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"),
),
},
},
})
}
sfc-gh-swinkler marked this conversation as resolved.
Show resolved Hide resolved

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