Skip to content

Commit 86484bc

Browse files
antoineverinbpaquet
authored andcommitted
fix cast to string error and refactor casts (#19)
1 parent 8ec6659 commit 86484bc

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

postgresql/resource_postgresql_script.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package postgresql
33
import (
44
"crypto/sha1"
55
"encoding/hex"
6+
"fmt"
67
"log"
78
"time"
89

@@ -54,10 +55,14 @@ func resourcePostgreSQLScript() *schema.Resource {
5455
}
5556

5657
func resourcePostgreSQLScriptCreateOrUpdate(db *DBConnection, d *schema.ResourceData) error {
57-
commands := d.Get(scriptCommandsAttr).([]any)
58+
commands, err := toStringArray(d.Get(scriptCommandsAttr).([]any))
5859
tries := d.Get(scriptTriesAttr).(int)
5960
backoffDelay := d.Get(scriptBackoffDelayAttr).(int)
6061

62+
if err != nil {
63+
return err
64+
}
65+
6166
sum := shasumCommands(commands)
6267

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

7277
func resourcePostgreSQLScriptRead(db *DBConnection, d *schema.ResourceData) error {
73-
commands := d.Get(scriptCommandsAttr).([]any)
78+
commands, err := toStringArray(d.Get(scriptCommandsAttr).([]any))
79+
if err != nil {
80+
return err
81+
}
7482
newSum := shasumCommands(commands)
7583
d.Set(scriptShasumAttr, newSum)
7684

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

84-
func executeCommands(db *DBConnection, commands []any, tries int, backoffDelay int) error {
92+
func executeCommands(db *DBConnection, commands []string, tries int, backoffDelay int) error {
8593
for try := 1; ; try++ {
8694
err := executeBatch(db, commands)
8795
if err == nil {
@@ -95,26 +103,38 @@ func executeCommands(db *DBConnection, commands []any, tries int, backoffDelay i
95103
}
96104
}
97105

98-
func executeBatch(db *DBConnection, commands []any) error {
106+
func executeBatch(db *DBConnection, commands []string) error {
99107
for _, command := range commands {
100-
log.Printf("[ERROR] Executing %s", command.(string))
101-
_, err := db.Query(command.(string))
108+
log.Printf("[DEBUG] Executing %s", command)
109+
_, err := db.Query(command)
102110

103111
if err != nil {
104-
log.Println("[ERROR] Error catched:", err)
112+
log.Println("[DEBUG] Error catched:", err)
105113
if _, rollbackError := db.Query("ROLLBACK"); rollbackError != nil {
106-
log.Println("[ERROR] Rollback raised an error:", rollbackError)
114+
log.Println("[DEBUG] Rollback raised an error:", rollbackError)
107115
}
108116
return err
109117
}
110118
}
111119
return nil
112120
}
113121

114-
func shasumCommands(commands []any) string {
122+
func shasumCommands(commands []string) string {
115123
sha := sha1.New()
116124
for _, command := range commands {
117-
sha.Write([]byte(command.(string)))
125+
sha.Write([]byte(command))
118126
}
119127
return hex.EncodeToString(sha.Sum(nil))
120128
}
129+
130+
func toStringArray(array []any) ([]string, error) {
131+
strings := make([]string, 0, len(array))
132+
for _, elem := range array {
133+
str, ok := elem.(string)
134+
if !ok {
135+
return nil, fmt.Errorf("element %v is not a string", elem)
136+
}
137+
strings = append(strings, str)
138+
}
139+
return strings, nil
140+
}

postgresql/resource_postgresql_script_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,29 @@ func TestAccPostgresqlScript_reapply(t *testing.T) {
133133
})
134134
}
135135

136+
func TestAccPostgresqlScript_invalid(t *testing.T) {
137+
config := `
138+
resource "postgresql_script" "invalid" {
139+
commands = [
140+
""
141+
]
142+
tries = 2
143+
backoff_delay = 2
144+
}
145+
`
146+
147+
resource.Test(t, resource.TestCase{
148+
PreCheck: func() { testAccPreCheck(t) },
149+
Providers: testAccProviders,
150+
Steps: []resource.TestStep{
151+
{
152+
Config: config,
153+
ExpectError: regexp.MustCompile("element <nil> is not a string"),
154+
},
155+
},
156+
})
157+
}
158+
136159
func TestAccPostgresqlScript_fail(t *testing.T) {
137160
config := `
138161
resource "postgresql_script" "invalid" {

0 commit comments

Comments
 (0)