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: standard database v1 readiness #2842

Merged
merged 24 commits into from
Jun 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add state upgraders
sfc-gh-jcieslak committed Jun 13, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit cf6792246b1afd11e22b7766673321634cfcc2c5
36 changes: 34 additions & 2 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
@@ -21,9 +21,41 @@ contains completely new implementation that follows our guidelines we set for V1
When upgrading to the 0.93.0 version, the automatic state upgrader should cover the migration for databases that didn't have the following fields set:
- `from_share` (now, the new `snowflake_shared_database` should be used instead)
- `from_replica` (now, the new `snowflake_secondary_database` should be used instead)
- [//]: # (- // TODO: check for cloning)
- `from_database` (for now, we're dropping the possibility to create a database from other databases)
- `replication_configuration`

For configurations containing `replication_configuraiton` like this one:
```terraform
resource "snowflake_database" "test" {
name = "<name>"
replication_configuration {
accounts = ["<account_locator>", "<account_locator_2>"]
ignore_edition_check = true
}
}
```

You have to transform the configuration into the following format (notice the change from account locator into the new account identifier format):
```terraform
resource "snowflake_database" "test" {
name = "%s"
replication {
enable_to_account {
account_identifier = "<organization_name>.<account_name>"
with_failover = false
}
enable_to_account {
account_identifier = "<organization_name_2>.<account_name_2>"
with_failover = false
}
}
ignore_edition_check = true
}
```

If you had `from_database` set, it should migrate automatically.
For now, we're dropping the possibility to create a clone database from other databases.
The only way will be to clone a database manually and import it as `snowflake_database`, but if
cloned databases diverge in behavior from standard databases, it may cause issues.

For databases with one of the fields mentioned above, manual migration will be needed.
Please refer to our [migration guide](https://github.com/Snowflake-Labs/terraform-provider-snowflake/blob/main/docs/technical-documentation/resource_migration.md) to perform zero downtime migration.
28 changes: 28 additions & 0 deletions pkg/acceptance/snowflakechecks/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package snowflakechecks

import (
"errors"
"fmt"
"testing"

acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

func CheckDatabaseDataRetentionTimeInDays(t *testing.T, databaseId sdk.AccountObjectIdentifier, level string, value string) resource.TestCheckFunc {
sfc-gh-asawicki marked this conversation as resolved.
Show resolved Hide resolved
t.Helper()
return func(state *terraform.State) error {
param := helpers.FindParameter(t, acc.TestClient().Parameter.ShowDatabaseParameters(t, databaseId), sdk.AccountParameterDataRetentionTimeInDays)
var errs []error
if param.Level != sdk.ParameterType(level) {
errs = append(errs, fmt.Errorf("expected parameter level %s, got %s", sdk.ParameterType(level), param.Level))
}
if param.Value != value {
errs = append(errs, fmt.Errorf("expected parameter value %s, got %s", sdk.ParameterType(level), param.Level))
}
return errors.Join(errs...)
}
}
39 changes: 25 additions & 14 deletions pkg/resources/database.go
Original file line number Diff line number Diff line change
@@ -7,16 +7,16 @@ import (
"slices"
"strings"

"github.com/hashicorp/go-cty/cty"

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

// TODO: State upgrader

var standardDatabaseSchema = map[string]*schema.Schema{
var databaseSchema = map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
@@ -76,21 +76,32 @@ var standardDatabaseSchema = map[string]*schema.Schema{

func Database() *schema.Resource {
return &schema.Resource{
CreateContext: CreateStandardDatabase,
ReadContext: ReadStandardDatabase,
DeleteContext: DeleteStandardDatabase,
UpdateContext: UpdateStandardDatabase,
SchemaVersion: 1,

CreateContext: CreateDatabase,
ReadContext: ReadDatabase,
DeleteContext: DeleteDatabase,
UpdateContext: UpdateDatabase,
Description: "Represents a standard database. If replication configuration is specified, the database is promoted to serve as a primary database for replication.",

CustomizeDiff: DatabaseParametersCustomDiff,
Schema: MergeMaps(standardDatabaseSchema, DatabaseParametersSchema),
Schema: MergeMaps(databaseSchema, DatabaseParametersSchema),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

StateUpgraders: []schema.StateUpgrader{
{
Version: 0,
// setting type to cty.EmptyObject is a bit hacky here but following https://developer.hashicorp.com/terraform/plugin/framework/migrating/resources/state-upgrade#sdkv2-1 would require lots of repetitive code; this should work with cty.EmptyObject
Type: cty.EmptyObject,
Upgrade: v092DatabaseStateUpgrader,
},
},
}
}

func CreateStandardDatabase(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
func CreateDatabase(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*provider.Context).Client

id := sdk.NewAccountObjectIdentifier(d.Get("name").(string))
@@ -197,10 +208,10 @@ func CreateStandardDatabase(ctx context.Context, d *schema.ResourceData, meta an
}
}

return append(diags, ReadStandardDatabase(ctx, d, meta)...)
return append(diags, ReadDatabase(ctx, d, meta)...)
}

func UpdateStandardDatabase(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
func UpdateDatabase(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*provider.Context).Client
id := helpers.DecodeSnowflakeID(d.Id()).(sdk.AccountObjectIdentifier)

@@ -326,10 +337,10 @@ func UpdateStandardDatabase(ctx context.Context, d *schema.ResourceData, meta an
}
}

return ReadStandardDatabase(ctx, d, meta)
return ReadDatabase(ctx, d, meta)
}

func ReadStandardDatabase(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
func ReadDatabase(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*provider.Context).Client
id := helpers.DecodeSnowflakeID(d.Id()).(sdk.AccountObjectIdentifier)

@@ -440,7 +451,7 @@ func ReadStandardDatabase(ctx context.Context, d *schema.ResourceData, meta any)
return nil
}

func DeleteStandardDatabase(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
func DeleteDatabase(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*provider.Context).Client
id := helpers.DecodeSnowflakeID(d.Id()).(sdk.AccountObjectIdentifier)

Loading