From aa74b840572723e10973a7cc38bc01aa806f74fd Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Thu, 25 Jan 2024 14:02:25 +0100 Subject: [PATCH] Do not read query for dynamic table (#2329) --- pkg/resources/dynamic_table.go | 5 -- .../dynamic_table_acceptance_test.go | 46 +++++++++++++++++++ .../TestAcc_DynamicTable_issue2329/1/test.tf | 27 +++++++++++ .../1/variables.tf | 27 +++++++++++ 4 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 pkg/resources/testdata/TestAcc_DynamicTable_issue2329/1/test.tf create mode 100644 pkg/resources/testdata/TestAcc_DynamicTable_issue2329/1/variables.tf diff --git a/pkg/resources/dynamic_table.go b/pkg/resources/dynamic_table.go index 90cbdd1c22..820fad98ab 100644 --- a/pkg/resources/dynamic_table.go +++ b/pkg/resources/dynamic_table.go @@ -200,11 +200,6 @@ func ReadDynamicTable(d *schema.ResourceData, meta interface{}) error { return err } } - // trim up to " ..AS" and remove whitespace - query := strings.TrimSpace(text[strings.Index(text, "AS")+3:]) - if err := d.Set("query", query); err != nil { - return err - } if err := d.Set("cluster_by", dynamicTable.ClusterBy); err != nil { return err } diff --git a/pkg/resources/dynamic_table_acceptance_test.go b/pkg/resources/dynamic_table_acceptance_test.go index 6a59b1c37d..49e309954b 100644 --- a/pkg/resources/dynamic_table_acceptance_test.go +++ b/pkg/resources/dynamic_table_acceptance_test.go @@ -284,6 +284,52 @@ func TestAcc_DynamicTable_issue2276(t *testing.T) { }) } +// TestAcc_DynamicTable_issue2329 proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2329 issue. +func TestAcc_DynamicTable_issue2329(t *testing.T) { + dynamicTableName := strings.ToUpper(acctest.RandStringFromCharSet(4, acctest.CharSetAlpha) + "AS" + acctest.RandStringFromCharSet(4, acctest.CharSetAlpha)) + tableName := dynamicTableName + "_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(dynamicTableName), + "database": config.StringVariable(acc.TestDatabaseName), + "schema": config.StringVariable(acc.TestSchemaName), + "warehouse": config.StringVariable(acc.TestWarehouseName), + "query": config.StringVariable(query), + "comment": config.StringVariable("Comment with AS on purpose"), + "table_name": config.StringVariable(tableName), + } + } + + 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{ + { + ConfigDirectory: config.TestStepDirectory(), + ConfigVariables: m(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_dynamic_table.dt", "name", dynamicTableName), + resource.TestCheckResourceAttr("snowflake_dynamic_table.dt", "query", query), + ), + }, + // No changes are expected + { + ConfigDirectory: acc.ConfigurationSameAsStepN(1), + ConfigVariables: m(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_dynamic_table.dt", "name", dynamicTableName), + resource.TestCheckResourceAttr("snowflake_dynamic_table.dt", "query", query), + ), + }, + }, + }) +} + func testAccCheckDynamicTableDestroy(s *terraform.State) error { db := acc.TestAccProvider.Meta().(*sql.DB) client := sdk.NewClientFromDB(db) diff --git a/pkg/resources/testdata/TestAcc_DynamicTable_issue2329/1/test.tf b/pkg/resources/testdata/TestAcc_DynamicTable_issue2329/1/test.tf new file mode 100644 index 0000000000..7942078c37 --- /dev/null +++ b/pkg/resources/testdata/TestAcc_DynamicTable_issue2329/1/test.tf @@ -0,0 +1,27 @@ +resource "snowflake_table" "t" { + database = var.database + schema = var.schema + name = var.table_name + change_tracking = true + column { + name = "id" + type = "NUMBER(38,0)" + } + column { + name = "data" + type = "VARCHAR(16)" + } +} + +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 +} diff --git a/pkg/resources/testdata/TestAcc_DynamicTable_issue2329/1/variables.tf b/pkg/resources/testdata/TestAcc_DynamicTable_issue2329/1/variables.tf new file mode 100644 index 0000000000..5a6d6701d9 --- /dev/null +++ b/pkg/resources/testdata/TestAcc_DynamicTable_issue2329/1/variables.tf @@ -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 +}