Skip to content

Commit

Permalink
Do not read query for dynamic table (#2329)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-asawicki committed Jan 26, 2024
1 parent b93f71b commit aa74b84
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 5 deletions.
5 changes: 0 additions & 5 deletions pkg/resources/dynamic_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
46 changes: 46 additions & 0 deletions pkg/resources/dynamic_table_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions pkg/resources/testdata/TestAcc_DynamicTable_issue2329/1/test.tf
Original file line number Diff line number Diff line change
@@ -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
}
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 aa74b84

Please sign in to comment.