@@ -18,6 +18,7 @@ import (
1818 "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1919 "github.com/hashicorp/terraform-plugin-framework/schema/validator"
2020 "github.com/hashicorp/terraform-plugin-framework/types"
21+ "github.com/hashicorp/terraform-plugin-log/tflog"
2122)
2223
2324var (
@@ -381,14 +382,29 @@ func (r *defaultPriviligesResource) Delete(ctx context.Context, req resource.Del
381382 tx , err := db .BeginTx (ctx , nil )
382383 if err != nil {
383384 resp .Diagnostics .AddError (
384- "Error creating role " ,
385+ "Error removing default privileges " ,
385386 "Unable to create transaction to the database, unexpected error: " + err .Error (),
386387 )
387388 return
388389 }
389390 defer tx .Rollback ()
390391
391- r .revokeAll (ctx , tx , owner , inSchema , objectType , role )
392+ err = r .revokeAll (ctx , tx , owner , inSchema , objectType , role )
393+ if err != nil {
394+ resp .Diagnostics .AddError (
395+ "Error removing default privileges" ,
396+ "Unexpected error: " + err .Error (),
397+ )
398+ return
399+ }
400+
401+ if err = tx .Commit (); err != nil {
402+ resp .Diagnostics .AddError (
403+ "Error rem]moving the default privileges" ,
404+ "Unable to commit the default privileges from '" + role + "' on database '" + database + "', unexpected error: " + err .Error (),
405+ )
406+ return
407+ }
392408}
393409
394410func (r * defaultPriviligesResource ) Configure (_ context.Context , req resource.ConfigureRequest , resp * resource.ConfigureResponse ) {
@@ -410,6 +426,7 @@ func (r *defaultPriviligesResource) Configure(_ context.Context, req resource.Co
410426
411427func (r * defaultPriviligesResource ) revokeAll (ctx context.Context , tx * sql.Tx , owner string , inSchema string , objectType string , role string ) error {
412428 sqlStatement := fmt .Sprintf ("ALTER DEFAULT PRIVILEGES FOR ROLE %s %s REVOKE ALL ON %s FROM %s" , owner , inSchema , objectType , role )
429+ tflog .Debug (ctx , "The SQL statement: " + sqlStatement )
413430
414431 _ , err := tx .ExecContext (ctx , sqlStatement )
415432 return err
0 commit comments