@@ -3,6 +3,7 @@ package postgresql
33import (
44 "crypto/sha1"
55 "encoding/hex"
6+ "fmt"
67 "log"
78 "time"
89
@@ -54,10 +55,14 @@ func resourcePostgreSQLScript() *schema.Resource {
5455}
5556
5657func 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
7277func 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+ }
0 commit comments