Skip to content

Commit

Permalink
feat: use sequence from sdk and add ordering attr (Snowflake-Labs#2419)
Browse files Browse the repository at this point in the history
removes old code from snowflake package, replacing with SDK package.
does not change any existing functionality.

closes Snowflake-Labs#2387
  • Loading branch information
sfc-gh-swinkler authored Jan 31, 2024
1 parent e8915cc commit 973b8f7
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 466 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
> ⚠️ **Please note**: If you believe you have found a security issue, _please responsibly disclose_ by contacting us at [team-cloud-foundation-tools-dl@snowflake.com](mailto:team-cloud-foundation-tools-dl@snowflake.com).
> ⚠️ **Disclaimer**: the project is still in the 0.x.x version, which means it’s still in the experimental phase (check [Go module versioning](https://go.dev/doc/modules/version-numbers#v0-number) for more details). It can be used in production but makes no stability or backward compatibility guarantees. We do not provide backward bug fixes and, therefore, always suggest using the newest version. We are providing only limited support for the provider; priorities will be assigned on a case-by-case basis.
>
> Our main current goals are stabilization, addressing existing issues, and providing the missing features (prioritizing the GA features; supporting PrPr and PuPr features are not high priorities now).
>
> Our main current goals are stabilization, addressing existing issues, and providing the missing features (prioritizing the GA features; supporting PrPr and PuPr features are not high priorities now).
>
> With all that in mind, we aim to reach V1 with a stable, reliable, and functional provider. V1 will be free of all the above limitations.
Expand Down Expand Up @@ -107,7 +107,7 @@ Migration status - indicates if given resource / datasource is using new SDK.
| External Table || snowflake_external_table | snowflake_external_table ||
| View || snowflake_view | snowflake_view ||
| Materialized View | 👨‍💻 | snowflake_materialized_view | snowflake_materialized_view ||
| Sequence | 👨‍💻 | snowflake_sequence | snowflake_sequence | |
| Sequence | | snowflake_sequence | snowflake_sequence | |
| Function || snowflake_function | snowflake_function ||
| External Function || snowflake_external_function | snowflake_external_function ||
| Stored Procedure || snowflake_procedure | snowflake_procedure ||
Expand Down
3 changes: 2 additions & 1 deletion docs/resources/sequence.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ resource "snowflake_sequence" "test_sequence" {

- `comment` (String) Specifies a comment for the sequence.
- `increment` (Number) The amount the sequence will increase by each time it is used
- `ordering` (String) The ordering of the sequence. Either ORDER or NOORDER. Default is ORDER.

### Read-Only

- `fully_qualified_name` (String) The fully qualified name of the sequence.
- `id` (String) The ID of this resource.
- `next_value` (Number) The next value the sequence will provide.
- `next_value` (Number) The increment sequence interval.

## Import

Expand Down
36 changes: 15 additions & 21 deletions pkg/datasources/sequences.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package datasources

import (
"context"
"database/sql"
"errors"
"fmt"
"log"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/snowflake"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -58,30 +57,25 @@ func Sequences() *schema.Resource {

func ReadSequences(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
client := sdk.NewClientFromDB(db)
ctx := context.Background()
databaseName := d.Get("database").(string)
schemaName := d.Get("schema").(string)

currentSequences, err := snowflake.ListSequences(databaseName, schemaName, db)
if errors.Is(err, sql.ErrNoRows) {
// If not found, mark resource to be removed from state file during apply or refresh
log.Printf("[DEBUG] sequences in schema (%s) not found", d.Id())
d.SetId("")
return nil
} else if err != nil {
log.Printf("[DEBUG] unable to parse sequences in schema (%s)", d.Id())
d.SetId("")
return nil
req := sdk.NewShowSequenceRequest().WithIn(&sdk.In{
Schema: sdk.NewDatabaseObjectIdentifier(databaseName, schemaName),
})
seqs, err := client.Sequences.Show(ctx, req)
if err != nil {
return err
}

sequences := []map[string]interface{}{}

for _, sequence := range currentSequences {
for _, seq := range seqs {
sequenceMap := map[string]interface{}{}

sequenceMap["name"] = sequence.Name.String
sequenceMap["database"] = sequence.DBName.String
sequenceMap["schema"] = sequence.SchemaName.String
sequenceMap["comment"] = sequence.Comment.String
sequenceMap["name"] = seq.Name
sequenceMap["database"] = seq.DatabaseName
sequenceMap["schema"] = seq.SchemaName
sequenceMap["comment"] = seq.Comment

sequences = append(sequences, sequenceMap)
}
Expand Down
Loading

0 comments on commit 973b8f7

Please sign in to comment.