Skip to content

Commit b224fb7

Browse files
committed
add timeout to context
1 parent d415907 commit b224fb7

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

postgresql/resource_postgresql_script.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

postgresql/resource_postgresql_script_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,27 @@ func TestAccPostgresqlScript_failMultiple(t *testing.T) {
203203
},
204204
})
205205
}
206+
207+
func TestAccPostgresqlScript_timeout(t *testing.T) {
208+
config := `
209+
resource "postgresql_script" "invalid" {
210+
commands = [
211+
"BEGIN",
212+
"SELECT pg_sleep(2);",
213+
"COMMIT"
214+
]
215+
timeout = 1
216+
}
217+
`
218+
219+
resource.Test(t, resource.TestCase{
220+
PreCheck: func() { testAccPreCheck(t) },
221+
Providers: testAccProviders,
222+
Steps: []resource.TestStep{
223+
{
224+
Config: config,
225+
ExpectError: regexp.MustCompile("canceling statement"),
226+
},
227+
},
228+
})
229+
}

0 commit comments

Comments
 (0)