@@ -16,6 +16,7 @@ const (
1616 scriptCommandsAttr = "commands"
1717 scriptTriesAttr = "tries"
1818 scriptBackoffDelayAttr = "backoff_delay"
19+ scriptTimeoutAttr = "timeout"
1920 scriptShasumAttr = "shasum"
2021)
2122
@@ -47,6 +48,12 @@ func resourcePostgreSQLScript() *schema.Resource {
4748 Default : 1 ,
4849 Description : "Number of seconds between two tries of the batch of commands" ,
4950 },
51+ scriptTimeoutAttr : {
52+ Type : schema .TypeInt ,
53+ Optional : true ,
54+ Default : 5 * 60 ,
55+ Description : "Number of seconds for a batch of command to timeout" ,
56+ },
5057 scriptShasumAttr : {
5158 Type : schema .TypeString ,
5259 Computed : true ,
@@ -60,6 +67,7 @@ func resourcePostgreSQLScriptCreateOrUpdate(ctx context.Context, db *DBConnectio
6067 commands , err := toStringArray (d .Get (scriptCommandsAttr ).([]any ))
6168 tries := d .Get (scriptTriesAttr ).(int )
6269 backoffDelay := d .Get (scriptBackoffDelayAttr ).(int )
70+ timeout := d .Get (scriptTimeoutAttr ).(int )
6371
6472 if err != nil {
6573 return diag.Diagnostics {diag.Diagnostic {
@@ -71,7 +79,7 @@ func resourcePostgreSQLScriptCreateOrUpdate(ctx context.Context, db *DBConnectio
7179
7280 sum := shasumCommands (commands )
7381
74- if err := executeCommands (ctx , db , commands , tries , backoffDelay ); err != nil {
82+ if err := executeCommands (ctx , db , commands , tries , backoffDelay , timeout ); err != nil {
7583 return diag.Diagnostics {diag.Diagnostic {
7684 Severity : diag .Error ,
7785 Summary : "Commands execution failed" ,
@@ -99,9 +107,9 @@ func resourcePostgreSQLScriptDelete(db *DBConnection, d *schema.ResourceData) er
99107 return nil
100108}
101109
102- func executeCommands (ctx context.Context , db * DBConnection , commands []string , tries int , backoffDelay int ) error {
110+ func executeCommands (ctx context.Context , db * DBConnection , commands []string , tries int , backoffDelay int , timeout int ) error {
103111 for try := 1 ; ; try ++ {
104- err := executeBatch (ctx , db , commands )
112+ err := executeBatch (ctx , db , commands , timeout )
105113 if err == nil {
106114 return nil
107115 } else {
@@ -113,10 +121,12 @@ func executeCommands(ctx context.Context, db *DBConnection, commands []string, t
113121 }
114122}
115123
116- func executeBatch (ctx context.Context , db * DBConnection , commands []string ) error {
124+ func executeBatch (ctx context.Context , db * DBConnection , commands []string , timeout int ) error {
125+ timeoutContext , timeoutCancel := context .WithTimeout (ctx , time .Duration (timeout )* time .Second )
126+ defer timeoutCancel ()
117127 for _ , command := range commands {
118128 log .Printf ("[DEBUG] Executing %s" , command )
119- _ , err := db .ExecContext (ctx , command )
129+ _ , err := db .ExecContext (timeoutContext , command )
120130 log .Printf ("[DEBUG] Result %s: %v" , command , err )
121131 if err != nil {
122132 log .Println ("[DEBUG] Error catched:" , err )
0 commit comments