Skip to content

Commit d415907

Browse files
committed
replace exec with execContext
1 parent 17b42d7 commit d415907

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

postgresql/helpers.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package postgresql
22

33
import (
4+
"context"
45
"database/sql"
56
"fmt"
67
"log"
78
"regexp"
89
"strings"
910

11+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1012
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1113
"github.com/lib/pq"
1214
)
@@ -37,6 +39,23 @@ func PGResourceExistsFunc(fn func(*DBConnection, *schema.ResourceData) (bool, er
3739
}
3840
}
3941

42+
func PGResourceContextFunc(fn func(context.Context, *DBConnection, *schema.ResourceData) diag.Diagnostics) func(context.Context, *schema.ResourceData, interface{}) diag.Diagnostics {
43+
return func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
44+
client := meta.(*Client)
45+
46+
db, err := client.Connect()
47+
if err != nil {
48+
return diag.Diagnostics{diag.Diagnostic{
49+
Severity: diag.Error,
50+
Summary: "Failled to connext",
51+
Detail: err.Error(),
52+
}}
53+
}
54+
55+
return fn(ctx, db, d)
56+
}
57+
}
58+
4059
// QueryAble is a DB connection (sql.DB/Tx)
4160
type QueryAble interface {
4261
Exec(query string, args ...interface{}) (sql.Result, error)

postgresql/resource_postgresql_script.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package postgresql
22

33
import (
4+
"context"
45
"crypto/sha1"
56
"encoding/hex"
67
"fmt"
78
"log"
89
"time"
910

11+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1012
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1113
)
1214

@@ -19,10 +21,10 @@ const (
1921

2022
func resourcePostgreSQLScript() *schema.Resource {
2123
return &schema.Resource{
22-
Create: PGResourceFunc(resourcePostgreSQLScriptCreateOrUpdate),
23-
Read: PGResourceFunc(resourcePostgreSQLScriptRead),
24-
Update: PGResourceFunc(resourcePostgreSQLScriptCreateOrUpdate),
25-
Delete: PGResourceFunc(resourcePostgreSQLScriptDelete),
24+
CreateContext: PGResourceContextFunc(resourcePostgreSQLScriptCreateOrUpdate),
25+
Read: PGResourceFunc(resourcePostgreSQLScriptRead),
26+
UpdateContext: PGResourceContextFunc(resourcePostgreSQLScriptCreateOrUpdate),
27+
Delete: PGResourceFunc(resourcePostgreSQLScriptDelete),
2628

2729
Schema: map[string]*schema.Schema{
2830
scriptCommandsAttr: {
@@ -54,19 +56,27 @@ func resourcePostgreSQLScript() *schema.Resource {
5456
}
5557
}
5658

57-
func resourcePostgreSQLScriptCreateOrUpdate(db *DBConnection, d *schema.ResourceData) error {
59+
func resourcePostgreSQLScriptCreateOrUpdate(ctx context.Context, db *DBConnection, d *schema.ResourceData) diag.Diagnostics {
5860
commands, err := toStringArray(d.Get(scriptCommandsAttr).([]any))
5961
tries := d.Get(scriptTriesAttr).(int)
6062
backoffDelay := d.Get(scriptBackoffDelayAttr).(int)
6163

6264
if err != nil {
63-
return err
65+
return diag.Diagnostics{diag.Diagnostic{
66+
Severity: diag.Error,
67+
Summary: "Commands input is not valid",
68+
Detail: err.Error(),
69+
}}
6470
}
6571

6672
sum := shasumCommands(commands)
6773

68-
if err := executeCommands(db, commands, tries, backoffDelay); err != nil {
69-
return err
74+
if err := executeCommands(ctx, db, commands, tries, backoffDelay); err != nil {
75+
return diag.Diagnostics{diag.Diagnostic{
76+
Severity: diag.Error,
77+
Summary: "Commands execution failed",
78+
Detail: err.Error(),
79+
}}
7080
}
7181

7282
d.Set(scriptShasumAttr, sum)
@@ -89,9 +99,9 @@ func resourcePostgreSQLScriptDelete(db *DBConnection, d *schema.ResourceData) er
8999
return nil
90100
}
91101

92-
func executeCommands(db *DBConnection, commands []string, tries int, backoffDelay int) error {
102+
func executeCommands(ctx context.Context, db *DBConnection, commands []string, tries int, backoffDelay int) error {
93103
for try := 1; ; try++ {
94-
err := executeBatch(db, commands)
104+
err := executeBatch(ctx, db, commands)
95105
if err == nil {
96106
return nil
97107
} else {
@@ -103,10 +113,10 @@ func executeCommands(db *DBConnection, commands []string, tries int, backoffDela
103113
}
104114
}
105115

106-
func executeBatch(db *DBConnection, commands []string) error {
116+
func executeBatch(ctx context.Context, db *DBConnection, commands []string) error {
107117
for _, command := range commands {
108118
log.Printf("[DEBUG] Executing %s", command)
109-
_, err := db.Exec(command)
119+
_, err := db.ExecContext(ctx, command)
110120
log.Printf("[DEBUG] Result %s: %v", command, err)
111121
if err != nil {
112122
log.Println("[DEBUG] Error catched:", err)

0 commit comments

Comments
 (0)