Skip to content

Commit

Permalink
feat: Adjust owner_role_type and schema_evolution_record columns (#2740)
Browse files Browse the repository at this point in the history
- **Support schema evolution record**
- **Support owner role type column**
- **Support owner role type in other views**

<!-- Feel free to delete comments as you fill this in -->

<!-- summary of changes -->

## Test Plan
<!-- detail ways in which this PR has been tested or needs to be tested
-->
* [ ] acceptance tests
<!-- add more below if you think they are relevant -->
* [x] integration tests

## References
<!-- issues documentation links, etc  -->
https://docs.snowflake.com/en/user-guide/data-load-schema-evolution
  • Loading branch information
sfc-gh-jmichalak authored Apr 26, 2024
1 parent eba3029 commit 424e393
Show file tree
Hide file tree
Showing 29 changed files with 284 additions and 126 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ We've included an example env file `test.env.example` with the environment varia

## Advanced Debugging

If you want to build and test the provider locally you should edit you `~.terraformrc` file to include the following:
If you want to build and test the provider locally you should edit your `~/.terraformrc` file to include the following:

```
provider_installation {
Expand Down
4 changes: 4 additions & 0 deletions docs/resources/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ Optional:
- `masking_policy` (String) Masking policy to apply on column. It has to be a fully qualified name.
- `nullable` (Boolean) Whether this column can contain null values. **Note**: Depending on your Snowflake version, the default value will not suffice if this column is used in a primary key constraint.

Read-Only:

- `schema_evolution_record` (String) Record of schema evolution.

<a id="nestedblock--column--default"></a>
### Nested Schema for `column.default`

Expand Down
11 changes: 11 additions & 0 deletions pkg/acceptance/helpers/stage_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,14 @@ func (c *StageClient) PutOnStageWithContent(t *testing.T, id sdk.SchemaObjectIde
require.NoError(t, err)
})
}

func (c *StageClient) CopyIntoTableFromFile(t *testing.T, table, stage sdk.SchemaObjectIdentifier, filename string) {
t.Helper()
ctx := context.Background()

_, err := c.context.client.ExecForTests(ctx, fmt.Sprintf(`COPY INTO %s
FROM @%s/%s
FILE_FORMAT = (type=json)
MATCH_BY_COLUMN_NAME = CASE_INSENSITIVE`, table.FullyQualifiedName(), stage.FullyQualifiedName(), filename))
require.NoError(t, err)
}
1 change: 1 addition & 0 deletions pkg/acceptance/helpers/table_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,5 @@ type InformationSchemaColumns struct {
IdentityCycle sql.NullString `db:"IDENTITY_CYCLE"`
IdentityOrdered sql.NullString `db:"IDENTITY_ORDERED"`
Comment sql.NullString `db:"COMMENT"`
SchemaEvolutionRecord sql.NullString `db:"SCHEMA_EVOLUTION_RECORD"`
}
10 changes: 10 additions & 0 deletions pkg/resources/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ var tableSchema = map[string]*schema.Schema{
Default: "",
Description: "Column collation, e.g. utf8",
},
"schema_evolution_record": {
Type: schema.TypeString,
Computed: true,
Description: "Record of schema evolution.",
},
},
},
},
Expand Down Expand Up @@ -496,6 +501,11 @@ func toColumnConfig(descriptions []sdk.TableColumnDetails) []any {
flat["default"] = []any{def}
}
}

if td.SchemaEvolutionRecord != nil {
flat["schema_evolution_record"] = *td.SchemaEvolutionRecord
}

flattened = append(flattened, flat)
}
return flattened
Expand Down
1 change: 1 addition & 0 deletions pkg/resources/table_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func TestAcc_Table(t *testing.T) {
resource.TestCheckResourceAttr("snowflake_table.test_table", "column.0.type", "VARIANT"),
resource.TestCheckResourceAttr("snowflake_table.test_table", "column.1.name", "column2"),
resource.TestCheckResourceAttr("snowflake_table.test_table", "column.1.comment", ""),
resource.TestCheckResourceAttr("snowflake_table.test_table", "column.1.schema_evolution_record", ""),
resource.TestCheckNoResourceAttr("snowflake_table.test_table", "primary_key.0"),
),
},
Expand Down
54 changes: 31 additions & 23 deletions pkg/sdk/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sdk

import (
"context"
"database/sql"
"errors"
"fmt"
"time"
Expand Down Expand Up @@ -218,35 +219,37 @@ func (v *Alert) ObjectType() ObjectType {
}

type Alert struct {
CreatedOn time.Time
Name string
DatabaseName string
SchemaName string
Owner string
Comment *string
Warehouse string
Schedule string
State AlertState
Condition string
Action string
CreatedOn time.Time
Name string
DatabaseName string
SchemaName string
Owner string
Comment *string
Warehouse string
Schedule string
State AlertState
Condition string
Action string
OwnerRoleType string
}

type alertDBRow struct {
CreatedOn time.Time `db:"created_on"`
Name string `db:"name"`
DatabaseName string `db:"database_name"`
SchemaName string `db:"schema_name"`
Owner string `db:"owner"`
Comment *string `db:"comment"`
Warehouse string `db:"warehouse"`
Schedule string `db:"schedule"`
State string `db:"state"` // suspended, started
Condition string `db:"condition"`
Action string `db:"action"`
CreatedOn time.Time `db:"created_on"`
Name string `db:"name"`
DatabaseName string `db:"database_name"`
SchemaName string `db:"schema_name"`
Owner string `db:"owner"`
Comment *string `db:"comment"`
Warehouse string `db:"warehouse"`
Schedule string `db:"schedule"`
State string `db:"state"` // suspended, started
Condition string `db:"condition"`
Action string `db:"action"`
OwnerRoleType sql.NullString `db:"owner_role_type"`
}

func (row alertDBRow) convert() *Alert {
return &Alert{
alert := &Alert{
CreatedOn: row.CreatedOn,
Name: row.Name,
DatabaseName: row.DatabaseName,
Expand All @@ -259,6 +262,11 @@ func (row alertDBRow) convert() *Alert {
Condition: row.Condition,
Action: row.Action,
}
if row.OwnerRoleType.Valid {
alert.OwnerRoleType = row.OwnerRoleType.String
}

return alert
}

func (opts *ShowAlertOptions) validate() error {
Expand Down
5 changes: 5 additions & 0 deletions pkg/sdk/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Database struct {
DroppedOn time.Time
Transient bool
Kind string
OwnerRoleType string
}

func (v *Database) ID() AccountObjectIdentifier {
Expand All @@ -80,6 +81,7 @@ type databaseRow struct {
ResourceGroup sql.NullString `db:"resource_group"`
DroppedOn sql.NullTime `db:"dropped_on"`
Kind sql.NullString `db:"kind"`
OwnerRoleType sql.NullString `db:"owner_role_type"`
}

func (row databaseRow) convert() *Database {
Expand Down Expand Up @@ -129,6 +131,9 @@ func (row databaseRow) convert() *Database {
if row.Kind.Valid {
database.Kind = row.Kind.String
}
if row.OwnerRoleType.Valid {
database.OwnerRoleType = row.OwnerRoleType.String
}
return database
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/sdk/dynamic_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ type DynamicTable struct {
IsClone bool
IsReplica bool
DataTimestamp time.Time
OwnerRoleType string
}

func (dt *DynamicTable) ID() SchemaObjectIdentifier {
Expand Down Expand Up @@ -153,6 +154,7 @@ type dynamicTableRow struct {
IsClone bool `db:"is_clone"`
IsReplica bool `db:"is_replica"`
DataTimestamp sql.NullTime `db:"data_timestamp"`
OwnerRoleType sql.NullString `db:"owner_role_type"`
}

func (dtr dynamicTableRow) convert() *DynamicTable {
Expand Down Expand Up @@ -185,6 +187,10 @@ func (dtr dynamicTableRow) convert() *DynamicTable {
if dtr.LastSuspendedOn.Valid {
dt.LastSuspendedOn = dtr.LastSuspendedOn.Time
}
if dtr.OwnerRoleType.Valid {
dt.OwnerRoleType = dtr.OwnerRoleType.String
}

return dt
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/sdk/masking_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ type MaskingPolicy struct {
Owner string
Comment string
ExemptOtherPolicies bool
OwnerRoleType string
}

func (v *MaskingPolicy) ID() SchemaObjectIdentifier {
Expand Down Expand Up @@ -267,6 +268,7 @@ func (row maskingPolicyDBRow) convert() *MaskingPolicy {
Owner: row.Owner,
Comment: row.Comment,
ExemptOtherPolicies: exemptOtherPolicies,
OwnerRoleType: row.OwnerRoleType,
}
}

Expand Down
30 changes: 16 additions & 14 deletions pkg/sdk/password_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,14 @@ func (opts *ShowPasswordPolicyOptions) validate() error {

// PasswordPolicy is a user-friendly result for a CREATE PASSWORD POLICY query.
type PasswordPolicy struct {
CreatedOn time.Time
Name string
DatabaseName string
SchemaName string
Kind string
Owner string
Comment string
CreatedOn time.Time
Name string
DatabaseName string
SchemaName string
Kind string
Owner string
Comment string
OwnerRoleType string
}

func (v *PasswordPolicy) ID() SchemaObjectIdentifier {
Expand All @@ -283,13 +284,14 @@ type passwordPolicyDBRow struct {

func (row passwordPolicyDBRow) convert() PasswordPolicy {
return PasswordPolicy{
CreatedOn: row.CreatedOn,
Name: row.Name,
DatabaseName: row.DatabaseName,
SchemaName: row.SchemaName,
Kind: row.Kind,
Owner: row.Owner,
Comment: row.Comment,
CreatedOn: row.CreatedOn,
Name: row.Name,
DatabaseName: row.DatabaseName,
SchemaName: row.SchemaName,
Kind: row.Kind,
Owner: row.Owner,
Comment: row.Comment,
OwnerRoleType: row.OwnerRoleType,
}
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/sdk/session_policies_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ var SessionPoliciesDef = g.NewInterface(
Field("kind", "string").
Field("owner", "string").
Field("comment", "string").
Field("options", "string"),
Field("options", "string").
Field("owner_role_type", "string"),
g.PlainStruct("SessionPolicy").
Field("CreatedOn", "string").
Field("Name", "string").
Expand All @@ -82,7 +83,8 @@ var SessionPoliciesDef = g.NewInterface(
Field("Kind", "string").
Field("Owner", "string").
Field("Comment", "string").
Field("Options", "string"),
Field("Options", "string").
Field("OwnerRoleType", "string"),
g.NewQueryStruct("ShowSessionPolicies").
Show().
SQL("SESSION POLICIES"),
Expand Down
34 changes: 18 additions & 16 deletions pkg/sdk/session_policies_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,27 @@ type ShowSessionPolicyOptions struct {
}

type showSessionPolicyDBRow struct {
CreatedOn string `db:"created_on"`
Name string `db:"name"`
DatabaseName string `db:"database_name"`
SchemaName string `db:"schema_name"`
Kind string `db:"kind"`
Owner string `db:"owner"`
Comment string `db:"comment"`
Options string `db:"options"`
CreatedOn string `db:"created_on"`
Name string `db:"name"`
DatabaseName string `db:"database_name"`
SchemaName string `db:"schema_name"`
Kind string `db:"kind"`
Owner string `db:"owner"`
Comment string `db:"comment"`
Options string `db:"options"`
OwnerRoleType string `db:"owner_role_type"`
}

type SessionPolicy struct {
CreatedOn string
Name string
DatabaseName string
SchemaName string
Kind string
Owner string
Comment string
Options string
CreatedOn string
Name string
DatabaseName string
SchemaName string
Kind string
Owner string
Comment string
Options string
OwnerRoleType string
}

// DescribeSessionPolicyOptions is based on https://docs.snowflake.com/en/sql-reference/sql/desc-session-policy.
Expand Down
17 changes: 9 additions & 8 deletions pkg/sdk/session_policies_impl_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,15 @@ func (r *ShowSessionPolicyRequest) toOpts() *ShowSessionPolicyOptions {

func (r showSessionPolicyDBRow) convert() *SessionPolicy {
return &SessionPolicy{
CreatedOn: r.CreatedOn,
Name: r.Name,
DatabaseName: r.DatabaseName,
SchemaName: r.SchemaName,
Kind: r.Kind,
Owner: r.Owner,
Comment: r.Comment,
Options: r.Options,
CreatedOn: r.CreatedOn,
Name: r.Name,
DatabaseName: r.DatabaseName,
SchemaName: r.SchemaName,
Kind: r.Kind,
Owner: r.Owner,
Comment: r.Comment,
Options: r.Options,
OwnerRoleType: r.OwnerRoleType,
}
}

Expand Down
Loading

0 comments on commit 424e393

Please sign in to comment.