@@ -3,6 +3,7 @@ package postgresql
3
3
import (
4
4
"crypto/sha1"
5
5
"encoding/hex"
6
+ "fmt"
6
7
"log"
7
8
"time"
8
9
@@ -54,10 +55,14 @@ func resourcePostgreSQLScript() *schema.Resource {
54
55
}
55
56
56
57
func resourcePostgreSQLScriptCreateOrUpdate (db * DBConnection , d * schema.ResourceData ) error {
57
- commands := d .Get (scriptCommandsAttr ).([]any )
58
+ commands , err := toStringArray ( d .Get (scriptCommandsAttr ).([]any ) )
58
59
tries := d .Get (scriptTriesAttr ).(int )
59
60
backoffDelay := d .Get (scriptBackoffDelayAttr ).(int )
60
61
62
+ if err != nil {
63
+ return err
64
+ }
65
+
61
66
sum := shasumCommands (commands )
62
67
63
68
if err := executeCommands (db , commands , tries , backoffDelay ); err != nil {
@@ -70,7 +75,10 @@ func resourcePostgreSQLScriptCreateOrUpdate(db *DBConnection, d *schema.Resource
70
75
}
71
76
72
77
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
+ }
74
82
newSum := shasumCommands (commands )
75
83
d .Set (scriptShasumAttr , newSum )
76
84
@@ -81,7 +89,7 @@ func resourcePostgreSQLScriptDelete(db *DBConnection, d *schema.ResourceData) er
81
89
return nil
82
90
}
83
91
84
- func executeCommands (db * DBConnection , commands []any , tries int , backoffDelay int ) error {
92
+ func executeCommands (db * DBConnection , commands []string , tries int , backoffDelay int ) error {
85
93
for try := 1 ; ; try ++ {
86
94
err := executeBatch (db , commands )
87
95
if err == nil {
@@ -95,26 +103,38 @@ func executeCommands(db *DBConnection, commands []any, tries int, backoffDelay i
95
103
}
96
104
}
97
105
98
- func executeBatch (db * DBConnection , commands []any ) error {
106
+ func executeBatch (db * DBConnection , commands []string ) error {
99
107
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 )
102
110
103
111
if err != nil {
104
- log .Println ("[ERROR ] Error catched:" , err )
112
+ log .Println ("[DEBUG ] Error catched:" , err )
105
113
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 )
107
115
}
108
116
return err
109
117
}
110
118
}
111
119
return nil
112
120
}
113
121
114
- func shasumCommands (commands []any ) string {
122
+ func shasumCommands (commands []string ) string {
115
123
sha := sha1 .New ()
116
124
for _ , command := range commands {
117
- sha .Write ([]byte (command .( string ) ))
125
+ sha .Write ([]byte (command ))
118
126
}
119
127
return hex .EncodeToString (sha .Sum (nil ))
120
128
}
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
+ }
0 commit comments