Skip to content

Commit

Permalink
Fix show by id for dynamic table
Browse files Browse the repository at this point in the history
Fixes #2173
  • Loading branch information
sfc-gh-asawicki committed Nov 14, 2023
1 parent 0f45fd5 commit ca91c38
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 1 deletion.
112 changes: 112 additions & 0 deletions pkg/resources/dynamic_table_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"strings"
"testing"

Expand Down Expand Up @@ -99,6 +100,71 @@ func TestAcc_DynamicTable_basic(t *testing.T) {
})
}

// TestAcc_DynamicTable_issue2173 proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2173 issue.
func TestAcc_DynamicTable_issue2173(t *testing.T) {
resourceName := "snowflake_dynamic_table.dt"
name := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
tableName := name + "_table"
query := fmt.Sprintf(`select "id" from "%v"."%v"."%v"`, acc.TestDatabaseName, acc.TestSchemaName, tableName)
m := func() map[string]config.Variable {
return map[string]config.Variable{
"name": config.StringVariable(name),
"database": config.StringVariable(acc.TestDatabaseName),
"schema": config.StringVariable(acc.TestSchemaName),
"warehouse": config.StringVariable(acc.TestWarehouseName),
"query": config.StringVariable(query),
"comment": config.StringVariable("Terraform acceptance test for GH issue 2173"),
"table_name": config.StringVariable(tableName),
}
}
otherSchema := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() {
acc.TestAccPreCheck(t)
createDynamicTableOutsideTerraform(t, otherSchema, name)
},
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: dropAdditionalSchemaAndCheckDynamicTableDestroy(t, otherSchema),
Steps: []resource.TestStep{
{
ConfigDirectory: config.TestStepDirectory(),
ConfigVariables: m(),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{plancheck.ExpectNonEmptyPlan()},
},
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", name),
),
},
{
ConfigDirectory: config.TestStepDirectory(),
ConfigVariables: m(),
ConfigPlanChecks: resource.ConfigPlanChecks{
/*
* Before the fix this step resulted in
* # snowflake_dynamic_table.dt will be updated in-place
* ~ resource "snowflake_dynamic_table" "dt" {
* + comment = "Terraform acceptance test for GH issue 2173"
* id = "terraform_test_database|terraform_test_schema|JJIVTACVOU"
* name = "JJIVTACVOU"
* ~ query = "select id from \"terraform_test_database\".\"WFAUDTBOJW\".\"JJIVTACVOU_table\"" -> "select \"id\" from \"terraform_test_database\".\"terraform_test_schema\".\"JJIVTACVOU_table\""
* ~ schema = "WFAUDTBOJW" -> "terraform_test_schema"
* # (13 unchanged attributes hidden) *
* # (1 unchanged block hidden)
* }
* which matches the issue description exactly.
*/
PreApply: []plancheck.PlanCheck{plancheck.ExpectEmptyPlan()},
},
},
},
})
}

func testAccCheckDynamicTableDestroy(s *terraform.State) error {
db := acc.TestAccProvider.Meta().(*sql.DB)
client := sdk.NewClientFromDB(db)
Expand All @@ -115,3 +181,49 @@ func testAccCheckDynamicTableDestroy(s *terraform.State) error {
}
return nil
}

func createDynamicTableOutsideTerraform(t *testing.T, schemaName string, dynamicTableName string) {
t.Helper()
client, err := sdk.NewDefaultClient()
if err != nil {
t.Fatal(err)
}
ctx := context.Background()

schemaId := sdk.NewDatabaseObjectIdentifier(acc.TestDatabaseName, schemaName)
if err := client.Schemas.Create(ctx, schemaId, &sdk.CreateSchemaOptions{
IfNotExists: sdk.Bool(true),
}); err != nil {
t.Fatal(fmt.Errorf("error creating schema: %w", err))
}

tableName := dynamicTableName + "_table"
tableId := sdk.NewSchemaObjectIdentifier(acc.TestDatabaseName, schemaName, tableName)

_, err = client.ExecForTests(ctx, fmt.Sprintf("CREATE TABLE %s (id NUMBER)", tableId.FullyQualifiedName()))
if err != nil {
t.Fatal(fmt.Errorf("error creating table: %w", err))
}

query := fmt.Sprintf(`select id from "%v"."%v"."%v"`, acc.TestDatabaseName, schemaName, tableName)
dynamicTableId := sdk.NewSchemaObjectIdentifier(acc.TestDatabaseName, schemaName, dynamicTableName)
if err := client.DynamicTables.Create(ctx, sdk.NewCreateDynamicTableRequest(dynamicTableId, sdk.NewAccountObjectIdentifier(acc.TestWarehouseName), sdk.TargetLag{MaximumDuration: sdk.String("2 minutes")}, query)); err != nil {
t.Fatal(fmt.Errorf("error creating dynamic table: %w", err))
}
}

func dropAdditionalSchemaAndCheckDynamicTableDestroy(t *testing.T, schemaName string) resource.TestCheckFunc {
t.Helper()
return func(s *terraform.State) error {
client, err := sdk.NewDefaultClient()
if err != nil {
t.Fatal(err)
}
ctx := context.Background()

schemaId := sdk.NewDatabaseObjectIdentifier(acc.TestDatabaseName, schemaName)
_ = client.Schemas.Drop(ctx, schemaId, nil)

return testAccCheckDynamicTableDestroy(s)
}
}
23 changes: 23 additions & 0 deletions pkg/resources/testdata/TestAcc_DynamicTable_issue2173/1/test.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
resource "snowflake_table" "t" {
database = var.database
schema = var.schema
name = var.table_name
change_tracking = true
column {
name = "id"
type = "NUMBER(38,0)"
}
}

resource "snowflake_dynamic_table" "dt" {
depends_on = [snowflake_table.t]
name = var.name
database = var.database
schema = var.schema
target_lag {
maximum_duration = "2 minutes"
}
warehouse = var.warehouse
query = var.query
comment = var.comment
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
variable "name" {
type = string
}

variable "database" {
type = string
}

variable "schema" {
type = string
}

variable "warehouse" {
type = string
}

variable "query" {
type = string
}

variable "comment" {
type = string
}

variable "table_name" {
type = string
}
23 changes: 23 additions & 0 deletions pkg/resources/testdata/TestAcc_DynamicTable_issue2173/2/test.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
resource "snowflake_table" "t" {
database = var.database
schema = var.schema
name = var.table_name
change_tracking = true
column {
name = "id"
type = "NUMBER(38,0)"
}
}

resource "snowflake_dynamic_table" "dt" {
depends_on = [snowflake_table.t]
name = var.name
database = var.database
schema = var.schema
target_lag {
maximum_duration = "2 minutes"
}
warehouse = var.warehouse
query = var.query
comment = var.comment
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
variable "name" {
type = string
}

variable "database" {
type = string
}

variable "schema" {
type = string
}

variable "warehouse" {
type = string
}

variable "query" {
type = string
}

variable "comment" {
type = string
}

variable "table_name" {
type = string
}
2 changes: 1 addition & 1 deletion pkg/sdk/dynamic_table_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (v *dynamicTables) Show(ctx context.Context, request *ShowDynamicTableReque
}

func (v *dynamicTables) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*DynamicTable, error) {
request := NewShowDynamicTableRequest().WithLike(&Like{Pattern: String(id.Name())})
request := NewShowDynamicTableRequest().WithIn(&In{Schema: NewDatabaseObjectIdentifier(id.DatabaseName(), id.SchemaName())}).WithLike(&Like{String(id.Name())})
dynamicTables, err := v.Show(ctx, request)
if err != nil {
return nil, err
Expand Down
7 changes: 7 additions & 0 deletions pkg/sdk/testint/dynamic_table_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ func TestInt_DynamicTableCreateAndDrop(t *testing.T) {
require.Equal(t, name.Name(), entity.Name)
require.Equal(t, testWarehouse(t).ID().Name(), entity.Warehouse)
require.Equal(t, *targetLag.MaximumDuration, entity.TargetLag)

dynamicTableById, err := client.DynamicTables.ShowByID(ctx, name)
require.NoError(t, err)
require.NotNil(t, dynamicTableById)
require.Equal(t, name.Name(), dynamicTableById.Name)
require.Equal(t, testWarehouse(t).ID().Name(), dynamicTableById.Warehouse)
require.Equal(t, *targetLag.MaximumDuration, dynamicTableById.TargetLag)
})

t.Run("test complete with target lag", func(t *testing.T) {
Expand Down

0 comments on commit ca91c38

Please sign in to comment.