11package postgresql
22
33import (
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
2022func 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