Skip to content

Commit

Permalink
Apply PR feedback on DT initialize/refreshMode
Browse files Browse the repository at this point in the history
* Use constants in all places where RefreshMode and Initialize are
  passed around as arguments
* Remove checks for unchanged values between acceptance test steps
* Update docs to include the AUTO option for refresh_mode
  • Loading branch information
sonya committed Feb 14, 2024
1 parent bd98940 commit ef96b3b
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 48 deletions.
4 changes: 2 additions & 2 deletions docs/resources/dynamic_table.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ resource "snowflake_dynamic_table" "dt" {

- `comment` (String) Specifies a comment for the dynamic table.
- `or_replace` (Boolean) Specifies whether to replace the dynamic table if it already exists.
- `refresh_mode` (String) INCREMENTAL if the dynamic table will use incremental refreshes, or FULL if it will recompute the whole table on every refresh.
- `initialize` (String) Specifies the behavior of the initial refresh of the dynamic table. This property cannot be altered after you create the dynamic table.
- `refresh_mode` (String) INCREMENTAL if the dynamic table will use incremental refreshes, or FULL if it will recompute the whole table on every refresh. Specify AUTO to let Snowflake decide. The default is AUTO.
- `initialize` (String) Specifies the behavior of the initial refresh of the dynamic table. This property cannot be altered after you create the dynamic table. Specify ON_CREATE to initialize the dynamic table immeidately, or ON_SCHEDULE to have it initialize at the next tick after creation. The default os ON_CREATE.

### Read-Only

Expand Down
4 changes: 2 additions & 2 deletions pkg/resources/dynamic_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,10 @@ func CreateDynamicTable(d *schema.ResourceData, meta interface{}) error {
request.WithOrReplace(true)
}
if v, ok := d.GetOk("refresh_mode"); ok {
request.WithRefreshMode(sdk.String(v.(string)))
request.WithRefreshMode(sdk.DynamicTableRefreshMode(v.(string)))
}
if v, ok := d.GetOk("initialize"); ok {
request.WithInitialize(sdk.String(v.(string)))
request.WithInitialize(sdk.DynamicTableInitialize(v.(string)))
}
if err := client.DynamicTables.Create(context.Background(), request); err != nil {
return err
Expand Down
29 changes: 8 additions & 21 deletions pkg/resources/dynamic_table_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ func TestAcc_DynamicTable_basic(t *testing.T) {
variableSet2["comment"] = config.StringVariable("Terraform acceptance test - updated")

variableSet3 := m()
variableSet3["initialize"] = config.StringVariable("ON_SCHEDULE")
variableSet3["initialize"] = config.StringVariable(string(sdk.DynamicTableInitializeOnSchedule))

variableSet4 := m()
variableSet4["initialize"] = config.StringVariable("ON_SCHEDULE") // keep the same setting from set 3
variableSet4["refresh_mode"] = config.StringVariable("FULL")
variableSet4["initialize"] = config.StringVariable(string(sdk.DynamicTableInitializeOnSchedule)) // keep the same setting from set 3
variableSet4["refresh_mode"] = config.StringVariable(string(sdk.DynamicTableRefreshModeFull))

// used to check whether a dynamic table was replaced
var createdOn string
Expand All @@ -63,8 +63,8 @@ func TestAcc_DynamicTable_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "database", acc.TestDatabaseName),
resource.TestCheckResourceAttr(resourceName, "schema", acc.TestSchemaName),
resource.TestCheckResourceAttr(resourceName, "warehouse", acc.TestWarehouseName),
resource.TestCheckResourceAttr(resourceName, "initialize", "ON_CREATE"),
resource.TestCheckResourceAttr(resourceName, "refresh_mode", "AUTO"),
resource.TestCheckResourceAttr(resourceName, "initialize", string(sdk.DynamicTableInitializeOnCreate)),
resource.TestCheckResourceAttr(resourceName, "refresh_mode", string(sdk.DynamicTableRefreshModeAuto)),
resource.TestCheckResourceAttr(resourceName, "target_lag.#", "1"),
resource.TestCheckResourceAttr(resourceName, "target_lag.0.maximum_duration", "2 minutes"),
resource.TestCheckResourceAttr(resourceName, "query", fmt.Sprintf("select \"id\" from \"%v\".\"%v\".\"%v\"", acc.TestDatabaseName, acc.TestSchemaName, tableName)),
Expand Down Expand Up @@ -118,14 +118,7 @@ func TestAcc_DynamicTable_basic(t *testing.T) {
ConfigDirectory: config.TestStepDirectory(),
ConfigVariables: variableSet3,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "database", acc.TestDatabaseName),
resource.TestCheckResourceAttr(resourceName, "schema", acc.TestSchemaName),
resource.TestCheckResourceAttr(resourceName, "initialize", "ON_SCHEDULE"),
resource.TestCheckResourceAttr(resourceName, "refresh_mode", "AUTO"),
resource.TestCheckResourceAttr(resourceName, "target_lag.#", "1"),
resource.TestCheckResourceAttr(resourceName, "target_lag.0.downstream", "true"),
resource.TestCheckResourceAttr(resourceName, "comment", "Terraform acceptance test"),
resource.TestCheckResourceAttr(resourceName, "initialize", string(sdk.DynamicTableInitializeOnSchedule)),

resource.TestCheckResourceAttrWith(resourceName, "created_on", func(value string) error {
if value == createdOn {
Expand All @@ -141,14 +134,8 @@ func TestAcc_DynamicTable_basic(t *testing.T) {
ConfigDirectory: config.TestStepDirectory(),
ConfigVariables: variableSet4,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "database", acc.TestDatabaseName),
resource.TestCheckResourceAttr(resourceName, "schema", acc.TestSchemaName),
resource.TestCheckResourceAttr(resourceName, "initialize", "ON_SCHEDULE"),
resource.TestCheckResourceAttr(resourceName, "refresh_mode", "FULL"),
resource.TestCheckResourceAttr(resourceName, "target_lag.#", "1"),
resource.TestCheckResourceAttr(resourceName, "target_lag.0.downstream", "true"),
resource.TestCheckResourceAttr(resourceName, "comment", "Terraform acceptance test"),
resource.TestCheckResourceAttr(resourceName, "initialize", string(sdk.DynamicTableInitializeOnSchedule)),
resource.TestCheckResourceAttr(resourceName, "refresh_mode", string(sdk.DynamicTableRefreshModeFull)),

resource.TestCheckResourceAttrWith(resourceName, "created_on", func(value string) error {
if value == createdOn {
Expand Down
28 changes: 18 additions & 10 deletions pkg/sdk/dynamic_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ type DynamicTables interface {

// createDynamicTableOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-dynamic-table
type createDynamicTableOptions struct {
create bool `ddl:"static" sql:"CREATE"`
OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"`
dynamicTable bool `ddl:"static" sql:"DYNAMIC TABLE"`
name SchemaObjectIdentifier `ddl:"identifier"`
targetLag TargetLag `ddl:"parameter,no_quotes" sql:"TARGET_LAG"`
Initialize *string `ddl:"parameter,no_quotes" sql:"INITIALIZE"`
RefreshMode *string `ddl:"parameter,no_quotes" sql:"REFRESH_MODE"`
warehouse AccountObjectIdentifier `ddl:"identifier,equals" sql:"WAREHOUSE"`
Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"`
query string `ddl:"parameter,no_equals,no_quotes" sql:"AS"`
create bool `ddl:"static" sql:"CREATE"`
OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"`
dynamicTable bool `ddl:"static" sql:"DYNAMIC TABLE"`
name SchemaObjectIdentifier `ddl:"identifier"`
targetLag TargetLag `ddl:"parameter,no_quotes" sql:"TARGET_LAG"`
Initialize *DynamicTableInitialize `ddl:"parameter,no_quotes" sql:"INITIALIZE"`
RefreshMode *DynamicTableRefreshMode `ddl:"parameter,no_quotes" sql:"REFRESH_MODE"`
warehouse AccountObjectIdentifier `ddl:"identifier,equals" sql:"WAREHOUSE"`
Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"`
query string `ddl:"parameter,no_equals,no_quotes" sql:"AS"`
}

type TargetLag struct {
Expand Down Expand Up @@ -76,6 +76,10 @@ const (
DynamicTableRefreshModeFull DynamicTableRefreshMode = "FULL"
)

func (d DynamicTableRefreshMode) ToPointer() *DynamicTableRefreshMode {
return &d
}

var AllDynamicRefreshModes = []DynamicTableRefreshMode{DynamicTableRefreshModeAuto, DynamicTableRefreshModeIncremental, DynamicTableRefreshModeFull}

type DynamicTableInitialize string
Expand All @@ -85,6 +89,10 @@ const (
DynamicTableInitializeOnSchedule DynamicTableInitialize = "ON_SCHEDULE"
)

func (d DynamicTableInitialize) ToPointer() *DynamicTableInitialize {
return &d
}

var AllDynamicTableInitializes = []DynamicTableInitialize{DynamicTableInitializeOnCreate, DynamicTableInitializeOnSchedule}

type DynamicTableSchedulingState string
Expand Down
4 changes: 2 additions & 2 deletions pkg/sdk/dynamic_table_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type CreateDynamicTableRequest struct {
query string // required

comment *string
refreshMode *string
initialize *string
refreshMode *DynamicTableRefreshMode
initialize *DynamicTableInitialize
}

type AlterDynamicTableRequest struct {
Expand Down
8 changes: 4 additions & 4 deletions pkg/sdk/dynamic_table_dto_builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ func (s *CreateDynamicTableRequest) WithComment(comment *string) *CreateDynamicT
return s
}

func (s *CreateDynamicTableRequest) WithRefreshMode(refreshMode *string) *CreateDynamicTableRequest {
s.refreshMode = refreshMode
func (s *CreateDynamicTableRequest) WithRefreshMode(refreshMode DynamicTableRefreshMode) *CreateDynamicTableRequest {
s.refreshMode = &refreshMode
return s
}

func (s *CreateDynamicTableRequest) WithInitialize(initialize *string) *CreateDynamicTableRequest {
s.initialize = initialize
func (s *CreateDynamicTableRequest) WithInitialize(initialize DynamicTableInitialize) *CreateDynamicTableRequest {
s.initialize = &initialize
return s
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/sdk/dynamic_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func TestDynamicTableCreate(t *testing.T) {
opts := defaultOpts()
opts.OrReplace = Bool(true)
opts.Comment = String("comment")
opts.RefreshMode = String("FULL")
opts.Initialize = String("ON_SCHEDULE")
opts.RefreshMode = DynamicTableRefreshModeFull.ToPointer()
opts.Initialize = DynamicTableInitializeOnSchedule.ToPointer()
assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE DYNAMIC TABLE %s TARGET_LAG = '1 minutes' INITIALIZE = ON_SCHEDULE REFRESH_MODE = FULL WAREHOUSE = "warehouse_name" COMMENT = 'comment' AS SELECT product_id, product_name FROM staging_table`, id.FullyQualifiedName())
})
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/sdk/testint/dynamic_table_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestInt_DynamicTableCreateAndDrop(t *testing.T) {
require.Equal(t, name.Name(), entity.Name)
require.Equal(t, testWarehouse(t).ID().Name(), entity.Warehouse)
require.Equal(t, "DOWNSTREAM", entity.TargetLag)
require.Equal(t, "INCREMENTAL", entity.RefreshMode)
require.Equal(t, sdk.DynamicTableRefreshModeIncremental, entity.RefreshMode)
require.Contains(t, entity.Text, "initialize = 'ON_SCHEDULE'")
require.Contains(t, entity.Text, "refresh_mode = 'AUTO'")
})
Expand All @@ -80,9 +80,9 @@ func TestInt_DynamicTableCreateAndDrop(t *testing.T) {
}
query := "select id from " + tableTest.ID().FullyQualifiedName()
comment := random.Comment()
refreshMode := "FULL"
initialize := "ON_SCHEDULE"
err := client.DynamicTables.Create(ctx, sdk.NewCreateDynamicTableRequest(name, testWarehouse(t).ID(), targetLag, query).WithOrReplace(true).WithInitialize(&initialize).WithRefreshMode(&refreshMode).WithComment(&comment))
refreshMode := sdk.DynamicTableRefreshModeFull
initialize := sdk.DynamicTableInitializeOnSchedule
err := client.DynamicTables.Create(ctx, sdk.NewCreateDynamicTableRequest(name, testWarehouse(t).ID(), targetLag, query).WithOrReplace(true).WithInitialize(initialize).WithRefreshMode(refreshMode).WithComment(&comment))
require.NoError(t, err)
t.Cleanup(func() {
err = client.DynamicTables.Drop(ctx, sdk.NewDropDynamicTableRequest(name))
Expand All @@ -96,7 +96,7 @@ func TestInt_DynamicTableCreateAndDrop(t *testing.T) {
require.Equal(t, name.Name(), entity.Name)
require.Equal(t, testWarehouse(t).ID().Name(), entity.Warehouse)
require.Equal(t, "DOWNSTREAM", entity.TargetLag)
require.Equal(t, "FULL", entity.RefreshMode)
require.Equal(t, sdk.DynamicTableRefreshModeFull, entity.RefreshMode)
require.Contains(t, entity.Text, "initialize = 'ON_SCHEDULE'")
require.Contains(t, entity.Text, "refresh_mode = 'FULL'")
})
Expand Down

0 comments on commit ef96b3b

Please sign in to comment.