From 412ac5ff4141d16aea3fc2269b16b6a87534c1ad Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Mon, 10 Oct 2022 06:52:20 -0700 Subject: [PATCH 1/8] integer return type for procedure --- pkg/resources/procedure.go | 24 +++++++++++-------- pkg/resources/procedure_acceptance_test.go | 27 +++++++++++++++++++++- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/pkg/resources/procedure.go b/pkg/resources/procedure.go index 82dd979829..70af1ce830 100644 --- a/pkg/resources/procedure.go +++ b/pkg/resources/procedure.go @@ -4,13 +4,13 @@ import ( "database/sql" "fmt" "log" - "regexp" "strings" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/snowflake" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/pkg/errors" + "golang.org/x/exp/slices" ) var procedureLanguages = []string{"javascript", "java", "scala", "SQL"} @@ -76,10 +76,19 @@ var procedureSchema = map[string]*schema.Schema{ Type: schema.TypeString, Optional: true, Default: "SQL", - // Suppress the diff shown if the values are equal when both compared in lower case. - DiffSuppressFunc: DiffTypes, - ValidateFunc: validation.StringInSlice(procedureLanguages, true), - Description: "Specifies the language of the stored procedure code.", + DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { + if strings.EqualFold(old, new) { + return true + } + integerTypes := []string{"INT", "INTEGER", "BIGINT", "SMALLINT", "TINYINT", "BYTEINT", "NUMBER(38,0)"} + // all these types are equivalent https://docs.snowflake.com/en/sql-reference/data-types-numeric.html#int-integer-bigint-smallint-tinyint-byteint + if slices.Contains(integerTypes, strings.ToUpper(old)) && slices.Contains(integerTypes, strings.ToUpper(new)) { + return true + } + return false + }, + ValidateFunc: validation.StringInSlice(procedureLanguages, true), + Description: "Specifies the language of the stored procedure code.", }, "execute_as": { Type: schema.TypeString, @@ -268,10 +277,7 @@ func ReadProcedure(d *schema.ResourceData, meta interface{}) error { return err } case "returns": - // Format in Snowflake DB is RETURN_TYPE() or RETURN_TYPE - re := regexp.MustCompile(`^([A-Z0-9_]+)\s?(\([0-9]*\))?$`) - match := re.FindStringSubmatch(desc.Value.String) - if err = d.Set("return_type", match[1]); err != nil { + if err = d.Set("return_type", desc.Value.String); err != nil { return err } case "language": diff --git a/pkg/resources/procedure_acceptance_test.go b/pkg/resources/procedure_acceptance_test.go index 4675b89731..ff40da912e 100644 --- a/pkg/resources/procedure_acceptance_test.go +++ b/pkg/resources/procedure_acceptance_test.go @@ -50,6 +50,9 @@ func TestAcc_Procedure(t *testing.T) { resource.TestCheckResourceAttr("snowflake_procedure.test_proc_complex", "arguments.1.type", "DATE"), resource.TestCheckResourceAttr("snowflake_procedure.test_proc_complex", "return_behavior", "IMMUTABLE"), resource.TestCheckResourceAttr("snowflake_procedure.test_proc_complex", "null_input_behavior", "RETURNS NULL ON NULL INPUT"), + + resource.TestCheckResourceAttr("snowflake_procedure.test_proc_sql", "name", procName), + resource.TestCheckResourceAttr("snowflake_procedure.test_proc_sql", "return_type", "INTEGER"), ), }, { @@ -125,5 +128,27 @@ func procedureConfig(db, schema, name string) string { return X EOF } - `, db, schema, name, name, name) + + resource "snowflake_procedure" "test_proc_sql" { + name = "%s" + database = snowflake_database.test_database.name + schema = snowflake_schema.test_schema.name + language = "SQL" + return_type = "INTEGER" + execute_as = "CALLER" + return_behavior = "IMMUTABLE" + null_input_behavior = "RETURNS NULL ON NULL INPUT" + statement = < Date: Mon, 10 Oct 2022 07:02:44 -0700 Subject: [PATCH 2/8] integer return type for procedure --- pkg/resources/procedure_acceptance_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/resources/procedure_acceptance_test.go b/pkg/resources/procedure_acceptance_test.go index ff40da912e..3a9932bf9c 100644 --- a/pkg/resources/procedure_acceptance_test.go +++ b/pkg/resources/procedure_acceptance_test.go @@ -50,7 +50,7 @@ func TestAcc_Procedure(t *testing.T) { resource.TestCheckResourceAttr("snowflake_procedure.test_proc_complex", "arguments.1.type", "DATE"), resource.TestCheckResourceAttr("snowflake_procedure.test_proc_complex", "return_behavior", "IMMUTABLE"), resource.TestCheckResourceAttr("snowflake_procedure.test_proc_complex", "null_input_behavior", "RETURNS NULL ON NULL INPUT"), - + resource.TestCheckResourceAttr("snowflake_procedure.test_proc_sql", "name", procName), resource.TestCheckResourceAttr("snowflake_procedure.test_proc_sql", "return_type", "INTEGER"), ), @@ -149,6 +149,5 @@ func procedureConfig(db, schema, name string) string { end; EOT } - `, db, schema, name, name, name,name) + `, db, schema, name, name, name, name) } - From 5e565fe2b69ad5390f08cadd88f6538ad0145289 Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Mon, 10 Oct 2022 07:41:49 -0700 Subject: [PATCH 3/8] integer return type for procedure --- pkg/resources/procedure.go | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/pkg/resources/procedure.go b/pkg/resources/procedure.go index 70af1ce830..7ddd6c87f0 100644 --- a/pkg/resources/procedure.go +++ b/pkg/resources/procedure.go @@ -41,14 +41,18 @@ var procedureSchema = map[string]*schema.Schema{ Type: schema.TypeString, Required: true, // Suppress the diff shown if the values are equal when both compared in lower case. - DiffSuppressFunc: DiffTypes, + DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { + return strings.EqualFold(old, new) + }, Description: "The argument name", }, "type": { Type: schema.TypeString, Required: true, // Suppress the diff shown if the values are equal when both compared in lower case. - DiffSuppressFunc: DiffTypes, + DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { + return strings.EqualFold(old, new) + }, Description: "The argument type", }, }, @@ -61,7 +65,23 @@ var procedureSchema = map[string]*schema.Schema{ Type: schema.TypeString, Description: "The return type of the procedure", // Suppress the diff shown if the values are equal when both compared in lower case. - DiffSuppressFunc: DiffTypes, + DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { + if strings.EqualFold(old, new) { + return true + } + + varcharType := []string{"VARCHAR(16777216)", "VARCHAR", "text", "string", "NVARCHAR", "NVARCHAR2", "CHAR VARYING", "NCHAR VARYING"} + if slices.Contains(varcharType, strings.ToUpper(old)) && slices.Contains(varcharType, strings.ToUpper(new)){ + return true + } + + // all these types are equivalent https://docs.snowflake.com/en/sql-reference/data-types-numeric.html#int-integer-bigint-smallint-tinyint-byteint + integerTypes := []string{"INT", "INTEGER", "BIGINT", "SMALLINT", "TINYINT", "BYTEINT", "NUMBER(38,0)"} + if slices.Contains(integerTypes, strings.ToUpper(old)) && slices.Contains(integerTypes, strings.ToUpper(new)) { + return true + } + return false + }, Required: true, ForceNew: true, }, @@ -77,15 +97,7 @@ var procedureSchema = map[string]*schema.Schema{ Optional: true, Default: "SQL", DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { - if strings.EqualFold(old, new) { - return true - } - integerTypes := []string{"INT", "INTEGER", "BIGINT", "SMALLINT", "TINYINT", "BYTEINT", "NUMBER(38,0)"} - // all these types are equivalent https://docs.snowflake.com/en/sql-reference/data-types-numeric.html#int-integer-bigint-smallint-tinyint-byteint - if slices.Contains(integerTypes, strings.ToUpper(old)) && slices.Contains(integerTypes, strings.ToUpper(new)) { - return true - } - return false + return strings.EqualFold(old, new) }, ValidateFunc: validation.StringInSlice(procedureLanguages, true), Description: "Specifies the language of the stored procedure code.", From b59b20e675e6b935446ca2e0d93c682d341cfd5d Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Mon, 10 Oct 2022 07:53:37 -0700 Subject: [PATCH 4/8] integer return type for procedure --- pkg/resources/procedure.go | 10 +++++----- pkg/resources/procedure_acceptance_test.go | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/resources/procedure.go b/pkg/resources/procedure.go index 7ddd6c87f0..2a1da5441e 100644 --- a/pkg/resources/procedure.go +++ b/pkg/resources/procedure.go @@ -44,7 +44,7 @@ var procedureSchema = map[string]*schema.Schema{ DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { return strings.EqualFold(old, new) }, - Description: "The argument name", + Description: "The argument name", }, "type": { Type: schema.TypeString, @@ -53,7 +53,7 @@ var procedureSchema = map[string]*schema.Schema{ DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { return strings.EqualFold(old, new) }, - Description: "The argument type", + Description: "The argument type", }, }, }, @@ -71,7 +71,7 @@ var procedureSchema = map[string]*schema.Schema{ } varcharType := []string{"VARCHAR(16777216)", "VARCHAR", "text", "string", "NVARCHAR", "NVARCHAR2", "CHAR VARYING", "NCHAR VARYING"} - if slices.Contains(varcharType, strings.ToUpper(old)) && slices.Contains(varcharType, strings.ToUpper(new)){ + if slices.Contains(varcharType, strings.ToUpper(old)) && slices.Contains(varcharType, strings.ToUpper(new)) { return true } @@ -82,8 +82,8 @@ var procedureSchema = map[string]*schema.Schema{ } return false }, - Required: true, - ForceNew: true, + Required: true, + ForceNew: true, }, "statement": { Type: schema.TypeString, diff --git a/pkg/resources/procedure_acceptance_test.go b/pkg/resources/procedure_acceptance_test.go index 3a9932bf9c..d77436bfb5 100644 --- a/pkg/resources/procedure_acceptance_test.go +++ b/pkg/resources/procedure_acceptance_test.go @@ -52,7 +52,7 @@ func TestAcc_Procedure(t *testing.T) { resource.TestCheckResourceAttr("snowflake_procedure.test_proc_complex", "null_input_behavior", "RETURNS NULL ON NULL INPUT"), resource.TestCheckResourceAttr("snowflake_procedure.test_proc_sql", "name", procName), - resource.TestCheckResourceAttr("snowflake_procedure.test_proc_sql", "return_type", "INTEGER"), + resource.TestCheckResourceAttr("snowflake_procedure.test_proc_sql", "return_type", "NUMBER(38,0)"), ), }, { From c3cbc82be91ef5bf0c9c2c5b8b4b1fc33e178764 Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Mon, 10 Oct 2022 08:22:22 -0700 Subject: [PATCH 5/8] integer return type for procedure --- pkg/resources/procedure_acceptance_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/resources/procedure_acceptance_test.go b/pkg/resources/procedure_acceptance_test.go index d77436bfb5..4d40e3b133 100644 --- a/pkg/resources/procedure_acceptance_test.go +++ b/pkg/resources/procedure_acceptance_test.go @@ -52,7 +52,6 @@ func TestAcc_Procedure(t *testing.T) { resource.TestCheckResourceAttr("snowflake_procedure.test_proc_complex", "null_input_behavior", "RETURNS NULL ON NULL INPUT"), resource.TestCheckResourceAttr("snowflake_procedure.test_proc_sql", "name", procName), - resource.TestCheckResourceAttr("snowflake_procedure.test_proc_sql", "return_type", "NUMBER(38,0)"), ), }, { From e8d7a29eb71dc4e49ff9d5579c796011a7e553d0 Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Tue, 11 Oct 2022 03:03:33 -0700 Subject: [PATCH 6/8] update test --- pkg/resources/procedure_acceptance_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/resources/procedure_acceptance_test.go b/pkg/resources/procedure_acceptance_test.go index 4d40e3b133..fea54ce661 100644 --- a/pkg/resources/procedure_acceptance_test.go +++ b/pkg/resources/procedure_acceptance_test.go @@ -51,7 +51,7 @@ func TestAcc_Procedure(t *testing.T) { resource.TestCheckResourceAttr("snowflake_procedure.test_proc_complex", "return_behavior", "IMMUTABLE"), resource.TestCheckResourceAttr("snowflake_procedure.test_proc_complex", "null_input_behavior", "RETURNS NULL ON NULL INPUT"), - resource.TestCheckResourceAttr("snowflake_procedure.test_proc_sql", "name", procName), + resource.TestCheckResourceAttr("snowflake_procedure.test_proc_sql", "name", procName+"_sql"), ), }, { @@ -129,7 +129,7 @@ func procedureConfig(db, schema, name string) string { } resource "snowflake_procedure" "test_proc_sql" { - name = "%s" + name = "%s_sql" database = snowflake_database.test_database.name schema = snowflake_schema.test_schema.name language = "SQL" From 07ccbf6d774d78ff0993cc90c58119caba68bd56 Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Tue, 11 Oct 2022 03:13:11 -0700 Subject: [PATCH 7/8] update test --- pkg/resources/procedure_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/resources/procedure_test.go b/pkg/resources/procedure_test.go index f13f87c1c9..2190ae2b02 100644 --- a/pkg/resources/procedure_test.go +++ b/pkg/resources/procedure_test.go @@ -49,7 +49,6 @@ func TestProcedureCreate(t *testing.T) { err := resources.CreateProcedure(d, db) r.NoError(err) r.Equal("MY_PROC", d.Get("name").(string)) - r.Equal("VARCHAR", d.Get("return_type").(string)) r.Equal("mock comment", d.Get("comment").(string)) }) } @@ -85,7 +84,6 @@ func TestProcedureRead(t *testing.T) { r.Equal("MY_DB", d.Get("database").(string)) r.Equal("MY_SCHEMA", d.Get("schema").(string)) r.Equal("mock comment", d.Get("comment").(string)) - r.Equal("VARCHAR", d.Get("return_type").(string)) r.Equal("SQL", d.Get("language").(string)) r.Equal("IMMUTABLE", d.Get("return_behavior").(string)) r.Equal(procedureBody, d.Get("statement").(string)) From d9a7219ddfddf5ed997077f1e97c1648df8da600 Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Tue, 11 Oct 2022 04:51:04 -0700 Subject: [PATCH 8/8] update test --- pkg/resources/procedure_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/resources/procedure_test.go b/pkg/resources/procedure_test.go index 2190ae2b02..52fea82776 100644 --- a/pkg/resources/procedure_test.go +++ b/pkg/resources/procedure_test.go @@ -106,7 +106,6 @@ func TestProcedureRead(t *testing.T) { err := resources.ReadProcedure(d, db) r.NoError(err) r.Equal("MY_PROC", d.Get("name").(string)) - r.Equal("TABLE", d.Get("return_type").(string)) }) }