From 19c51e22448cbd77bb0085ff084df1adcedf0709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Cie=C5=9Blak?= Date: Thu, 16 May 2024 11:53:37 +0200 Subject: [PATCH] wip --- pkg/resources/database.go | 2 +- pkg/sdk/databases.go | 21 ++--- pkg/sdk/testint/databases_integration_test.go | 89 +++++++++++++------ 3 files changed, 69 insertions(+), 43 deletions(-) diff --git a/pkg/resources/database.go b/pkg/resources/database.go index c19c0ca9992..25c686f537a 100644 --- a/pkg/resources/database.go +++ b/pkg/resources/database.go @@ -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 { diff --git a/pkg/sdk/databases.go b/pkg/sdk/databases.go index 1bc0cdcf82f..c49c2169fe0 100644 --- a/pkg/sdk/databases.go +++ b/pkg/sdk/databases.go @@ -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 @@ -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"` diff --git a/pkg/sdk/testint/databases_integration_test.go b/pkg/sdk/testint/databases_integration_test.go index 2982c08a3ab..98c02b4dd25 100644 --- a/pkg/sdk/testint/databases_integration_test.go +++ b/pkg/sdk/testint/databases_integration_test.go @@ -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" @@ -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) @@ -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) @@ -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) @@ -473,14 +482,17 @@ 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) @@ -488,12 +500,10 @@ func TestInt_DatabasesAlter(t *testing.T) { 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) @@ -501,23 +511,49 @@ func TestInt_DatabasesAlter(t *testing.T) { 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) { @@ -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) @@ -658,7 +688,6 @@ func TestInt_DatabasesAlterFailover(t *testing.T) { }, }) require.NoError(t, err) - }) t.Run("promote to primary", func(t *testing.T) { @@ -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) }) @@ -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), @@ -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),