Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak committed May 16, 2024
1 parent 152120a commit 19c51e2
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 43 deletions.
2 changes: 1 addition & 1 deletion pkg/resources/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func UpdateDatabase(d *schema.ResourceData, meta interface{}) error {
if d.HasChange("name") {
newName := d.Get("name").(string)
opts := &sdk.AlterDatabaseOptions{
NewName: sdk.NewAccountObjectIdentifier(newName),
NewName: sdk.Pointer(sdk.NewAccountObjectIdentifier(newName)),
}
err := client.Databases.Alter(ctx, id, opts)
if err != nil {
Expand Down
21 changes: 6 additions & 15 deletions pkg/sdk/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ var (
_ validatable = new(describeDatabaseOptions)
)

// TODO: What should I do with clone
// TODO: Test new values (unit and int)
// Modified:
// - [ ] Create
// - Create from share - everything minus Transient and Data_retention option
// - Create as replica of - everything as in Create

type Databases interface {
Create(ctx context.Context, id AccountObjectIdentifier, opts *CreateDatabaseOptions) error
CreateShared(ctx context.Context, id AccountObjectIdentifier, shareID ExternalObjectIdentifier, opts *CreateSharedDatabaseOptions) error
Expand Down Expand Up @@ -206,14 +199,12 @@ func (v *databases) Create(ctx context.Context, id AccountObjectIdentifier, opts

// CreateSharedDatabaseOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-database.
type CreateSharedDatabaseOptions struct {
create bool `ddl:"static" sql:"CREATE"`
OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"`
database bool `ddl:"static" sql:"DATABASE"`
IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"`
name AccountObjectIdentifier `ddl:"identifier"`
fromShare ExternalObjectIdentifier `ddl:"identifier" sql:"FROM SHARE"`
// TODO: Can be used but is not returned in the `show parameters for database` and can't be altered
// MaxDataExtensionTimeInDays *int `ddl:"parameter" sql:"MAX_DATA_EXTENSION_TIME_IN_DAYS"`
create bool `ddl:"static" sql:"CREATE"`
OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"`
database bool `ddl:"static" sql:"DATABASE"`
IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"`
name AccountObjectIdentifier `ddl:"identifier"`
fromShare ExternalObjectIdentifier `ddl:"identifier" sql:"FROM SHARE"`
ExternalVolume *AccountObjectIdentifier `ddl:"identifier,equals" sql:"EXTERNAL_VOLUME"`
Catalog *AccountObjectIdentifier `ddl:"identifier,equals" sql:"CATALOG"`
DefaultDDLCollation *string `ddl:"parameter,single_quotes" sql:"DEFAULT_DDL_COLLATION"`
Expand Down
89 changes: 62 additions & 27 deletions pkg/sdk/testint/databases_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package testint

import (
"fmt"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/collections"
"testing"

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

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -301,7 +302,6 @@ func TestInt_DatabasesCreateSecondary(t *testing.T) {
assert.Equal(t, string(sdk.TraceLevelAlways), traceLevelParam.Value)
}

// TODO: Other database types
func TestInt_DatabasesAlter(t *testing.T) {
client := testClient(t)
secondaryClient := testSecondaryClient(t)
Expand Down Expand Up @@ -412,10 +412,15 @@ func TestInt_DatabasesAlter(t *testing.T) {
t.Cleanup(testClientHelper().Database.DropDatabaseFunc(t, newName))

database, err := client.Databases.ShowByID(ctx, newName)
require.NoError(t, err)
assert.Equal(t, newName.Name(), database.Name)
})

t.Run(fmt.Sprintf("Database: %s - setting and unsetting log_level and trace_level", testCase.DatabaseType), func(t *testing.T) {
if testCase.DatabaseType == "From Share" {
t.Skipf("Skipping database test because from share is not supported")
}

databaseTest, databaseTestCleanup := testCase.CreateFn(t)
t.Cleanup(databaseTestCleanup)

Expand Down Expand Up @@ -443,6 +448,10 @@ func TestInt_DatabasesAlter(t *testing.T) {
})

t.Run(fmt.Sprintf("Database: %s - setting and unsetting external volume and catalog", testCase.DatabaseType), func(t *testing.T) {
if testCase.DatabaseType == "From Share" {
t.Skipf("Skipping database test because from share is not supported")
}

databaseTest, databaseTestCleanup := testCase.CreateFn(t)
t.Cleanup(databaseTestCleanup)

Expand Down Expand Up @@ -473,51 +482,78 @@ func TestInt_DatabasesAlter(t *testing.T) {
require.Empty(t, queryParameterValueForDatabase(t, databaseTest.ID(), sdk.ObjectParameterCatalog))
})

t.Run(fmt.Sprintf("Database: %s - setting and unsetting retention time + comment", testCase.DatabaseType), func(t *testing.T) {
t.Run(fmt.Sprintf("Database: %s - setting and unsetting retention time", testCase.DatabaseType), func(t *testing.T) {
if testCase.DatabaseType == "From Share" {
t.Skipf("Skipping database test because from share is not supported")
}

databaseTest, databaseTestCleanup := testCase.CreateFn(t)
t.Cleanup(databaseTestCleanup)

err := client.Databases.Alter(ctx, databaseTest.ID(), &sdk.AlterDatabaseOptions{
Set: &sdk.DatabaseSet{
DataRetentionTimeInDays: sdk.Int(42),
Comment: sdk.String("test comment"),
},
})
require.NoError(t, err)

database, err := client.Databases.ShowByID(ctx, databaseTest.ID())
require.NoError(t, err)
assert.Equal(t, 42, database.RetentionTime)
assert.Equal(t, "test comment", database.Comment)

err = client.Databases.Alter(ctx, databaseTest.ID(), &sdk.AlterDatabaseOptions{
Unset: &sdk.DatabaseUnset{
DataRetentionTimeInDays: sdk.Bool(true),
Comment: sdk.Bool(true),
},
})
require.NoError(t, err)

database, err = client.Databases.ShowByID(ctx, databaseTest.ID())
require.NoError(t, err)
assert.NotEqual(t, 42, database.RetentionTime)
assert.Equal(t, "", database.Comment)
})
}

t.Run("swap with another database", func(t *testing.T) {
databaseTest, databaseCleanup := testClientHelper().Database.CreateDatabase(t)
t.Cleanup(databaseCleanup)
t.Run(fmt.Sprintf("Database: %s - setting and unsetting comment", testCase.DatabaseType), func(t *testing.T) {
databaseTest, databaseTestCleanup := testCase.CreateFn(t)
t.Cleanup(databaseTestCleanup)

err := client.Databases.Alter(ctx, databaseTest.ID(), &sdk.AlterDatabaseOptions{
Set: &sdk.DatabaseSet{
Comment: sdk.String("test comment"),
},
})
require.NoError(t, err)

databaseTest2, databaseCleanup2 := testClientHelper().Database.CreateDatabase(t)
t.Cleanup(databaseCleanup2)
database, err := client.Databases.ShowByID(ctx, databaseTest.ID())
require.NoError(t, err)

assert.Equal(t, "test comment", database.Comment)

err = client.Databases.Alter(ctx, databaseTest.ID(), &sdk.AlterDatabaseOptions{
Unset: &sdk.DatabaseUnset{
Comment: sdk.Bool(true),
},
})
require.NoError(t, err)

err := client.Databases.Alter(ctx, databaseTest.ID(), &sdk.AlterDatabaseOptions{
SwapWith: sdk.Pointer(databaseTest2.ID()),
database, err = client.Databases.ShowByID(ctx, databaseTest.ID())
require.NoError(t, err)
assert.Equal(t, "", database.Comment)
})
require.NoError(t, err)
})

t.Run(fmt.Sprintf("Database: %s - swap with another database", testCase.DatabaseType), func(t *testing.T) {
databaseTest, databaseTestCleanup := testCase.CreateFn(t)
t.Cleanup(databaseTestCleanup)

databaseTest2, databaseCleanup2 := testClientHelper().Database.CreateDatabase(t)
t.Cleanup(databaseCleanup2)

err := client.Databases.Alter(ctx, databaseTest.ID(), &sdk.AlterDatabaseOptions{
SwapWith: sdk.Pointer(databaseTest2.ID()),
})
require.NoError(t, err)
})
}
}

func TestInt_DatabasesAlterReplication(t *testing.T) {
Expand Down Expand Up @@ -548,12 +584,6 @@ func TestInt_DatabasesAlterReplication(t *testing.T) {
})

t.Run("refresh replicated database", func(t *testing.T) {
// TODO(SNOW-1348346): implement once ReplicationGroups are supported.
//err := testClient(t).Databases.AlterReplication(ctx, database.ID(), &sdk.AlterDatabaseReplicationOptions{
// Refresh: sdk.Bool(true),
//})
//require.NoError(t, err)

client := testClient(t)
secondaryClient := testSecondaryClient(t)
ctx := testContext(t)
Expand Down Expand Up @@ -658,7 +688,6 @@ func TestInt_DatabasesAlterFailover(t *testing.T) {
},
})
require.NoError(t, err)

})

t.Run("promote to primary", func(t *testing.T) {
Expand Down Expand Up @@ -698,7 +727,9 @@ func TestInt_DatabasesDrop(t *testing.T) {
ctx := testContext(t)

t.Run("drop with nil options", func(t *testing.T) {
databaseTest, _ := testClientHelper().Database.CreateDatabase(t)
databaseTest, databaseTestCleanup := testClientHelper().Database.CreateDatabase(t)
t.Cleanup(databaseTestCleanup)

err := client.Databases.Drop(ctx, databaseTest.ID(), nil)
require.NoError(t, err)
})
Expand All @@ -712,7 +743,9 @@ func TestInt_DatabasesDrop(t *testing.T) {
})

t.Run("drop with cascade", func(t *testing.T) {
databaseTest, _ := testClientHelper().Database.CreateDatabase(t)
databaseTest, databaseTestCleanup := testClientHelper().Database.CreateDatabase(t)
t.Cleanup(databaseTestCleanup)

err := client.Databases.Drop(ctx, databaseTest.ID(), &sdk.DropDatabaseOptions{
IfExists: sdk.Bool(true),
Cascade: sdk.Bool(true),
Expand All @@ -721,7 +754,9 @@ func TestInt_DatabasesDrop(t *testing.T) {
})

t.Run("drop with restrict", func(t *testing.T) {
databaseTest, _ := testClientHelper().Database.CreateDatabase(t)
databaseTest, databaseTestCleanup := testClientHelper().Database.CreateDatabase(t)
t.Cleanup(databaseTestCleanup)

err := client.Databases.Drop(ctx, databaseTest.ID(), &sdk.DropDatabaseOptions{
IfExists: sdk.Bool(true),
Restrict: sdk.Bool(true),
Expand Down

0 comments on commit 19c51e2

Please sign in to comment.