Skip to content

Commit

Permalink
Fix 2134
Browse files Browse the repository at this point in the history
Fixes #2134
  • Loading branch information
sfc-gh-asawicki committed Nov 14, 2023
1 parent ca11e8f commit 91c3673
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 14 deletions.
18 changes: 12 additions & 6 deletions pkg/resources/dynamic_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ var dynamicTableShema = map[string]*schema.Schema{
ForceNew: true,
},
"query": {
Type: schema.TypeString,
Required: true,
Description: "Specifies the query to use to populate the dynamic table.",
Type: schema.TypeString,
Required: true,
Description: "Specifies the query to use to populate the dynamic table.",
DiffSuppressFunc: DiffSuppressStatement,
},
"comment": {
Type: schema.TypeString,
Expand Down Expand Up @@ -298,20 +299,25 @@ func UpdateDynamicTable(d *schema.ResourceData, meta interface{}) error {
id := helpers.DecodeSnowflakeID(d.Id()).(sdk.SchemaObjectIdentifier)
request := sdk.NewAlterDynamicTableRequest(id)

runSet := false
set := sdk.NewDynamicTableSetRequest()
if d.HasChange("target_lag") {
tl := parseTargetLag(d.Get("target_lag"))
set.WithTargetLag(tl)
runSet = true
}

if d.HasChange("warehouse") {
warehouseName := d.Get("warehouse").(string)
set.WithWarehouse(sdk.NewAccountObjectIdentifier(warehouseName))
runSet = true
}

request.WithSet(set)
if err := client.DynamicTables.Alter(ctx, request); err != nil {
return err
if runSet {
request.WithSet(set)
if err := client.DynamicTables.Alter(ctx, request); err != nil {
return err
}
}

if d.HasChange("comment") {
Expand Down
79 changes: 71 additions & 8 deletions pkg/resources/dynamic_table_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@ func TestAcc_DynamicTable_issue2173(t *testing.T) {
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|SFVNXKJFAA"
* name = "SFVNXKJFAA"
* ~ schema = "MEYIYWUGGO" -> "terraform_test_schema"
* # (14 unchanged attributes hidden)
* }
* # 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|SFVNXKJFAA"
* name = "SFVNXKJFAA"
* ~ schema = "MEYIYWUGGO" -> "terraform_test_schema"
* # (14 unchanged attributes hidden)
* }
* which matches the issue description exactly (issue mentioned also query being changed but here for simplicity the same underlying table and query were used).
*/
PreApply: []plancheck.PlanCheck{plancheck.ExpectEmptyPlan()},
Expand All @@ -173,6 +173,69 @@ func TestAcc_DynamicTable_issue2173(t *testing.T) {
})
}

// TestAcc_DynamicTable_issue2134 proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2134 issue.
func TestAcc_DynamicTable_issue2134(t *testing.T) {
dynamicTableName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
tableName := dynamicTableName + "_table"
// whitespace (initial tab) is added on purpose here
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(dynamicTableName),
"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 2134"),
"table_name": config.StringVariable(tableName),
}
}
m2 := m()
m2["comment"] = config.StringVariable("Changed comment")

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: testAccCheckDynamicTableDestroy,
Steps: []resource.TestStep{
/*
* Before the fix the first step resulted in not empty plan (as expected)
* # snowflake_dynamic_table.dt will be updated in-place
* ~ resource "snowflake_dynamic_table" "dt" {
* id = "terraform_test_database|terraform_test_schema|IKLBYWKSOV"
* name = "IKLBYWKSOV"
* ~ query = "select \"id\" from \"terraform_test_database\".\"terraform_test_schema\".\"IKLBYWKSOV_table\"" -> "\tselect \"id\" from \"terraform_test_database\".\"terraform_test_schema\".\"IKLBYWKSOV_table\""
* # (15 unchanged attributes hidden)
* }
* which matches the issue description exactly.
*/
{
ConfigDirectory: config.TestStepDirectory(),
ConfigVariables: m(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_dynamic_table.dt", "name", dynamicTableName),
),
},
/*
* Before the fix the second step resulted in SQL error (as expected)
* Error: 001003 (42000): SQL compilation error:
* syntax error line 1 at position 86 unexpected '<EOF>'.
* which matches the issue description exactly.
*/
{
ConfigDirectory: acc.ConfigurationSameAsStepN(1),
ConfigVariables: m2,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_dynamic_table.dt", "name", dynamicTableName),
),
},
},
})
}

func testAccCheckDynamicTableDestroy(s *terraform.State) error {
db := acc.TestAccProvider.Meta().(*sql.DB)
client := sdk.NewClientFromDB(db)
Expand Down
23 changes: 23 additions & 0 deletions pkg/resources/testdata/TestAcc_DynamicTable_issue2134/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
}

0 comments on commit 91c3673

Please sign in to comment.