Skip to content

Commit

Permalink
feat: add sequences to sdk (#2351)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-swinkler authored Jan 23, 2024
1 parent 8c03ffb commit d2e5ffd
Show file tree
Hide file tree
Showing 12 changed files with 1,049 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/sdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Client struct {
Roles Roles
RowAccessPolicies RowAccessPolicies
Schemas Schemas
Sequences Sequences
SessionPolicies SessionPolicies
Sessions Sessions
Shares Shares
Expand Down Expand Up @@ -209,6 +210,7 @@ func (c *Client) initialize() {
c.Roles = &roles{client: c}
c.RowAccessPolicies = &rowAccessPolicies{client: c}
c.Schemas = &schemas{client: c}
c.Sequences = &sequences{client: c}
c.SessionPolicies = &sessionPolicies{client: c}
c.Sessions = &sessions{client: c}
c.Shares = &shares{client: c}
Expand Down
11 changes: 11 additions & 0 deletions pkg/sdk/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,17 @@ type Secret struct {
Name string `ddl:"parameter,no_quotes"`
}

type ValuesBehavior string

var (
ValuesBehaviorOrder ValuesBehavior = "ORDER"
ValuesBehaviorNoOrder ValuesBehavior = "NOORDER"
)

func ValuesBehaviorPointer(v ValuesBehavior) *ValuesBehavior {
return &v
}

type Distribution string

var (
Expand Down
1 change: 1 addition & 0 deletions pkg/sdk/poc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var definitionMapping = map[string]*generator.Interface{
"storage_integration_def.go": sdk.StorageIntegrationDef,
"managed_accounts_def.go": sdk.ManagedAccountsDef,
"row_access_policies_def.go": sdk.RowAccessPoliciesDef,
"sequences_def.go": sdk.SequencesDef,
}

func main() {
Expand Down
124 changes: 124 additions & 0 deletions pkg/sdk/sequences_def.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package sdk

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

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

var sequenceSet = g.NewQueryStruct("SequenceSet").
PredefinedQueryStructField("ValuesBehavior", "*ValuesBehavior", g.KeywordOptions()).
OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes())

var sequenceConstraint = g.NewQueryStruct("SequenceConstraint").
OptionalSQL("CASCADE").
OptionalSQL("RESTRICT").
WithValidation(g.ExactlyOneValueSet, "Cascade", "Restrict")

var SequencesDef = g.NewInterface(
"Sequences",
"Sequence",
g.KindOfT[SchemaObjectIdentifier](),
).CreateOperation(
"https://docs.snowflake.com/en/sql-reference/sql/create-sequence",
g.NewQueryStruct("CreateSequence").
Create().
OrReplace().
SQL("SEQUENCE").
IfNotExists().
Name().
OptionalNumberAssignment("START", g.ParameterOptions().NoQuotes()).
OptionalNumberAssignment("INCREMENT", g.ParameterOptions().NoQuotes()).
PredefinedQueryStructField("ValuesBehavior", "*ValuesBehavior", g.KeywordOptions()).
OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()).
WithValidation(g.ValidIdentifier, "name").
WithValidation(g.ConflictingFields, "OrReplace", "IfNotExists"),
).AlterOperation(
"https://docs.snowflake.com/en/sql-reference/sql/alter-sequence",
g.NewQueryStruct("AlterSequence").
Alter().
SQL("SEQUENCE").
IfExists().
Name().
Identifier("RenameTo", g.KindOfTPointer[SchemaObjectIdentifier](), g.IdentifierOptions().SQL("RENAME TO")).
OptionalNumberAssignment("SET INCREMENT", g.ParameterOptions().NoQuotes()).
OptionalQueryStructField(
"Set",
sequenceSet,
g.KeywordOptions().SQL("SET"),
).
OptionalSQL("UNSET COMMENT").
WithValidation(g.ValidIdentifier, "name").
WithValidation(g.ValidIdentifierIfSet, "RenameTo").
WithValidation(g.ExactlyOneValueSet, "RenameTo", "SetIncrement", "Set", "UnsetComment"),
).ShowOperation(
"https://docs.snowflake.com/en/sql-reference/sql/show-sequences",
g.DbStruct("sequenceRow").
Field("created_on", "string").
Field("name", "string").
Field("schema_name", "string").
Field("database_name", "string").
Field("next_value", "int").
Field("interval", "int").
Field("owner", "string").
Field("owner_role_type", "string").
Field("comment", "string").
Field("ordered", "string"),
g.PlainStruct("Sequence").
Field("CreatedOn", "string").
Field("Name", "string").
Field("SchemaName", "string").
Field("DatabaseName", "string").
Field("NextValue", "int").
Field("Interval", "int").
Field("Owner", "string").
Field("OwnerRoleType", "string").
Field("Comment", "string").
Field("Ordered", "bool"),
g.NewQueryStruct("ShowSequences").
Show().
SQL("SEQUENCES").
OptionalLike().
OptionalIn(),
).ShowByIdOperation().DescribeOperation(
g.DescriptionMappingKindSingleValue,
"https://docs.snowflake.com/en/sql-reference/sql/desc-sequence",
g.DbStruct("sequenceDetailRow").
Field("created_on", "string").
Field("name", "string").
Field("schema_name", "string").
Field("database_name", "string").
Field("next_value", "int").
Field("interval", "int").
Field("owner", "string").
Field("owner_role_type", "string").
Field("comment", "string").
Field("ordered", "string"),
g.PlainStruct("SequenceDetail").
Field("CreatedOn", "string").
Field("Name", "string").
Field("SchemaName", "string").
Field("DatabaseName", "string").
Field("NextValue", "int").
Field("Interval", "int").
Field("Owner", "string").
Field("OwnerRoleType", "string").
Field("Comment", "string").
Field("Ordered", "bool"),
g.NewQueryStruct("DescribeSequence").
Describe().
SQL("SEQUENCE").
Name().
WithValidation(g.ValidIdentifier, "name"),
).DropOperation(
"https://docs.snowflake.com/en/sql-reference/sql/drop-sequence",
g.NewQueryStruct("DropSequence").
Drop().
SQL("SEQUENCE").
IfExists().
Name().
OptionalQueryStructField(
"Constraint",
sequenceConstraint,
g.KeywordOptions(),
).
WithValidation(g.ValidIdentifier, "name"),
)
144 changes: 144 additions & 0 deletions pkg/sdk/sequences_dto_builders_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions pkg/sdk/sequences_dto_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package sdk

//go:generate go run ./dto-builder-generator/main.go

var (
_ optionsProvider[CreateSequenceOptions] = new(CreateSequenceRequest)
_ optionsProvider[AlterSequenceOptions] = new(AlterSequenceRequest)
_ optionsProvider[ShowSequenceOptions] = new(ShowSequenceRequest)
_ optionsProvider[DescribeSequenceOptions] = new(DescribeSequenceRequest)
_ optionsProvider[DropSequenceOptions] = new(DropSequenceRequest)
)

type CreateSequenceRequest struct {
OrReplace *bool
IfNotExists *bool
name SchemaObjectIdentifier // required
Start *int
Increment *int
ValuesBehavior *ValuesBehavior
Comment *string
}

type AlterSequenceRequest struct {
IfExists *bool
name SchemaObjectIdentifier // required
RenameTo *SchemaObjectIdentifier
SetIncrement *int
Set *SequenceSetRequest
UnsetComment *bool
}

type SequenceSetRequest struct {
ValuesBehavior *ValuesBehavior
Comment *string
}

type ShowSequenceRequest struct {
Like *Like
In *In
}

type DescribeSequenceRequest struct {
name SchemaObjectIdentifier // required
}

type DropSequenceRequest struct {
IfExists *bool
name SchemaObjectIdentifier // required
Constraint *SequenceConstraintRequest
}

type SequenceConstraintRequest struct {
Cascade *bool
Restrict *bool
}
Loading

0 comments on commit d2e5ffd

Please sign in to comment.