Skip to content

Commit

Permalink
add unsafe exec resource
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak committed Nov 24, 2023
1 parent 6d7f833 commit 522d7ef
Show file tree
Hide file tree
Showing 12 changed files with 447 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ func getResources() map[string]*schema.Resource {
"snowflake_tag_association": resources.TagAssociation(),
"snowflake_tag_masking_policy_association": resources.TagMaskingPolicyAssociation(),
"snowflake_task": resources.Task(),
"snowflake_unsafe_migration": resources.UnsafeMigration(),
"snowflake_user": resources.User(),
"snowflake_user_ownership_grant": resources.UserOwnershipGrant(),
"snowflake_user_public_keys": resources.UserPublicKeys(),
Expand Down
4 changes: 4 additions & 0 deletions pkg/resources/testdata/TestAcc_UnsafeMigration_basic/test.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "snowflake_unsafe_migration" "migration" {
up = var.up
down = var.down
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "up" {
type = string
}

variable "down" {
type = string
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "snowflake_unsafe_migration" "migration" {
up = var.up
down = var.down
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "up" {
type = string
}

variable "down" {
type = string
}
4 changes: 4 additions & 0 deletions pkg/resources/testdata/TestAcc_UnsafeMigration_grants/test.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "snowflake_unsafe_migration" "migration" {
up = var.up
down = var.down
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "up" {
type = string
}

variable "down" {
type = string
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "snowflake_unsafe_migration" "migration" {
up = var.up
down = var.down
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "up" {
type = string
}

variable "down" {
type = string
}
74 changes: 74 additions & 0 deletions pkg/resources/unsafe_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package resources

import (
"context"
"database/sql"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"log"
)

var unsafeMigrationSchema = map[string]*schema.Schema{
"up": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "TODO",
},
"down": {
Type: schema.TypeString,
Required: true,
Description: "TODO",
},
}

func UnsafeMigration() *schema.Resource {
return &schema.Resource{
Create: ApplyUnsafeMigration,
Read: schema.Noop,
Delete: RevertUnsafeMigration,
Update: schema.Noop,

Schema: unsafeMigrationSchema,
}
}

func ApplyUnsafeMigration(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
ctx := context.Background()
client := sdk.NewClientFromDB(db)

id, err := uuid.GenerateUUID()
if err != nil {
return err
}

upStatement := d.Get("up").(string)
_, err = client.ExecUnsafe(ctx, upStatement)
if err != nil {
return err
}

d.SetId(id)
log.Printf(`[DEBUG] SQL "%s" applied successfully\n`, upStatement)

return nil
}

func RevertUnsafeMigration(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
ctx := context.Background()
client := sdk.NewClientFromDB(db)

downStatement := d.Get("down").(string)
_, err := client.ExecUnsafe(ctx, downStatement)
if err != nil {
return err
}

d.SetId("")
log.Printf(`[DEBUG] SQL "%s" applied successfully\n`, downStatement)

return nil
}
Loading

0 comments on commit 522d7ef

Please sign in to comment.