diff --git a/MIGRATION_GUIDE.md b/MIGRATION_GUIDE.md index a2c42bb525..8ab61dfbba 100644 --- a/MIGRATION_GUIDE.md +++ b/MIGRATION_GUIDE.md @@ -26,11 +26,15 @@ The Snowflake provider will use the following order of precedence when determini Longer context in [#2517](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2517). After this change, one apply may be required to update the state correctly for failover group resources using `ACCOUNT PARAMETERS`. -### snowflake_schema resource changes -#### *(behavior change)* Schema `data_retention_days` -To make `snowflake_schema.data_retention_days` truly optional field (previously it was producing plan every time when no value was set), +### snowflake_database, snowflake_schema, and snowflake_table resource changes +#### *(behavior change)* Database `data_retention_time_in_days` + Schema `data_retention_days` + Table `data_retention_time_in_days` +To make data retention fields truly optional (previously they were producing plan every time when no value was set), we added `-1` as a possible value as set it as default. That got rid of the unexpected plans when no value is set and added possibility to use default value assigned by Snowflake (see [the data retention period](https://docs.snowflake.com/en/user-guide/data-time-travel#data-retention-period)). +### snowflake_table resource changes +#### *(behavior change)* Table `data_retention_days` field removed in favor of `data_retention_time_in_days` +To define data retention days for table `data_retention_time_in_days` should be used as deprecated `data_retention_days` field is being removed. + ## v0.85.0 ➞ v0.86.0 ### snowflake_table_constraint resource changes diff --git a/docs/resources/database.md b/docs/resources/database.md index b8b126b890..a8d09fdeed 100644 --- a/docs/resources/database.md +++ b/docs/resources/database.md @@ -55,7 +55,7 @@ resource "snowflake_database" "from_share" { ### Optional - `comment` (String) -- `data_retention_time_in_days` (Number) Number of days for which Snowflake retains historical data for performing Time Travel actions (SELECT, CLONE, UNDROP) on the object. A value of 0 effectively disables Time Travel for the specified database, schema, or table. For more information, see Understanding & Using Time Travel. +- `data_retention_time_in_days` (Number) Number of days for which Snowflake retains historical data for performing Time Travel actions (SELECT, CLONE, UNDROP) on the object. A value of 0 effectively disables Time Travel for the specified database. Default value for this field is set to -1, which is a fallback to use Snowflake default. For more information, see Understanding & Using Time Travel. - `from_database` (String) Specify a database to create a clone from. - `from_replica` (String) Specify a fully-qualified path to a database to create a replica from. A fully qualified path follows the format of "".""."". An example would be: "myorg1"."account1"."db1" - `from_share` (Map of String) Specify a provider and a share in this map to create a database from a share. diff --git a/docs/resources/table.md b/docs/resources/table.md index c137d245c9..94fc8c8643 100644 --- a/docs/resources/table.md +++ b/docs/resources/table.md @@ -95,8 +95,7 @@ resource "snowflake_table" "table" { - `change_tracking` (Boolean) Specifies whether to enable change tracking on the table. Default false. - `cluster_by` (List of String) A list of one or more table columns/expressions to be used as clustering key(s) for the table - `comment` (String) Specifies a comment for the table. -- `data_retention_days` (Number, Deprecated) Specifies the retention period for the table so that Time Travel actions (SELECT, CLONE, UNDROP) can be performed on historical data in the table. Default value is 1, if you wish to inherit the parent schema setting then pass in the schema attribute to this argument. -- `data_retention_time_in_days` (Number) Specifies the retention period for the table so that Time Travel actions (SELECT, CLONE, UNDROP) can be performed on historical data in the table. Default value is 1, if you wish to inherit the parent schema setting then pass in the schema attribute to this argument. +- `data_retention_time_in_days` (Number) Specifies the retention period for the table so that Time Travel actions (SELECT, CLONE, UNDROP) can be performed on historical data in the table. If you wish to inherit the parent schema setting then pass in the schema attribute to this argument or do not fill this parameter at all; the default value for this field is -1, which is a fallback to use Snowflake default - in this case the schema value - `primary_key` (Block List, Max: 1, Deprecated) Definitions of primary key constraint to create on table (see [below for nested schema](#nestedblock--primary_key)) - `tag` (Block List, Deprecated) Definitions of a tag to associate with the resource. (see [below for nested schema](#nestedblock--tag)) diff --git a/pkg/resources/database.go b/pkg/resources/database.go index 1cff71017b..e55e6dff1c 100644 --- a/pkg/resources/database.go +++ b/pkg/resources/database.go @@ -4,7 +4,11 @@ import ( "context" "database/sql" "fmt" + "log" "slices" + "strconv" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -28,10 +32,11 @@ var databaseSchema = map[string]*schema.Schema{ ForceNew: true, }, "data_retention_time_in_days": { - Type: schema.TypeInt, - Optional: true, - Description: "Number of days for which Snowflake retains historical data for performing Time Travel actions (SELECT, CLONE, UNDROP) on the object. A value of 0 effectively disables Time Travel for the specified database, schema, or table. For more information, see Understanding & Using Time Travel.", - Default: 1, + Type: schema.TypeInt, + Optional: true, + Default: -1, + Description: "Number of days for which Snowflake retains historical data for performing Time Travel actions (SELECT, CLONE, UNDROP) on the object. A value of 0 effectively disables Time Travel for the specified database. Default value for this field is set to -1, which is a fallback to use Snowflake default. For more information, see Understanding & Using Time Travel.", + ValidateFunc: validation.IntBetween(-1, 90), }, "from_share": { Type: schema.TypeMap, @@ -120,8 +125,9 @@ func CreateDatabase(d *schema.ResourceData, meta interface{}) error { // Is it a Secondary Database? if primaryName, ok := d.GetOk("from_replica"); ok { primaryID := sdk.NewExternalObjectIdentifierFromFullyQualifiedName(primaryName.(string)) - opts := &sdk.CreateSecondaryDatabaseOptions{ - DataRetentionTimeInDays: sdk.Int(d.Get("data_retention_time_in_days").(int)), + opts := &sdk.CreateSecondaryDatabaseOptions{} + if v := d.Get("data_retention_time_in_days"); v.(int) != -1 { + opts.DataRetentionTimeInDays = sdk.Int(v.(int)) } err := client.Databases.CreateSecondary(ctx, id, primaryID, opts) if err != nil { @@ -148,7 +154,7 @@ func CreateDatabase(d *schema.ResourceData, meta interface{}) error { } } - if v, ok := d.GetOk("data_retention_time_in_days"); ok { + if v := d.Get("data_retention_time_in_days"); v.(int) != -1 { opts.DataRetentionTimeInDays = sdk.Int(v.(int)) } @@ -192,6 +198,7 @@ func ReadDatabase(d *schema.ResourceData, meta interface{}) error { database, err := client.Databases.ShowByID(ctx, id) if err != nil { d.SetId("") + log.Printf("Database %s not found, err = %s", name, err) return nil } @@ -202,10 +209,21 @@ func ReadDatabase(d *schema.ResourceData, meta interface{}) error { return err } - if err := d.Set("data_retention_time_in_days", database.RetentionTime); err != nil { + dataRetention, err := client.Parameters.ShowAccountParameter(ctx, sdk.AccountParameterDataRetentionTimeInDays) + if err != nil { + return err + } + paramDataRetention, err := strconv.Atoi(dataRetention.Value) + if err != nil { return err } + if dataRetentionDays := d.Get("data_retention_time_in_days"); dataRetentionDays.(int) != -1 || database.RetentionTime != paramDataRetention { + if err := d.Set("data_retention_time_in_days", database.RetentionTime); err != nil { + return err + } + } + if err := d.Set("is_transient", database.Transient); err != nil { return err } @@ -250,15 +268,24 @@ func UpdateDatabase(d *schema.ResourceData, meta interface{}) error { } if d.HasChange("data_retention_time_in_days") { - days := d.Get("data_retention_time_in_days").(int) - opts := &sdk.AlterDatabaseOptions{ - Set: &sdk.DatabaseSet{ - DataRetentionTimeInDays: sdk.Int(days), - }, - } - err := client.Databases.Alter(ctx, id, opts) - if err != nil { - return fmt.Errorf("error updating database data retention time on %v err = %w", d.Id(), err) + if days := d.Get("data_retention_time_in_days"); days.(int) != -1 { + err := client.Databases.Alter(ctx, id, &sdk.AlterDatabaseOptions{ + Set: &sdk.DatabaseSet{ + DataRetentionTimeInDays: sdk.Int(days.(int)), + }, + }) + if err != nil { + return fmt.Errorf("error when setting database data retention time on %v err = %w", d.Id(), err) + } + } else { + err := client.Databases.Alter(ctx, id, &sdk.AlterDatabaseOptions{ + Unset: &sdk.DatabaseUnset{ + DataRetentionTimeInDays: sdk.Bool(true), + }, + }) + if err != nil { + return fmt.Errorf("error when usetting database data retention time on %v err = %w", d.Id(), err) + } } } diff --git a/pkg/resources/database_acceptance_test.go b/pkg/resources/database_acceptance_test.go index 1f9a3c46e2..6a803588fc 100644 --- a/pkg/resources/database_acceptance_test.go +++ b/pkg/resources/database_acceptance_test.go @@ -2,7 +2,9 @@ package resources_test import ( "context" + "database/sql" "fmt" + "strconv" "strings" "testing" @@ -178,6 +180,167 @@ func TestAcc_Database_issue2021(t *testing.T) { }) } +// proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2356 issue is fixed. +func TestAcc_Database_DefaultDataRetentionTime(t *testing.T) { + databaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + id := sdk.NewAccountObjectIdentifier(databaseName) + + configVariablesWithoutDatabaseDataRetentionTime := func() config.Variables { + return config.Variables{ + "database": config.StringVariable(databaseName), + } + } + + configVariablesWithDatabaseDataRetentionTime := func(databaseDataRetentionTime int) config.Variables { + vars := configVariablesWithoutDatabaseDataRetentionTime() + vars["database_data_retention_time"] = config.IntegerVariable(databaseDataRetentionTime) + return vars + } + + client, err := sdk.NewDefaultClient() + require.NoError(t, err) + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, + PreCheck: func() { acc.TestAccPreCheck(t) }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.RequireAbove(tfversion.Version1_5_0), + }, + CheckDestroy: nil, + Steps: []resource.TestStep{ + { + PreConfig: updateAccountParameter(t, client, sdk.AccountParameterDataRetentionTimeInDays, true, "5"), + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Database_DefaultDataRetentionTime/WithoutDataRetentionSet"), + ConfigVariables: configVariablesWithoutDatabaseDataRetentionTime(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_database.test", "data_retention_time_in_days", "-1"), + checkAccountAndDatabaseDataRetentionTime(id, 5, 5), + ), + }, + { + PreConfig: updateAccountParameter(t, client, sdk.AccountParameterDataRetentionTimeInDays, false, "10"), + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Database_DefaultDataRetentionTime/WithoutDataRetentionSet"), + ConfigVariables: configVariablesWithoutDatabaseDataRetentionTime(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_database.test", "data_retention_time_in_days", "-1"), + checkAccountAndDatabaseDataRetentionTime(id, 10, 10), + ), + }, + { + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Database_DefaultDataRetentionTime/WithDataRetentionSet"), + ConfigVariables: configVariablesWithDatabaseDataRetentionTime(5), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_database.test", "data_retention_time_in_days", "5"), + checkAccountAndDatabaseDataRetentionTime(id, 10, 5), + ), + }, + { + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Database_DefaultDataRetentionTime/WithDataRetentionSet"), + ConfigVariables: configVariablesWithDatabaseDataRetentionTime(15), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_database.test", "data_retention_time_in_days", "15"), + checkAccountAndDatabaseDataRetentionTime(id, 10, 15), + ), + }, + { + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Database_DefaultDataRetentionTime/WithoutDataRetentionSet"), + ConfigVariables: configVariablesWithoutDatabaseDataRetentionTime(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_database.test", "data_retention_time_in_days", "-1"), + checkAccountAndDatabaseDataRetentionTime(id, 10, 10), + ), + }, + { + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Database_DefaultDataRetentionTime/WithDataRetentionSet"), + ConfigVariables: configVariablesWithDatabaseDataRetentionTime(0), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_database.test", "data_retention_time_in_days", "0"), + checkAccountAndDatabaseDataRetentionTime(id, 10, 0), + ), + }, + { + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Database_DefaultDataRetentionTime/WithDataRetentionSet"), + ConfigVariables: configVariablesWithDatabaseDataRetentionTime(3), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_database.test", "data_retention_time_in_days", "3"), + checkAccountAndDatabaseDataRetentionTime(id, 10, 3), + ), + }, + }, + }) +} + +// proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2356 issue is fixed. +func TestAcc_Database_DefaultDataRetentionTime_SetOutsideOfTerraform(t *testing.T) { + databaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + id := sdk.NewAccountObjectIdentifier(databaseName) + + configVariablesWithoutDatabaseDataRetentionTime := func() config.Variables { + return config.Variables{ + "database": config.StringVariable(databaseName), + } + } + + configVariablesWithDatabaseDataRetentionTime := func(databaseDataRetentionTime int) config.Variables { + vars := configVariablesWithoutDatabaseDataRetentionTime() + vars["database_data_retention_time"] = config.IntegerVariable(databaseDataRetentionTime) + return vars + } + + client, err := sdk.NewDefaultClient() + require.NoError(t, err) + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, + PreCheck: func() { acc.TestAccPreCheck(t) }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.RequireAbove(tfversion.Version1_5_0), + }, + CheckDestroy: nil, + Steps: []resource.TestStep{ + { + PreConfig: updateAccountParameter(t, client, sdk.AccountParameterDataRetentionTimeInDays, true, "5"), + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Database_DefaultDataRetentionTime/WithoutDataRetentionSet"), + ConfigVariables: configVariablesWithoutDatabaseDataRetentionTime(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_database.test", "data_retention_time_in_days", "-1"), + checkAccountAndDatabaseDataRetentionTime(id, 5, 5), + ), + }, + { + PreConfig: func() { + err = client.Databases.Alter(context.Background(), id, &sdk.AlterDatabaseOptions{ + Set: &sdk.DatabaseSet{ + DataRetentionTimeInDays: sdk.Int(20), + }, + }) + require.NoError(t, err) + }, + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Database_DefaultDataRetentionTime/WithoutDataRetentionSet"), + ConfigVariables: configVariablesWithoutDatabaseDataRetentionTime(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_database.test", "data_retention_time_in_days", "-1"), + checkAccountAndDatabaseDataRetentionTime(id, 5, 5), + ), + }, + { + PreConfig: updateAccountParameter(t, client, sdk.AccountParameterDataRetentionTimeInDays, false, "10"), + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Database_DefaultDataRetentionTime/WithDataRetentionSet"), + ConfigVariables: configVariablesWithDatabaseDataRetentionTime(3), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_database.test", "data_retention_time_in_days", "3"), + checkAccountAndDatabaseDataRetentionTime(id, 10, 3), + ), + ConfigPlanChecks: resource.ConfigPlanChecks{ + PostApplyPostRefresh: []plancheck.PlanCheck{ + plancheck.ExpectEmptyPlan(), + }, + }, + }, + }, + }) +} + func dbConfig(prefix string) string { s := ` resource "snowflake_database" "db" { @@ -293,3 +456,35 @@ func testAccCheckIfDatabaseIsReplicated(t *testing.T, id string) func(state *ter return nil } } + +func checkAccountAndDatabaseDataRetentionTime(id sdk.AccountObjectIdentifier, expectedAccountRetentionDays int, expectedDatabaseRetentionsDays int) func(state *terraform.State) error { + return func(state *terraform.State) error { + db := acc.TestAccProvider.Meta().(*sql.DB) + client := sdk.NewClientFromDB(db) + ctx := context.Background() + + database, err := client.Databases.ShowByID(ctx, id) + if err != nil { + return err + } + + if database.RetentionTime != expectedDatabaseRetentionsDays { + return fmt.Errorf("invalid database retention time, expected: %d, got: %d", expectedDatabaseRetentionsDays, database.RetentionTime) + } + + param, err := client.Parameters.ShowAccountParameter(ctx, sdk.AccountParameterDataRetentionTimeInDays) + if err != nil { + return err + } + accountRetentionDays, err := strconv.Atoi(param.Value) + if err != nil { + return err + } + + if accountRetentionDays != expectedAccountRetentionDays { + return fmt.Errorf("invalid account retention time, expected: %d, got: %d", expectedAccountRetentionDays, accountRetentionDays) + } + + return nil + } +} diff --git a/pkg/resources/helpers_test.go b/pkg/resources/helpers_test.go index 941a71be92..44cdfa6b89 100644 --- a/pkg/resources/helpers_test.go +++ b/pkg/resources/helpers_test.go @@ -548,3 +548,25 @@ func queriedPrivilegesContainAtLeast(query func(client *sdk.Client, ctx context. return nil } } + +func updateAccountParameter(t *testing.T, client *sdk.Client, parameter sdk.AccountParameter, temporarily bool, newValue string) func() { + t.Helper() + + ctx := context.Background() + + param, err := client.Parameters.ShowAccountParameter(ctx, parameter) + require.NoError(t, err) + oldValue := param.Value + + if temporarily { + t.Cleanup(func() { + err = client.Parameters.SetAccountParameter(ctx, parameter, oldValue) + require.NoError(t, err) + }) + } + + return func() { + err = client.Parameters.SetAccountParameter(ctx, parameter, newValue) + require.NoError(t, err) + } +} diff --git a/pkg/resources/schema_acceptance_test.go b/pkg/resources/schema_acceptance_test.go index f29e649b19..39c19a2aed 100644 --- a/pkg/resources/schema_acceptance_test.go +++ b/pkg/resources/schema_acceptance_test.go @@ -170,65 +170,66 @@ func TestAcc_Schema_DefaultDataRetentionTime(t *testing.T) { CheckDestroy: testAccCheckSchemaDestroy, Steps: []resource.TestStep{ { - ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithoutSchema"), + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithoutDataRetentionSet"), ConfigVariables: configVariablesWithoutSchemaDataRetentionTime(5), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("snowflake_schema.test", "data_retention_days", "-1"), - checkDataRetentionTime(id, 5, 5), + checkDatabaseAndSchemaDataRetentionTime(id, 5, 5), ), }, { - ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithoutSchema"), + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithoutDataRetentionSet"), ConfigVariables: configVariablesWithoutSchemaDataRetentionTime(10), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("snowflake_schema.test", "data_retention_days", "-1"), - checkDataRetentionTime(id, 10, 10), + checkDatabaseAndSchemaDataRetentionTime(id, 10, 10), ), }, { - ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithSchema"), + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithDataRetentionSet"), ConfigVariables: configVariablesWithSchemaDataRetentionTime(10, 5), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("snowflake_schema.test", "data_retention_days", "5"), - checkDataRetentionTime(id, 10, 5), + checkDatabaseAndSchemaDataRetentionTime(id, 10, 5), ), }, { - ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithSchema"), + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithDataRetentionSet"), ConfigVariables: configVariablesWithSchemaDataRetentionTime(10, 15), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("snowflake_schema.test", "data_retention_days", "15"), - checkDataRetentionTime(id, 10, 15), + checkDatabaseAndSchemaDataRetentionTime(id, 10, 15), ), }, { - ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithoutSchema"), + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithoutDataRetentionSet"), ConfigVariables: configVariablesWithoutSchemaDataRetentionTime(10), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("snowflake_schema.test", "data_retention_days", "-1"), - checkDataRetentionTime(id, 10, 10), + checkDatabaseAndSchemaDataRetentionTime(id, 10, 10), ), }, { - ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithSchema"), + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithDataRetentionSet"), ConfigVariables: configVariablesWithSchemaDataRetentionTime(10, 0), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("snowflake_schema.test", "data_retention_days", "0"), - checkDataRetentionTime(id, 10, 0), + checkDatabaseAndSchemaDataRetentionTime(id, 10, 0), ), }, { - ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithSchema"), + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithDataRetentionSet"), ConfigVariables: configVariablesWithSchemaDataRetentionTime(10, 3), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("snowflake_schema.test", "data_retention_days", "3"), - checkDataRetentionTime(id, 10, 3), + checkDatabaseAndSchemaDataRetentionTime(id, 10, 3), ), }, }, }) } +// proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2356 issue is fixed. func TestAcc_Schema_DefaultDataRetentionTime_SetOutsideOfTerraform(t *testing.T) { databaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) schemaName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) @@ -257,28 +258,28 @@ func TestAcc_Schema_DefaultDataRetentionTime_SetOutsideOfTerraform(t *testing.T) CheckDestroy: testAccCheckSchemaDestroy, Steps: []resource.TestStep{ { - ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithoutSchema"), + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithoutDataRetentionSet"), ConfigVariables: configVariablesWithoutSchemaDataRetentionTime(5), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("snowflake_schema.test", "data_retention_days", "-1"), - checkDataRetentionTime(id, 5, 5), + checkDatabaseAndSchemaDataRetentionTime(id, 5, 5), ), }, { PreConfig: setSchemaDataRetentionTime(t, id, 20), - ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithoutSchema"), + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithoutDataRetentionSet"), ConfigVariables: configVariablesWithoutSchemaDataRetentionTime(5), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("snowflake_schema.test", "data_retention_days", "-1"), - checkDataRetentionTime(id, 5, 5), + checkDatabaseAndSchemaDataRetentionTime(id, 5, 5), ), }, { - ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithSchema"), + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Schema_DefaultDataRetentionTime/WithDataRetentionSet"), ConfigVariables: configVariablesWithSchemaDataRetentionTime(10, 3), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("snowflake_schema.test", "data_retention_days", "3"), - checkDataRetentionTime(id, 10, 3), + checkDatabaseAndSchemaDataRetentionTime(id, 10, 3), ), ConfigPlanChecks: resource.ConfigPlanChecks{ PostApplyPostRefresh: []plancheck.PlanCheck{ @@ -290,7 +291,7 @@ func TestAcc_Schema_DefaultDataRetentionTime_SetOutsideOfTerraform(t *testing.T) }) } -func checkDataRetentionTime(id sdk.DatabaseObjectIdentifier, expectedDatabaseRetentionsDays int, expectedSchemaRetentionDays int) func(state *terraform.State) error { +func checkDatabaseAndSchemaDataRetentionTime(id sdk.DatabaseObjectIdentifier, expectedDatabaseRetentionsDays int, expectedSchemaRetentionDays int) func(state *terraform.State) error { return func(state *terraform.State) error { db := acc.TestAccProvider.Meta().(*sql.DB) client := sdk.NewClientFromDB(db) diff --git a/pkg/resources/table.go b/pkg/resources/table.go index ae0b67da63..7539925bbb 100644 --- a/pkg/resources/table.go +++ b/pkg/resources/table.go @@ -182,20 +182,12 @@ var tableSchema = map[string]*schema.Schema{ }, }, }, - "data_retention_days": { - Type: schema.TypeInt, - Optional: true, - Description: "Specifies the retention period for the table so that Time Travel actions (SELECT, CLONE, UNDROP) can be performed on historical data in the table. Default value is 1, if you wish to inherit the parent schema setting then pass in the schema attribute to this argument.", - ValidateFunc: validation.IntBetween(0, 90), - Deprecated: "Use data_retention_time_in_days attribute instead", - ConflictsWith: []string{"data_retention_time_in_days"}, - }, "data_retention_time_in_days": { - Type: schema.TypeInt, - Optional: true, - Description: "Specifies the retention period for the table so that Time Travel actions (SELECT, CLONE, UNDROP) can be performed on historical data in the table. Default value is 1, if you wish to inherit the parent schema setting then pass in the schema attribute to this argument.", - ValidateFunc: validation.IntBetween(0, 90), - ConflictsWith: []string{"data_retention_days"}, + Type: schema.TypeInt, + Optional: true, + Default: -1, + Description: "Specifies the retention period for the table so that Time Travel actions (SELECT, CLONE, UNDROP) can be performed on historical data in the table. If you wish to inherit the parent schema setting then pass in the schema attribute to this argument or do not fill this parameter at all; the default value for this field is -1, which is a fallback to use Snowflake default - in this case the schema value", + ValidateFunc: validation.IntBetween(0, 90), }, "change_tracking": { Type: schema.TypeBool, @@ -599,9 +591,7 @@ func CreateTable(d *schema.ResourceData, meta interface{}) error { } } - if v, ok := d.GetOk("data_retention_days"); ok { - createRequest.WithDataRetentionTimeInDays(sdk.Int(v.(int))) - } else if v, ok := d.GetOk("data_retention_time_in_days"); ok { + if v := d.Get("data_retention_time_in_days"); v.(int) != -1 { createRequest.WithDataRetentionTimeInDays(sdk.Int(v.(int))) } @@ -643,6 +633,26 @@ func ReadTable(d *schema.ResourceData, meta interface{}) error { return nil } + s, err := client.Schemas.ShowByID(ctx, sdk.NewDatabaseObjectIdentifier(id.DatabaseName(), id.SchemaName())) + if err != nil { + log.Printf("[DEBUG] schema (%s) not found", d.Id()) + d.SetId("") + return nil + } + var schemaRetentionTime int64 + // "retention_time" may sometimes be empty string instead of an integer + { + rt := s.RetentionTime + if rt == "" { + rt = "0" + } + + schemaRetentionTime, err = strconv.ParseInt(rt, 10, 64) + if err != nil { + return err + } + } + tableDescription, err := client.Tables.DescribeColumns(ctx, sdk.NewDescribeTableColumnsRequest(id)) if err != nil { return err @@ -660,14 +670,8 @@ func ReadTable(d *schema.ResourceData, meta interface{}) error { "change_tracking": table.ChangeTracking, "qualified_name": id.FullyQualifiedName(), } - var dataRetentionKey string - if _, ok := d.GetOk("data_retention_time_in_days"); ok { - dataRetentionKey = "data_retention_time_in_days" - } else if _, ok := d.GetOk("data_retention_days"); ok { - dataRetentionKey = "data_retention_days" - } - if dataRetentionKey != "" { - toSet[dataRetentionKey] = table.RetentionTime + if v := d.Get("data_retention_time_in_days"); v.(int) != -1 || int64(table.RetentionTime) != schemaRetentionTime { + toSet["data_retention_time_in_days"] = table.RetentionTime } for key, val := range toSet { @@ -721,15 +725,15 @@ func UpdateTable(d *schema.ResourceData, meta interface{}) error { setRequest.WithChangeTracking(sdk.Bool(changeTracking)) } - checkChangeForDataRetention := func(key string) { - if d.HasChange(key) { - dataRetentionDays := d.Get(key).(int) + if d.HasChange("data_retention_time_in_days") { + if days := d.Get("data_retention_time_in_days"); days.(int) != -1 { runSetStatement = true - setRequest.WithDataRetentionTimeInDays(sdk.Int(dataRetentionDays)) + setRequest.WithDataRetentionTimeInDays(sdk.Int(days.(int))) + } else { + runUnsetStatement = true + unsetRequest.WithDataRetentionTimeInDays(true) } } - checkChangeForDataRetention("data_retention_days") - checkChangeForDataRetention("data_retention_time_in_days") if runSetStatement { err := client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithSet(setRequest)) diff --git a/pkg/resources/table_acceptance_test.go b/pkg/resources/table_acceptance_test.go index d5c70c53cf..58bbb9f892 100644 --- a/pkg/resources/table_acceptance_test.go +++ b/pkg/resources/table_acceptance_test.go @@ -5,9 +5,14 @@ import ( "database/sql" "fmt" "regexp" + "strconv" "strings" "testing" + "github.com/hashicorp/terraform-plugin-testing/config" + "github.com/hashicorp/terraform-plugin-testing/plancheck" + "github.com/stretchr/testify/require" + acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -1475,6 +1480,175 @@ func TestAcc_Table_MaskingPolicy(t *testing.T) { }) } +// proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2356 issue is fixed. +func TestAcc_Table_DefaultDataRetentionTime(t *testing.T) { + databaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + schemaName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + tableName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + id := sdk.NewSchemaObjectIdentifier(databaseName, schemaName, tableName) + + configWithDatabaseDataRetentionSet := func(databaseDataRetentionTime int) config.Variables { + return config.Variables{ + "database": config.StringVariable(databaseName), + "schema": config.StringVariable(schemaName), + "table": config.StringVariable(tableName), + "database_data_retention_time": config.IntegerVariable(databaseDataRetentionTime), + } + } + + configWithSchemaDataRetentionSet := func(databaseDataRetentionTime int, schemaDataRetentionTime int) config.Variables { + vars := configWithDatabaseDataRetentionSet(databaseDataRetentionTime) + vars["schema_data_retention_time"] = config.IntegerVariable(schemaDataRetentionTime) + return vars + } + + configWithTableDataRetentionSet := func(databaseDataRetentionTime int, schemaDataRetentionTime int, tableDataRetentionTime int) config.Variables { + vars := configWithSchemaDataRetentionSet(databaseDataRetentionTime, schemaDataRetentionTime) + vars["table_data_retention_time"] = config.IntegerVariable(tableDataRetentionTime) + return vars + } + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, + PreCheck: func() { acc.TestAccPreCheck(t) }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.RequireAbove(tfversion.Version1_5_0), + }, + CheckDestroy: testAccCheckTableDestroy, + Steps: []resource.TestStep{ + { + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Table_DefaultDataRetentionTime/WithDatabaseDataRetentionSet"), + ConfigVariables: configWithDatabaseDataRetentionSet(5), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_table.test", "data_retention_time_in_days", "-1"), + checkDatabaseSchemaAndTableDataRetentionTime(id, 5, 5, 5), + ), + }, + { + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Table_DefaultDataRetentionTime/WithSchemaDataRetentionSet"), + ConfigVariables: configWithSchemaDataRetentionSet(5, 10), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_table.test", "data_retention_time_in_days", "-1"), + checkDatabaseSchemaAndTableDataRetentionTime(id, 5, 10, 10), + ), + }, + { + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Table_DefaultDataRetentionTime/WithTableDataRetentionSet"), + ConfigVariables: configWithTableDataRetentionSet(10, 3, 5), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_table.test", "data_retention_time_in_days", "5"), + checkDatabaseSchemaAndTableDataRetentionTime(id, 10, 3, 5), + ), + }, + { + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Table_DefaultDataRetentionTime/WithTableDataRetentionSet"), + ConfigVariables: configWithTableDataRetentionSet(10, 3, 15), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_table.test", "data_retention_time_in_days", "15"), + checkDatabaseSchemaAndTableDataRetentionTime(id, 10, 3, 15), + ), + }, + { + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Table_DefaultDataRetentionTime/WithSchemaDataRetentionSet"), + ConfigVariables: configWithSchemaDataRetentionSet(10, 3), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_table.test", "data_retention_time_in_days", "-1"), + checkDatabaseSchemaAndTableDataRetentionTime(id, 10, 3, 3), + ), + }, + { + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Table_DefaultDataRetentionTime/WithDatabaseDataRetentionSet"), + ConfigVariables: configWithDatabaseDataRetentionSet(10), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_table.test", "data_retention_time_in_days", "-1"), + checkDatabaseSchemaAndTableDataRetentionTime(id, 10, 10, 10), + ), + }, + { + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Table_DefaultDataRetentionTime/WithTableDataRetentionSet"), + ConfigVariables: configWithTableDataRetentionSet(10, 5, 0), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_table.test", "data_retention_time_in_days", "0"), + checkDatabaseSchemaAndTableDataRetentionTime(id, 10, 5, 0), + ), + }, + { + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Table_DefaultDataRetentionTime/WithTableDataRetentionSet"), + ConfigVariables: configWithTableDataRetentionSet(10, 5, 3), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_table.test", "data_retention_time_in_days", "3"), + checkDatabaseSchemaAndTableDataRetentionTime(id, 10, 5, 3), + ), + }, + }, + }) +} + +// proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2356 issue is fixed. +func TestAcc_Table_DefaultDataRetentionTime_SetOutsideOfTerraform(t *testing.T) { + databaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + schemaName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + tableName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + id := sdk.NewSchemaObjectIdentifier(databaseName, schemaName, tableName) + + configWithDatabaseDataRetentionSet := func(databaseDataRetentionTime int) config.Variables { + return config.Variables{ + "database": config.StringVariable(databaseName), + "schema": config.StringVariable(schemaName), + "table": config.StringVariable(tableName), + "database_data_retention_time": config.IntegerVariable(databaseDataRetentionTime), + } + } + + configWithTableDataRetentionSet := func(databaseDataRetentionTime int, schemaDataRetentionTime int, tableDataRetentionTime int) config.Variables { + vars := configWithDatabaseDataRetentionSet(databaseDataRetentionTime) + vars["schema_data_retention_time"] = config.IntegerVariable(schemaDataRetentionTime) + vars["table_data_retention_time"] = config.IntegerVariable(tableDataRetentionTime) + return vars + } + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, + PreCheck: func() { acc.TestAccPreCheck(t) }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.RequireAbove(tfversion.Version1_5_0), + }, + CheckDestroy: nil, + Steps: []resource.TestStep{ + { + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Table_DefaultDataRetentionTime/WithDatabaseDataRetentionSet"), + ConfigVariables: configWithDatabaseDataRetentionSet(5), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_table.test", "data_retention_time_in_days", "-1"), + checkDatabaseSchemaAndTableDataRetentionTime(id, 5, 5, 5), + ), + }, + { + PreConfig: setTableDataRetentionTime(t, id, 20), + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Table_DefaultDataRetentionTime/WithDatabaseDataRetentionSet"), + ConfigVariables: configWithDatabaseDataRetentionSet(5), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_table.test", "data_retention_time_in_days", "-1"), + checkDatabaseSchemaAndTableDataRetentionTime(id, 5, 5, 5), + ), + }, + { + ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Table_DefaultDataRetentionTime/WithTableDataRetentionSet"), + ConfigVariables: configWithTableDataRetentionSet(5, 10, 3), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("snowflake_table.test", "data_retention_time_in_days", "3"), + checkDatabaseSchemaAndTableDataRetentionTime(id, 5, 10, 3), + ), + ConfigPlanChecks: resource.ConfigPlanChecks{ + PostApplyPostRefresh: []plancheck.PlanCheck{ + plancheck.ExpectEmptyPlan(), + }, + }, + }, + }, + }) +} + func tableWithMaskingPolicy(name string, databaseName string, schemaName string, policy string) string { s := ` resource "snowflake_masking_policy" "policy1" { @@ -1584,3 +1758,67 @@ resource "snowflake_table" "test_table" { } `, name, databaseName, schemaName) } + +func checkDatabaseSchemaAndTableDataRetentionTime(id sdk.SchemaObjectIdentifier, expectedDatabaseRetentionDays int, expectedSchemaRetentionDays int, expectedTableRetentionsDays int) func(state *terraform.State) error { + return func(state *terraform.State) error { + db := acc.TestAccProvider.Meta().(*sql.DB) + client := sdk.NewClientFromDB(db) + ctx := context.Background() + + database, err := client.Databases.ShowByID(ctx, sdk.NewAccountObjectIdentifier(id.DatabaseName())) + if err != nil { + return err + } + + if database.RetentionTime != expectedDatabaseRetentionDays { + return fmt.Errorf("invalid database retention time, expected: %d, got: %d", expectedDatabaseRetentionDays, database.RetentionTime) + } + + s, err := client.Schemas.ShowByID(ctx, sdk.NewDatabaseObjectIdentifier(id.DatabaseName(), id.SchemaName())) + if err != nil { + return err + } + + // "retention_time" may sometimes be an empty string instead of an integer + var schemaRetentionTime int64 + { + rt := s.RetentionTime + if rt == "" { + rt = "0" + } + + schemaRetentionTime, err = strconv.ParseInt(rt, 10, 64) + if err != nil { + return err + } + } + + if schemaRetentionTime != int64(expectedSchemaRetentionDays) { + return fmt.Errorf("invalid schema retention time, expected: %d, got: %d", expectedSchemaRetentionDays, schemaRetentionTime) + } + + table, err := client.Tables.ShowByID(ctx, id) + if err != nil { + return err + } + + if table.RetentionTime != expectedTableRetentionsDays { + return fmt.Errorf("invalid table retention time, expected: %d, got: %d", expectedTableRetentionsDays, table.RetentionTime) + } + + return nil + } +} + +func setTableDataRetentionTime(t *testing.T, id sdk.SchemaObjectIdentifier, days int) func() { + t.Helper() + + return func() { + client, err := sdk.NewDefaultClient() + require.NoError(t, err) + ctx := context.Background() + + err = client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithSet(sdk.NewTableSetRequest().WithDataRetentionTimeInDays(sdk.Int(days)))) + require.NoError(t, err) + } +} diff --git a/pkg/resources/testdata/TestAcc_Database_DefaultDataRetentionTime/WithDataRetentionSet/test.tf b/pkg/resources/testdata/TestAcc_Database_DefaultDataRetentionTime/WithDataRetentionSet/test.tf new file mode 100644 index 0000000000..12a910c7d2 --- /dev/null +++ b/pkg/resources/testdata/TestAcc_Database_DefaultDataRetentionTime/WithDataRetentionSet/test.tf @@ -0,0 +1,4 @@ +resource "snowflake_database" "test" { + name = var.database + data_retention_time_in_days = var.database_data_retention_time +} diff --git a/pkg/resources/testdata/TestAcc_Database_DefaultDataRetentionTime/WithDataRetentionSet/variables.tf b/pkg/resources/testdata/TestAcc_Database_DefaultDataRetentionTime/WithDataRetentionSet/variables.tf new file mode 100644 index 0000000000..32f9fb7140 --- /dev/null +++ b/pkg/resources/testdata/TestAcc_Database_DefaultDataRetentionTime/WithDataRetentionSet/variables.tf @@ -0,0 +1,7 @@ +variable "database" { + type = string +} + +variable "database_data_retention_time" { + type = number +} diff --git a/pkg/resources/testdata/TestAcc_Database_DefaultDataRetentionTime/WithoutDataRetentionSet/test.tf b/pkg/resources/testdata/TestAcc_Database_DefaultDataRetentionTime/WithoutDataRetentionSet/test.tf new file mode 100644 index 0000000000..894e86ac20 --- /dev/null +++ b/pkg/resources/testdata/TestAcc_Database_DefaultDataRetentionTime/WithoutDataRetentionSet/test.tf @@ -0,0 +1,3 @@ +resource "snowflake_database" "test" { + name = var.database +} diff --git a/pkg/resources/testdata/TestAcc_Database_DefaultDataRetentionTime/WithoutDataRetentionSet/variables.tf b/pkg/resources/testdata/TestAcc_Database_DefaultDataRetentionTime/WithoutDataRetentionSet/variables.tf new file mode 100644 index 0000000000..bfdd9eeb3c --- /dev/null +++ b/pkg/resources/testdata/TestAcc_Database_DefaultDataRetentionTime/WithoutDataRetentionSet/variables.tf @@ -0,0 +1,3 @@ +variable "database" { + type = string +} diff --git a/pkg/resources/testdata/TestAcc_Schema_DefaultDataRetentionTime/WithSchema/test.tf b/pkg/resources/testdata/TestAcc_Schema_DefaultDataRetentionTime/WithDataRetentionSet/test.tf similarity index 100% rename from pkg/resources/testdata/TestAcc_Schema_DefaultDataRetentionTime/WithSchema/test.tf rename to pkg/resources/testdata/TestAcc_Schema_DefaultDataRetentionTime/WithDataRetentionSet/test.tf diff --git a/pkg/resources/testdata/TestAcc_Schema_DefaultDataRetentionTime/WithSchema/variables.tf b/pkg/resources/testdata/TestAcc_Schema_DefaultDataRetentionTime/WithDataRetentionSet/variables.tf similarity index 100% rename from pkg/resources/testdata/TestAcc_Schema_DefaultDataRetentionTime/WithSchema/variables.tf rename to pkg/resources/testdata/TestAcc_Schema_DefaultDataRetentionTime/WithDataRetentionSet/variables.tf diff --git a/pkg/resources/testdata/TestAcc_Schema_DefaultDataRetentionTime/WithoutSchema/test.tf b/pkg/resources/testdata/TestAcc_Schema_DefaultDataRetentionTime/WithoutDataRetentionSet/test.tf similarity index 100% rename from pkg/resources/testdata/TestAcc_Schema_DefaultDataRetentionTime/WithoutSchema/test.tf rename to pkg/resources/testdata/TestAcc_Schema_DefaultDataRetentionTime/WithoutDataRetentionSet/test.tf diff --git a/pkg/resources/testdata/TestAcc_Schema_DefaultDataRetentionTime/WithoutSchema/variables.tf b/pkg/resources/testdata/TestAcc_Schema_DefaultDataRetentionTime/WithoutDataRetentionSet/variables.tf similarity index 100% rename from pkg/resources/testdata/TestAcc_Schema_DefaultDataRetentionTime/WithoutSchema/variables.tf rename to pkg/resources/testdata/TestAcc_Schema_DefaultDataRetentionTime/WithoutDataRetentionSet/variables.tf diff --git a/pkg/resources/testdata/TestAcc_Table_DefaultDataRetentionTime/WithDatabaseDataRetentionSet/test.tf b/pkg/resources/testdata/TestAcc_Table_DefaultDataRetentionTime/WithDatabaseDataRetentionSet/test.tf new file mode 100644 index 0000000000..f5ec2ef471 --- /dev/null +++ b/pkg/resources/testdata/TestAcc_Table_DefaultDataRetentionTime/WithDatabaseDataRetentionSet/test.tf @@ -0,0 +1,20 @@ +resource "snowflake_database" "test" { + name = var.database + data_retention_time_in_days = var.database_data_retention_time +} + +resource "snowflake_schema" "test" { + database = snowflake_database.test.name + name = var.schema +} + +resource "snowflake_table" "test" { + database = snowflake_database.test.name + schema = snowflake_schema.test.name + name = var.table + + column { + name = "id" + type = "NUMBER(38,0)" + } +} diff --git a/pkg/resources/testdata/TestAcc_Table_DefaultDataRetentionTime/WithDatabaseDataRetentionSet/variables.tf b/pkg/resources/testdata/TestAcc_Table_DefaultDataRetentionTime/WithDatabaseDataRetentionSet/variables.tf new file mode 100644 index 0000000000..38cd514cf9 --- /dev/null +++ b/pkg/resources/testdata/TestAcc_Table_DefaultDataRetentionTime/WithDatabaseDataRetentionSet/variables.tf @@ -0,0 +1,15 @@ +variable "database" { + type = string +} + +variable "schema" { + type = string +} + +variable "table" { + type = string +} + +variable "database_data_retention_time" { + type = number +} diff --git a/pkg/resources/testdata/TestAcc_Table_DefaultDataRetentionTime/WithSchemaDataRetentionSet/test.tf b/pkg/resources/testdata/TestAcc_Table_DefaultDataRetentionTime/WithSchemaDataRetentionSet/test.tf new file mode 100644 index 0000000000..b8576b58af --- /dev/null +++ b/pkg/resources/testdata/TestAcc_Table_DefaultDataRetentionTime/WithSchemaDataRetentionSet/test.tf @@ -0,0 +1,21 @@ +resource "snowflake_database" "test" { + name = var.database + data_retention_time_in_days = var.database_data_retention_time +} + +resource "snowflake_schema" "test" { + database = snowflake_database.test.name + name = var.schema + data_retention_days = var.schema_data_retention_time +} + +resource "snowflake_table" "test" { + database = snowflake_database.test.name + schema = snowflake_schema.test.name + name = var.table + + column { + name = "id" + type = "NUMBER(38,0)" + } +} diff --git a/pkg/resources/testdata/TestAcc_Table_DefaultDataRetentionTime/WithSchemaDataRetentionSet/variables.tf b/pkg/resources/testdata/TestAcc_Table_DefaultDataRetentionTime/WithSchemaDataRetentionSet/variables.tf new file mode 100644 index 0000000000..9f9ad3fd5a --- /dev/null +++ b/pkg/resources/testdata/TestAcc_Table_DefaultDataRetentionTime/WithSchemaDataRetentionSet/variables.tf @@ -0,0 +1,19 @@ +variable "database" { + type = string +} + +variable "schema" { + type = string +} + +variable "table" { + type = string +} + +variable "database_data_retention_time" { + type = number +} + +variable "schema_data_retention_time" { + type = number +} diff --git a/pkg/resources/testdata/TestAcc_Table_DefaultDataRetentionTime/WithTableDataRetentionSet/test.tf b/pkg/resources/testdata/TestAcc_Table_DefaultDataRetentionTime/WithTableDataRetentionSet/test.tf new file mode 100644 index 0000000000..f417315d00 --- /dev/null +++ b/pkg/resources/testdata/TestAcc_Table_DefaultDataRetentionTime/WithTableDataRetentionSet/test.tf @@ -0,0 +1,22 @@ +resource "snowflake_database" "test" { + name = var.database + data_retention_time_in_days = var.database_data_retention_time +} + +resource "snowflake_schema" "test" { + database = snowflake_database.test.name + name = var.schema + data_retention_days = var.schema_data_retention_time +} + +resource "snowflake_table" "test" { + database = snowflake_database.test.name + schema = snowflake_schema.test.name + name = var.table + data_retention_time_in_days = var.table_data_retention_time + + column { + name = "id" + type = "NUMBER(38,0)" + } +} diff --git a/pkg/resources/testdata/TestAcc_Table_DefaultDataRetentionTime/WithTableDataRetentionSet/variables.tf b/pkg/resources/testdata/TestAcc_Table_DefaultDataRetentionTime/WithTableDataRetentionSet/variables.tf new file mode 100644 index 0000000000..9ca350baee --- /dev/null +++ b/pkg/resources/testdata/TestAcc_Table_DefaultDataRetentionTime/WithTableDataRetentionSet/variables.tf @@ -0,0 +1,23 @@ +variable "database" { + type = string +} + +variable "schema" { + type = string +} + +variable "table" { + type = string +} + +variable "database_data_retention_time" { + type = number +} + +variable "schema_data_retention_time" { + type = number +} + +variable "table_data_retention_time" { + type = number +}