Skip to content

fix cast to string error and refactor casts #19

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

Merged
merged 1 commit into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
40 changes: 30 additions & 10 deletions postgresql/resource_postgresql_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package postgresql
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"log"
"time"

Expand Down Expand Up @@ -54,10 +55,14 @@ func resourcePostgreSQLScript() *schema.Resource {
}

func resourcePostgreSQLScriptCreateOrUpdate(db *DBConnection, d *schema.ResourceData) error {
commands := d.Get(scriptCommandsAttr).([]any)
commands, err := toStringArray(d.Get(scriptCommandsAttr).([]any))
tries := d.Get(scriptTriesAttr).(int)
backoffDelay := d.Get(scriptBackoffDelayAttr).(int)

if err != nil {
return err
}

sum := shasumCommands(commands)

if err := executeCommands(db, commands, tries, backoffDelay); err != nil {
Expand All @@ -70,7 +75,10 @@ func resourcePostgreSQLScriptCreateOrUpdate(db *DBConnection, d *schema.Resource
}

func resourcePostgreSQLScriptRead(db *DBConnection, d *schema.ResourceData) error {
commands := d.Get(scriptCommandsAttr).([]any)
commands, err := toStringArray(d.Get(scriptCommandsAttr).([]any))
if err != nil {
return err
}
newSum := shasumCommands(commands)
d.Set(scriptShasumAttr, newSum)

Expand All @@ -81,7 +89,7 @@ func resourcePostgreSQLScriptDelete(db *DBConnection, d *schema.ResourceData) er
return nil
}

func executeCommands(db *DBConnection, commands []any, tries int, backoffDelay int) error {
func executeCommands(db *DBConnection, commands []string, tries int, backoffDelay int) error {
for try := 1; ; try++ {
err := executeBatch(db, commands)
if err == nil {
Expand All @@ -95,26 +103,38 @@ func executeCommands(db *DBConnection, commands []any, tries int, backoffDelay i
}
}

func executeBatch(db *DBConnection, commands []any) error {
func executeBatch(db *DBConnection, commands []string) error {
for _, command := range commands {
log.Printf("[ERROR] Executing %s", command.(string))
_, err := db.Query(command.(string))
log.Printf("[DEBUG] Executing %s", command)
_, err := db.Query(command)

if err != nil {
log.Println("[ERROR] Error catched:", err)
log.Println("[DEBUG] Error catched:", err)
if _, rollbackError := db.Query("ROLLBACK"); rollbackError != nil {
log.Println("[ERROR] Rollback raised an error:", rollbackError)
log.Println("[DEBUG] Rollback raised an error:", rollbackError)
}
return err
}
}
return nil
}

func shasumCommands(commands []any) string {
func shasumCommands(commands []string) string {
sha := sha1.New()
for _, command := range commands {
sha.Write([]byte(command.(string)))
sha.Write([]byte(command))
}
return hex.EncodeToString(sha.Sum(nil))
}

func toStringArray(array []any) ([]string, error) {
strings := make([]string, 0, len(array))
for _, elem := range array {
str, ok := elem.(string)
if !ok {
return nil, fmt.Errorf("element %v is not a string", elem)
}
strings = append(strings, str)
}
return strings, nil
}
23 changes: 23 additions & 0 deletions postgresql/resource_postgresql_script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,29 @@ func TestAccPostgresqlScript_reapply(t *testing.T) {
})
}

func TestAccPostgresqlScript_invalid(t *testing.T) {
config := `
resource "postgresql_script" "invalid" {
commands = [
""
]
tries = 2
backoff_delay = 2
}
`

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
ExpectError: regexp.MustCompile("element <nil> is not a string"),
},
},
})
}

func TestAccPostgresqlScript_fail(t *testing.T) {
config := `
resource "postgresql_script" "invalid" {
Expand Down
Loading