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

feat: Use tables from SDK #2453

Merged
merged 14 commits into from
Feb 2, 2024
2 changes: 1 addition & 1 deletion docs/resources/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Optional:
- `comment` (String) Column comment
- `default` (Block List, Max: 1) Defines the column default value; note due to limitations of Snowflake's ALTER TABLE ADD/MODIFY COLUMN updates to default will not be applied (see [below for nested schema](#nestedblock--column--default))
- `identity` (Block List, Max: 1) Defines the identity start/step values for a column. **Note** Identity/default are mutually exclusive. (see [below for nested schema](#nestedblock--column--identity))
- `masking_policy` (String) Masking policy to apply on column
- `masking_policy` (String) Masking policy to apply on column. It has to be a fully qualified name.
- `nullable` (Boolean) Whether this column can contain null values. **Note**: Depending on your Snowflake version, the default value will not suffice if this column is used in a primary key constraint.

<a id="nestedblock--column--default"></a>
Expand Down
44 changes: 22 additions & 22 deletions pkg/datasources/tables.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package datasources

import (
"context"
"database/sql"
"errors"
"fmt"
"log"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/snowflake"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -58,38 +58,38 @@ func Tables() *schema.Resource {

func ReadTables(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
ctx := context.Background()
client := sdk.NewClientFromDB(db)
databaseName := d.Get("database").(string)
schemaName := d.Get("schema").(string)

currentTables, err := snowflake.ListTables(databaseName, schemaName, db)
if errors.Is(err, sql.ErrNoRows) {
// If not found, mark resource to be removed from state file during apply or refresh
log.Printf("[DEBUG] tables in schema (%s) not found", d.Id())
d.SetId("")
return nil
} else if err != nil {
log.Printf("[DEBUG] unable to parse tables in schema (%s)", d.Id())
schemaId := sdk.NewDatabaseObjectIdentifier(databaseName, schemaName)
extractedTables, err := client.Tables.Show(ctx, sdk.NewShowTableRequest().WithIn(
&sdk.In{Schema: schemaId},
))
if err != nil {
log.Printf("[DEBUG] failed when searching tables in schema (%s), err = %s", schemaId.FullyQualifiedName(), err.Error())
d.SetId("")
return nil
}

tables := []map[string]interface{}{}
tables := make([]map[string]any, 0)

for _, table := range currentTables {
tableMap := map[string]interface{}{}

if table.IsExternal.String == "Y" {
for _, extractedTable := range extractedTables {
if extractedTable.IsExternal {
continue
}

tableMap["name"] = table.TableName.String
tableMap["database"] = table.DatabaseName.String
tableMap["schema"] = table.SchemaName.String
tableMap["comment"] = table.Comment.String
table := map[string]any{
"name": extractedTable.Name,
"database": extractedTable.DatabaseName,
"schema": extractedTable.SchemaName,
"comment": extractedTable.Comment,
}

tables = append(tables, tableMap)
tables = append(tables, table)
}

d.SetId(fmt.Sprintf(`%v|%v`, databaseName, schemaName))
d.SetId(helpers.EncodeSnowflakeID(databaseName, schemaName))
return d.Set("tables", tables)
}
9 changes: 8 additions & 1 deletion pkg/datasources/tables_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"strings"
"testing"

acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func TestAcc_Tables(t *testing.T) {
Expand All @@ -16,7 +19,11 @@ func TestAcc_Tables(t *testing.T) {
stageName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
externalTableName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
resource.ParallelTest(t, resource.TestCase{
Providers: providers(),
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Expand Down
6 changes: 3 additions & 3 deletions pkg/resources/materialized_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,13 @@ func UpdateMaterializedView(d *schema.ResourceData, meta interface{}) error {
unsetRequest := sdk.NewMaterializedViewUnsetRequest()

if d.HasChange("comment") {
comment := d.Get("comment")
if c := comment.(string); c == "" {
comment := d.Get("comment").(string)
if comment == "" {
runUnsetStatement = true
unsetRequest.WithComment(sdk.Bool(true))
} else {
runSetStatement = true
setRequest.WithComment(sdk.String(d.Get("comment").(string)))
setRequest.WithComment(sdk.String(comment))
}
}
if d.HasChange("is_secure") {
Expand Down
8 changes: 2 additions & 6 deletions pkg/resources/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/snowflake"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -131,14 +129,12 @@ func CreateStream(d *schema.ResourceData, meta interface{}) error {
}
tableId := tableObjectIdentifier.(sdk.SchemaObjectIdentifier)

tq := snowflake.NewTableBuilder(tableId.Name(), tableId.DatabaseName(), tableId.SchemaName()).Show()
tableRow := snowflake.QueryRow(db, tq)
t, err := snowflake.ScanTable(tableRow)
table, err := client.Tables.ShowByID(ctx, tableId)
if err != nil {
return err
}

if t.IsExternal.String == "Y" {
if table.IsExternal {
req := sdk.NewCreateStreamOnExternalTableRequest(id, tableId)
if insertOnly {
req.WithInsertOnly(sdk.Bool(true))
Expand Down
Loading
Loading