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 1 commit
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
5 changes: 0 additions & 5 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ builds:
- env:
- CGO_ENABLED=0
goos:
- openbsd
- solaris
- windows
- linux
Expand All @@ -17,10 +16,6 @@ builds:
ignore:
- goos: darwin
goarch: '386'
- goos: openbsd
goarch: arm
- goos: openbsd
goarch: arm64
binary: '{{ .ProjectName }}_v{{ .Version }}'

archives:
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
20 changes: 6 additions & 14 deletions pkg/resources/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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 @@ -219,22 +220,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,27 @@ 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 Expand Up @@ -78,3 +99,4 @@ resource "snowflake_database" "db" {
`
return fmt.Sprintf(s, prefix)
}

2 changes: 2 additions & 0 deletions pkg/snowflake/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,9 @@ func ListDatabase(sdb *sqlx.DB, databaseName string) (*Database, error) {
db := &Database{}
for _, d := range dbs {
d := d
log.Printf("[DEBUG] database: %v", d.DBName.String)
if d.DBName.String == databaseName {
log.Printf("[DEBUG] match database: %v with string %s", d.DBName.String,databaseName)
db = &d
break
}
Expand Down