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: Add materialized view to the SDK #2403

Merged
merged 26 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d42b7e6
Start materialized view definition
sfc-gh-asawicki Jan 22, 2024
95c6088
Add materialized view alter definition
sfc-gh-asawicki Jan 22, 2024
fb7a89d
Add materialized view drop definition
sfc-gh-asawicki Jan 22, 2024
51c93af
Add materialized view show definition
sfc-gh-asawicki Jan 22, 2024
9936289
Add materialized view describe definition
sfc-gh-asawicki Jan 22, 2024
49a3e95
Fix def
sfc-gh-asawicki Jan 22, 2024
e0eb4d8
Generate files for materialized views
sfc-gh-asawicki Jan 22, 2024
590419c
Fix name for describe
sfc-gh-asawicki Jan 22, 2024
8f27a12
Adjust show by id
sfc-gh-asawicki Jan 22, 2024
c22c0a3
Pass unit tests for create materialized view (WIP)
sfc-gh-asawicki Jan 22, 2024
b81acbc
Pass unit tests for create materialized view (with regenerate)
sfc-gh-asawicki Jan 22, 2024
b7d6cd0
Pass unit tests for alter materialized view (WIP)
sfc-gh-asawicki Jan 22, 2024
07bf913
Pass unit tests for alter materialized view (WIP - after regeneration)
sfc-gh-asawicki Jan 22, 2024
2a03b53
Pass unit tests for alter materialized view
sfc-gh-asawicki Jan 22, 2024
e035c2e
Pass unit tests for drop materialized view
sfc-gh-asawicki Jan 22, 2024
970947d
Pass unit tests for show materialized views
sfc-gh-asawicki Jan 22, 2024
bbf72c7
Pass unit tests for desc materialized view
sfc-gh-asawicki Jan 22, 2024
78c0c3b
Prepare integration test list
sfc-gh-asawicki Jan 23, 2024
5c5dbb6
Implement show integration tests
sfc-gh-asawicki Jan 23, 2024
2feda2c
Implement describe integration tests
sfc-gh-asawicki Jan 23, 2024
3fa0657
Implement drop integration tests
sfc-gh-asawicki Jan 23, 2024
4fe6daf
Implement create integration tests
sfc-gh-asawicki Jan 23, 2024
f9849cb
Implement half of alter integration tests
sfc-gh-asawicki Jan 23, 2024
0437301
Implement alter integration tests
sfc-gh-asawicki Jan 23, 2024
a1e3fba
Fix linter complaints
sfc-gh-asawicki Jan 23, 2024
c552d5f
Merge branch 'main' into add-materialized-view-to-the-sdk
sfc-gh-asawicki Jan 24, 2024
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
2 changes: 2 additions & 0 deletions pkg/sdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Client struct {
Grants Grants
ManagedAccounts ManagedAccounts
MaskingPolicies MaskingPolicies
MaterializedViews MaterializedViews
NetworkPolicies NetworkPolicies
Parameters Parameters
PasswordPolicies PasswordPolicies
Expand Down Expand Up @@ -202,6 +203,7 @@ func (c *Client) initialize() {
c.Grants = &grants{client: c}
c.ManagedAccounts = &managedAccounts{client: c}
c.MaskingPolicies = &maskingPolicies{client: c}
c.MaterializedViews = &materializedViews{client: c}
c.NetworkPolicies = &networkPolicies{client: c}
c.Parameters = &parameters{client: c}
c.PasswordPolicies = &passwordPolicies{client: c}
Expand Down
189 changes: 189 additions & 0 deletions pkg/sdk/materialized_views_def.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package sdk

import g "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator"

//go:generate go run ./poc/main.go

var materializedViewColumn = g.NewQueryStruct("MaterializedViewColumn").
Text("Name", g.KeywordOptions().DoubleQuotes().Required()).
OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes().NoEquals())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does column actually allow comment? im having trouble finding it in the docs

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add this to the questions about the docs I post on doc-discuss.

You are right. It's not written in materialized view docs but in regular view ones. Also, there is a test proving that it can be done: https://github.com/Snowflake-Labs/terraform-provider-snowflake/pull/2403/files#diff-7a8a3cc00d490b622d152a5bdf42e0915fff497c2eb4554f491dfaaaa3d51025R124.


var materializedViewColumnMaskingPolicy = g.NewQueryStruct("MaterializedViewColumnMaskingPolicy").
Text("Name", g.KeywordOptions().Required()).
Identifier("MaskingPolicy", g.KindOfT[SchemaObjectIdentifier](), g.IdentifierOptions().SQL("MASKING POLICY").Required()).
NamedListWithParens("USING", g.KindOfT[string](), nil). // TODO: double quotes here?
OptionalTags()

var materializedViewRowAccessPolicy = g.NewQueryStruct("MaterializedViewRowAccessPolicy").
Identifier("RowAccessPolicy", g.KindOfT[SchemaObjectIdentifier](), g.IdentifierOptions().SQL("ROW ACCESS POLICY").Required()).
NamedListWithParens("ON", g.KindOfT[string](), g.KeywordOptions().Required()). // TODO: double quotes here?
WithValidation(g.ValidIdentifier, "RowAccessPolicy").
WithValidation(g.ValidateValueSet, "On")

var materializedViewClusterByExpression = g.NewQueryStruct("MaterializedViewClusterByExpression").
Text("Name", g.KeywordOptions().DoubleQuotes().Required())

var materializedViewClusterBy = g.NewQueryStruct("MaterializedViewClusterBy").
SQL("CLUSTER BY").
ListQueryStructField("Expressions", materializedViewClusterByExpression, g.ListOptions().Parentheses()).
WithValidation(g.ValidateValueSet, "Expressions")

var materializedViewSet = g.NewQueryStruct("MaterializedViewSet").
OptionalSQL("SECURE").
OptionalComment().
WithValidation(g.ExactlyOneValueSet, "Secure", "Comment")

var materializedViewUnset = g.NewQueryStruct("MaterializedViewUnset").
OptionalSQL("SECURE").
OptionalSQL("COMMENT").
WithValidation(g.ExactlyOneValueSet, "Secure", "Comment")

var materializedViewDbRow = g.DbStruct("materializedViewDBRow").
Text("created_on").
Text("name").
OptionalText("reserved").
Text("database_name").
Text("schema_name").
OptionalText("cluster_by").
Number("rows").
Number("bytes").
Text("source_database_name").
Text("source_schema_name").
Text("source_table_name").
Time("refreshed_on").
Time("compacted_on").
Text("owner").
Bool("invalid").
OptionalText("invalid_reason").
Text("behind_by").
OptionalText("comment").
Text("text").
Bool("is_secure").
Text("automatic_clustering").
OptionalText("owner_role_type").
OptionalText("budget")

var materializedView = g.PlainStruct("MaterializedView").
Text("CreatedOn").
Text("Name").
OptionalText("Reserved").
Text("DatabaseName").
Text("SchemaName").
Text("ClusterBy").
Number("Rows").
Number("Bytes").
Text("SourceDatabaseName").
Text("SourceSchemaName").
Text("SourceTableName").
Time("RefreshedOn").
Time("CompactedOn").
Text("Owner").
Bool("Invalid").
Text("InvalidReason").
Text("BehindBy").
Text("Comment").
Text("Text").
Bool("IsSecure").
Bool("AutomaticClustering").
Text("OwnerRoleType").
Text("Budget")

var materializedViewDetailsDbRow = g.DbStruct("materializedViewDetailsRow").
Text("name").
Field("type", "DataType").
Text("kind").
Text("null").
OptionalText("default").
Text("primary key").
Text("unique key").
OptionalText("check").
OptionalText("expression").
OptionalText("comment")

var materializedViewDetails = g.PlainStruct("MaterializedViewDetails").
Text("Name").
Field("Type", "DataType").
Text("Kind").
Bool("IsNullable").
OptionalText("Default").
Bool("IsPrimary").
Bool("IsUnique").
OptionalBool("Check").
OptionalText("Expression").
OptionalText("Comment")

var MaterializedViewsDef = g.NewInterface(
"MaterializedViews",
"MaterializedView",
g.KindOfT[SchemaObjectIdentifier](),
).
CreateOperation(
"https://docs.snowflake.com/en/sql-reference/sql/create-materialized-view",
g.NewQueryStruct("CreateMaterializedView").
Create().
OrReplace().
OptionalSQL("SECURE").
SQL("MATERIALIZED VIEW").
IfNotExists().
Name().
OptionalCopyGrants().
ListQueryStructField("Columns", materializedViewColumn, g.ListOptions().Parentheses()).
ListQueryStructField("ColumnsMaskingPolicies", materializedViewColumnMaskingPolicy, g.ListOptions().NoParentheses().NoEquals()).
OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()).
OptionalQueryStructField("RowAccessPolicy", materializedViewRowAccessPolicy, g.KeywordOptions()).
OptionalTags().
OptionalQueryStructField("ClusterBy", materializedViewClusterBy, g.KeywordOptions()).
SQL("AS").
Text("sql", g.KeywordOptions().NoQuotes().Required()).
WithValidation(g.ValidIdentifier, "name").
WithValidation(g.ConflictingFields, "OrReplace", "IfNotExists"),
).
AlterOperation(
"https://docs.snowflake.com/en/sql-reference/sql/alter-materialized-view",
g.NewQueryStruct("AlterMaterializedView").
Alter().
SQL("MATERIALIZED VIEW").
Name().
OptionalIdentifier("RenameTo", g.KindOfT[SchemaObjectIdentifier](), g.IdentifierOptions().SQL("RENAME TO")).
OptionalQueryStructField("ClusterBy", materializedViewClusterBy, g.KeywordOptions()).
OptionalSQL("DROP CLUSTERING KEY").
OptionalSQL("SUSPEND RECLUSTER").
OptionalSQL("RESUME RECLUSTER").
OptionalSQL("SUSPEND").
OptionalSQL("RESUME").
OptionalQueryStructField("Set", materializedViewSet, g.KeywordOptions().SQL("SET")).
OptionalQueryStructField("Unset", materializedViewUnset, g.KeywordOptions().SQL("UNSET")).
WithValidation(g.ValidIdentifier, "name").
WithValidation(g.ExactlyOneValueSet, "RenameTo", "ClusterBy", "DropClusteringKey", "SuspendRecluster", "ResumeRecluster", "Suspend", "Resume", "Set", "Unset"),
).
DropOperation(
"https://docs.snowflake.com/en/sql-reference/sql/drop-materialized-view",
g.NewQueryStruct("DropMaterializedView").
Drop().
SQL("MATERIALIZED VIEW").
IfExists().
Name().
WithValidation(g.ValidIdentifier, "name"),
).
ShowOperation(
"https://docs.snowflake.com/en/sql-reference/sql/show-materialized-views",
materializedViewDbRow,
materializedView,
g.NewQueryStruct("ShowMaterializedViews").
Show().
SQL("MATERIALIZED VIEWS").
OptionalLike().
OptionalIn(),
).
ShowByIdOperation().
DescribeOperation(
g.DescriptionMappingKindSlice,
"https://docs.snowflake.com/en/sql-reference/sql/desc-materialized-view",
materializedViewDetailsDbRow,
materializedViewDetails,
g.NewQueryStruct("DescribeMaterializedView").
Describe().
SQL("MATERIALIZED VIEW").
Name().
WithValidation(g.ValidIdentifier, "name"),
)
Loading
Loading