From a35a2850446aabdbf54b7d4c118daad8197c0097 Mon Sep 17 00:00:00 2001 From: Antoine Verin Date: Mon, 17 Mar 2025 14:51:31 +0100 Subject: [PATCH 01/12] add postgresql_script resource --- postgresql/provider.go | 1 + postgresql/resource_postgresql_script.go | 106 ++++++++++++ postgresql/resource_postgresql_script_test.go | 157 ++++++++++++++++++ .../docs/r/postgresql_script.html.markdown | 46 +++++ 4 files changed, 310 insertions(+) create mode 100644 postgresql/resource_postgresql_script.go create mode 100644 postgresql/resource_postgresql_script_test.go create mode 100644 website/docs/r/postgresql_script.html.markdown diff --git a/postgresql/provider.go b/postgresql/provider.go index 2096576a..8479890b 100644 --- a/postgresql/provider.go +++ b/postgresql/provider.go @@ -201,6 +201,7 @@ func Provider() *schema.Provider { "postgresql_server": resourcePostgreSQLServer(), "postgresql_user_mapping": resourcePostgreSQLUserMapping(), "postgresql_alter_role": resourcePostgreSQLAlterRole(), + "postgresql_script": resourcePostgreSQLScript(), }, DataSourcesMap: map[string]*schema.Resource{ diff --git a/postgresql/resource_postgresql_script.go b/postgresql/resource_postgresql_script.go new file mode 100644 index 00000000..d1677025 --- /dev/null +++ b/postgresql/resource_postgresql_script.go @@ -0,0 +1,106 @@ +package postgresql + +import ( + "crypto/sha1" + "encoding/hex" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +const ( + scriptCommandsAttr = "commands" + scriptTriesAttr = "tries" + scriptTimeoutAttr = "timeout" + scriptShasumAttr = "shasum" +) + +func resourcePostgreSQLScript() *schema.Resource { + return &schema.Resource{ + Create: PGResourceFunc(resourcePostgreSQLScriptCreateOrUpdate), + Read: PGResourceFunc(resourcePostgreSQLScriptRead), + Update: PGResourceFunc(resourcePostgreSQLScriptCreateOrUpdate), + Delete: PGResourceFunc(resourcePostgreSQLScriptDelete), + + Schema: map[string]*schema.Schema{ + scriptCommandsAttr: { + Type: schema.TypeList, + Required: true, + Description: "List of SQL commands to execute", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + scriptTriesAttr: { + Type: schema.TypeInt, + Optional: true, + Default: 1, + Description: "Number of tries for a failing command", + }, + scriptTimeoutAttr: { + Type: schema.TypeInt, + Optional: true, + Default: 1, + Description: "Number of seconds between two tries for a command", + }, + scriptShasumAttr: { + Type: schema.TypeString, + Computed: true, + Description: "Shasum of commands", + }, + }, + } +} + +func resourcePostgreSQLScriptCreateOrUpdate(db *DBConnection, d *schema.ResourceData) error { + commands := d.Get(scriptCommandsAttr).([]any) + tries := d.Get(scriptTriesAttr).(int) + timeout := d.Get(scriptTimeoutAttr).(int) + + sum := shasumCommands(commands) + + if err := executeCommands(db, commands, tries, timeout); err != nil { + return err + } + + d.Set(scriptShasumAttr, sum) + d.SetId(sum) + return nil +} + +func resourcePostgreSQLScriptRead(db *DBConnection, d *schema.ResourceData) error { + commands := d.Get(scriptCommandsAttr).([]any) + newSum := shasumCommands(commands) + d.Set(scriptShasumAttr, newSum) + + return nil +} + +func resourcePostgreSQLScriptDelete(db *DBConnection, d *schema.ResourceData) error { + return nil +} + +func executeCommands(db *DBConnection, commands []any, tries int, timeout int) error { + for _, command := range commands { + for i := 1; ; i++ { + _, err := db.Query(command.(string)) + if err != nil { + if i >= tries { + return err + } + time.Sleep(time.Duration(timeout) * time.Second) + continue + } + break + } + } + return nil +} + +func shasumCommands(commands []any) string { + sha := sha1.New() + for _, command := range commands { + sha.Write([]byte(command.(string))) + } + return hex.EncodeToString(sha.Sum(nil)) +} diff --git a/postgresql/resource_postgresql_script_test.go b/postgresql/resource_postgresql_script_test.go new file mode 100644 index 00000000..6afe7be1 --- /dev/null +++ b/postgresql/resource_postgresql_script_test.go @@ -0,0 +1,157 @@ +package postgresql + +import ( + "regexp" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccPostgresqlScript_basic(t *testing.T) { + config := ` + resource "postgresql_script" "test" { + commands = [ + "SELECT 1;" + ] + tries = 2 + timeout = 4 + } + ` + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("postgresql_script.test", "commands.0", "SELECT 1;"), + resource.TestCheckResourceAttr("postgresql_script.test", "tries", "2"), + resource.TestCheckResourceAttr("postgresql_script.test", "timeout", "4"), + ), + }, + }, + }) +} + +func TestAccPostgresqlScript_multiple(t *testing.T) { + config := ` + resource "postgresql_script" "test" { + commands = [ + "SELECT 1;", + "SELECT 2;", + "SELECT 3;" + ] + tries = 2 + timeout = 4 + } + ` + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("postgresql_script.test", "commands.0", "SELECT 1;"), + resource.TestCheckResourceAttr("postgresql_script.test", "commands.1", "SELECT 2;"), + resource.TestCheckResourceAttr("postgresql_script.test", "commands.2", "SELECT 3;"), + resource.TestCheckResourceAttr("postgresql_script.test", "tries", "2"), + resource.TestCheckResourceAttr("postgresql_script.test", "timeout", "4"), + ), + }, + }, + }) +} + +func TestAccPostgresqlScript_default(t *testing.T) { + config := ` + resource "postgresql_script" "test" { + commands = [ + "SELECT 1;" + ] + } + ` + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("postgresql_script.test", "commands.0", "SELECT 1;"), + resource.TestCheckResourceAttr("postgresql_script.test", "tries", "1"), + resource.TestCheckResourceAttr("postgresql_script.test", "timeout", "1"), + ), + }, + }, + }) +} + +func TestAccPostgresqlScript_reapply(t *testing.T) { + config := ` + resource "postgresql_script" "test" { + commands = [ + "SELECT 1;" + ] + } + ` + + configChange := ` + resource "postgresql_script" "test" { + commands = [ + "SELECT 2;" + ] + } + ` + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("postgresql_script.test", "commands.0", "SELECT 1;"), + ), + }, + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("postgresql_script.test", "commands.0", "SELECT 1;"), + ), + }, + { + Config: configChange, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("postgresql_script.test", "commands.0", "SELECT 2;"), + ), + }, + }, + }) +} + +func TestAccPostgresqlScript_fail(t *testing.T) { + config := ` + resource "postgresql_script" "invalid" { + commands = [ + "SLC FROM nowhere;" + ] + tries = 2 + timeout = 2 + } + ` + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + ExpectError: regexp.MustCompile("syntax error"), + }, + }, + }) +} diff --git a/website/docs/r/postgresql_script.html.markdown b/website/docs/r/postgresql_script.html.markdown new file mode 100644 index 00000000..4ef2f2b6 --- /dev/null +++ b/website/docs/r/postgresql_script.html.markdown @@ -0,0 +1,46 @@ +--- +layout: "postgresql" +page_title: "PostgreSQL: postgresql_script" +sidebar_current: "docs-postgresql-resource-postgresql_script" +description: |- + Execute a SQL scipt +--- + +# postgresql\_script + +The ``postgresql_script`` execute a script given as parameter. + +## Usage + +```hcl +resource "postgresql_script" "foo" { + commands = [ + "command 1", + "command 2" + ] + tries = 1 + timeout = 1 +} +``` + +## Argument Reference + +* `commands` - (Required) An array of commands to execute, one by one. +* `tries` - (Optional) Number of tries of a command before raising an error. +* `timeout` - (Optional) Time in second between two failing commands. + +## Examples + +Revoke default accesses for public schema: + +```hcl +resource "postgresql_script" "foo" { + commands = [ + "BEBIN", + "SELECT * FROM table", + "COMMIT" + ] + tries = 3 + timeout = 1 +} +``` From 48ac715f9cb84be8030b101d0c5ec42d8f10871d Mon Sep 17 00:00:00 2001 From: Antoine Verin Date: Mon, 17 Mar 2025 17:08:01 +0100 Subject: [PATCH 02/12] fix typo in the postgresql_script description --- website/docs/r/postgresql_script.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/postgresql_script.html.markdown b/website/docs/r/postgresql_script.html.markdown index 4ef2f2b6..5a1bbf17 100644 --- a/website/docs/r/postgresql_script.html.markdown +++ b/website/docs/r/postgresql_script.html.markdown @@ -3,7 +3,7 @@ layout: "postgresql" page_title: "PostgreSQL: postgresql_script" sidebar_current: "docs-postgresql-resource-postgresql_script" description: |- - Execute a SQL scipt + Execute a SQL script --- # postgresql\_script From aafff3b32c3a13eb27b8c19c4c318ca0407cf455 Mon Sep 17 00:00:00 2001 From: Antoine Verin Date: Mon, 17 Mar 2025 17:08:29 +0100 Subject: [PATCH 03/12] add reference to postgresql_script in documentation --- website/postgresql.erb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/website/postgresql.erb b/website/postgresql.erb index 816b158b..e51e57ff 100644 --- a/website/postgresql.erb +++ b/website/postgresql.erb @@ -55,6 +55,9 @@ > postgresql_user_mapping + > + postgresql_script + From 255d84e24587ecda38db197b50c7a00068b9f736 Mon Sep 17 00:00:00 2001 From: Antoine Verin Date: Tue, 18 Mar 2025 09:06:43 +0100 Subject: [PATCH 04/12] improve definition of postgresql_script resource in documentation --- website/docs/r/postgresql_script.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/postgresql_script.html.markdown b/website/docs/r/postgresql_script.html.markdown index 5a1bbf17..2f4f66ac 100644 --- a/website/docs/r/postgresql_script.html.markdown +++ b/website/docs/r/postgresql_script.html.markdown @@ -8,7 +8,7 @@ description: |- # postgresql\_script -The ``postgresql_script`` execute a script given as parameter. +The ``postgresql_script`` execute a script given as parameter. This script will be executed each time it changes. ## Usage From 2e843f498c30b950b22ca8f5ed8ecbd98d9875d7 Mon Sep 17 00:00:00 2001 From: Antoine Verin Date: Tue, 18 Mar 2025 12:00:25 +0100 Subject: [PATCH 05/12] update retry logic to retry all instead of the failing one in postgresql_script resource --- postgresql/resource_postgresql_script.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/postgresql/resource_postgresql_script.go b/postgresql/resource_postgresql_script.go index d1677025..849fe055 100644 --- a/postgresql/resource_postgresql_script.go +++ b/postgresql/resource_postgresql_script.go @@ -3,6 +3,7 @@ package postgresql import ( "crypto/sha1" "encoding/hex" + "log" "time" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -81,20 +82,28 @@ func resourcePostgreSQLScriptDelete(db *DBConnection, d *schema.ResourceData) er } func executeCommands(db *DBConnection, commands []any, tries int, timeout int) error { - for _, command := range commands { - for i := 1; ; i++ { - _, err := db.Query(command.(string)) + for i := 1; ; i++ { + var err error + for _, command := range commands { + log.Printf("[DEBUG] Executing (%d try) %s", i, command.(string)) + _, err = db.Query(command.(string)) + if err != nil { + log.Println("[DEBUG] Error catched:", err) + if _, err := db.Query("ROLLBACK"); err != nil { + log.Println("[DEBUG] Rollback raised an error:", err) + } if i >= tries { return err } time.Sleep(time.Duration(timeout) * time.Second) - continue + break } - break + } + if err == nil { + return nil } } - return nil } func shasumCommands(commands []any) string { From e1a49373b37ffc963cf7546e5a512cd883ad8781 Mon Sep 17 00:00:00 2001 From: Antoine Verin Date: Tue, 18 Mar 2025 12:01:39 +0100 Subject: [PATCH 06/12] add test case for multiple failing command --- postgresql/resource_postgresql_script_test.go | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/postgresql/resource_postgresql_script_test.go b/postgresql/resource_postgresql_script_test.go index 6afe7be1..e319d498 100644 --- a/postgresql/resource_postgresql_script_test.go +++ b/postgresql/resource_postgresql_script_test.go @@ -155,3 +155,28 @@ func TestAccPostgresqlScript_fail(t *testing.T) { }, }) } + +func TestAccPostgresqlScript_failMultiple(t *testing.T) { + config := ` + resource "postgresql_script" "invalid" { + commands = [ + "BEGIN", + "SLC FROM nowhere;", + "COMMIT" + ] + tries = 2 + timeout = 2 + } + ` + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + ExpectError: regexp.MustCompile("syntax error"), + }, + }, + }) +} From 3f3e83f2d11219bc1e7598f4c5efd7625281ac9b Mon Sep 17 00:00:00 2001 From: Antoine Verin Date: Tue, 18 Mar 2025 17:19:44 +0100 Subject: [PATCH 07/12] rename timeout to backoffDelay --- postgresql/resource_postgresql_script.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/postgresql/resource_postgresql_script.go b/postgresql/resource_postgresql_script.go index 849fe055..11ae6cdb 100644 --- a/postgresql/resource_postgresql_script.go +++ b/postgresql/resource_postgresql_script.go @@ -10,10 +10,10 @@ import ( ) const ( - scriptCommandsAttr = "commands" - scriptTriesAttr = "tries" - scriptTimeoutAttr = "timeout" - scriptShasumAttr = "shasum" + scriptCommandsAttr = "commands" + scriptTriesAttr = "tries" + scriptBackoffDelayAttr = "backoffDelay" + scriptShasumAttr = "shasum" ) func resourcePostgreSQLScript() *schema.Resource { @@ -38,7 +38,7 @@ func resourcePostgreSQLScript() *schema.Resource { Default: 1, Description: "Number of tries for a failing command", }, - scriptTimeoutAttr: { + scriptBackoffDelayAttr: { Type: schema.TypeInt, Optional: true, Default: 1, @@ -56,11 +56,11 @@ func resourcePostgreSQLScript() *schema.Resource { func resourcePostgreSQLScriptCreateOrUpdate(db *DBConnection, d *schema.ResourceData) error { commands := d.Get(scriptCommandsAttr).([]any) tries := d.Get(scriptTriesAttr).(int) - timeout := d.Get(scriptTimeoutAttr).(int) + backoffDelay := d.Get(scriptBackoffDelayAttr).(int) sum := shasumCommands(commands) - if err := executeCommands(db, commands, tries, timeout); err != nil { + if err := executeCommands(db, commands, tries, backoffDelay); err != nil { return err } @@ -81,7 +81,7 @@ func resourcePostgreSQLScriptDelete(db *DBConnection, d *schema.ResourceData) er return nil } -func executeCommands(db *DBConnection, commands []any, tries int, timeout int) error { +func executeCommands(db *DBConnection, commands []any, tries int, backoffDelay int) error { for i := 1; ; i++ { var err error for _, command := range commands { @@ -90,13 +90,13 @@ func executeCommands(db *DBConnection, commands []any, tries int, timeout int) e if err != nil { log.Println("[DEBUG] Error catched:", err) - if _, err := db.Query("ROLLBACK"); err != nil { - log.Println("[DEBUG] Rollback raised an error:", err) + if _, rollbackError := db.Query("ROLLBACK"); rollbackError != nil { + log.Println("[DEBUG] Rollback raised an error:", rollbackError) } if i >= tries { return err } - time.Sleep(time.Duration(timeout) * time.Second) + time.Sleep(time.Duration(backoffDelay) * time.Second) break } } From fc353f8cd7d649d1eb2b3374993eb873ed11d5f3 Mon Sep 17 00:00:00 2001 From: Antoine Verin <24255551+antoineverin@users.noreply.github.com> Date: Tue, 18 Mar 2025 17:22:58 +0100 Subject: [PATCH 08/12] rename timeout to backoffdelay in documentation Co-authored-by: Bertrand Paquet --- website/docs/r/postgresql_script.html.markdown | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/website/docs/r/postgresql_script.html.markdown b/website/docs/r/postgresql_script.html.markdown index 2f4f66ac..6691bf74 100644 --- a/website/docs/r/postgresql_script.html.markdown +++ b/website/docs/r/postgresql_script.html.markdown @@ -10,6 +10,8 @@ description: |- The ``postgresql_script`` execute a script given as parameter. This script will be executed each time it changes. +If one command of the batch fails, the provider will send a `ROLLBACK` command to the database, and retry, according to the tries / backoff_delay configuration. + ## Usage ```hcl @@ -19,7 +21,7 @@ resource "postgresql_script" "foo" { "command 2" ] tries = 1 - timeout = 1 + backoff_delay = 1 } ``` @@ -27,7 +29,7 @@ resource "postgresql_script" "foo" { * `commands` - (Required) An array of commands to execute, one by one. * `tries` - (Optional) Number of tries of a command before raising an error. -* `timeout` - (Optional) Time in second between two failing commands. +* `backoff_delay` - (Optional) In case of failure, time in second to wait before a retry. ## Examples @@ -41,6 +43,6 @@ resource "postgresql_script" "foo" { "COMMIT" ] tries = 3 - timeout = 1 + backoff_delay = 1 } ``` From faa3740fad7f07da1509c84c8584205807c1b480 Mon Sep 17 00:00:00 2001 From: Antoine Verin Date: Tue, 18 Mar 2025 17:25:04 +0100 Subject: [PATCH 09/12] fix backoffdelay name to backoff_delay --- postgresql/resource_postgresql_script.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/postgresql/resource_postgresql_script.go b/postgresql/resource_postgresql_script.go index 11ae6cdb..d6c49b6a 100644 --- a/postgresql/resource_postgresql_script.go +++ b/postgresql/resource_postgresql_script.go @@ -12,7 +12,7 @@ import ( const ( scriptCommandsAttr = "commands" scriptTriesAttr = "tries" - scriptBackoffDelayAttr = "backoffDelay" + scriptBackoffDelayAttr = "backoff_delay" scriptShasumAttr = "shasum" ) From 0ece4b4c418bf1a9dc5da1b9abe20d745e2d3b26 Mon Sep 17 00:00:00 2001 From: Antoine Verin <24255551+antoineverin@users.noreply.github.com> Date: Wed, 19 Mar 2025 09:02:22 +0100 Subject: [PATCH 10/12] Update postgresql/resource_postgresql_script.go Co-authored-by: Bertrand Paquet --- postgresql/resource_postgresql_script.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/postgresql/resource_postgresql_script.go b/postgresql/resource_postgresql_script.go index d6c49b6a..f81ccbd8 100644 --- a/postgresql/resource_postgresql_script.go +++ b/postgresql/resource_postgresql_script.go @@ -42,7 +42,7 @@ func resourcePostgreSQLScript() *schema.Resource { Type: schema.TypeInt, Optional: true, Default: 1, - Description: "Number of seconds between two tries for a command", + Description: "Number of seconds between two tries of the batch of commands", }, scriptShasumAttr: { Type: schema.TypeString, From b46675b457c94dd71d1f8987a17f270b3a16d34c Mon Sep 17 00:00:00 2001 From: Antoine Verin Date: Wed, 19 Mar 2025 09:04:15 +0100 Subject: [PATCH 11/12] rename timeout to backoff_delay in tests --- postgresql/resource_postgresql_script_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/postgresql/resource_postgresql_script_test.go b/postgresql/resource_postgresql_script_test.go index e319d498..24660f0d 100644 --- a/postgresql/resource_postgresql_script_test.go +++ b/postgresql/resource_postgresql_script_test.go @@ -14,7 +14,7 @@ func TestAccPostgresqlScript_basic(t *testing.T) { "SELECT 1;" ] tries = 2 - timeout = 4 + backoff_delay = 4 } ` @@ -27,7 +27,7 @@ func TestAccPostgresqlScript_basic(t *testing.T) { Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("postgresql_script.test", "commands.0", "SELECT 1;"), resource.TestCheckResourceAttr("postgresql_script.test", "tries", "2"), - resource.TestCheckResourceAttr("postgresql_script.test", "timeout", "4"), + resource.TestCheckResourceAttr("postgresql_script.test", "backoff_delay", "4"), ), }, }, @@ -43,7 +43,7 @@ func TestAccPostgresqlScript_multiple(t *testing.T) { "SELECT 3;" ] tries = 2 - timeout = 4 + backoff_delay = 4 } ` @@ -58,7 +58,7 @@ func TestAccPostgresqlScript_multiple(t *testing.T) { resource.TestCheckResourceAttr("postgresql_script.test", "commands.1", "SELECT 2;"), resource.TestCheckResourceAttr("postgresql_script.test", "commands.2", "SELECT 3;"), resource.TestCheckResourceAttr("postgresql_script.test", "tries", "2"), - resource.TestCheckResourceAttr("postgresql_script.test", "timeout", "4"), + resource.TestCheckResourceAttr("postgresql_script.test", "backoff_delay", "4"), ), }, }, @@ -83,7 +83,7 @@ func TestAccPostgresqlScript_default(t *testing.T) { Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("postgresql_script.test", "commands.0", "SELECT 1;"), resource.TestCheckResourceAttr("postgresql_script.test", "tries", "1"), - resource.TestCheckResourceAttr("postgresql_script.test", "timeout", "1"), + resource.TestCheckResourceAttr("postgresql_script.test", "backoff_delay", "1"), ), }, }, @@ -140,7 +140,7 @@ func TestAccPostgresqlScript_fail(t *testing.T) { "SLC FROM nowhere;" ] tries = 2 - timeout = 2 + backoff_delay = 2 } ` @@ -165,7 +165,7 @@ func TestAccPostgresqlScript_failMultiple(t *testing.T) { "COMMIT" ] tries = 2 - timeout = 2 + backoff_delay = 2 } ` From 0d873a3624a84dd773f25cb988eb03b862e7b19d Mon Sep 17 00:00:00 2001 From: Antoine Verin Date: Wed, 19 Mar 2025 09:15:15 +0100 Subject: [PATCH 12/12] refactor resource logic --- postgresql/resource_postgresql_script.go | 41 +++++++++++++----------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/postgresql/resource_postgresql_script.go b/postgresql/resource_postgresql_script.go index f81ccbd8..bd4cad99 100644 --- a/postgresql/resource_postgresql_script.go +++ b/postgresql/resource_postgresql_script.go @@ -82,30 +82,35 @@ func resourcePostgreSQLScriptDelete(db *DBConnection, d *schema.ResourceData) er } func executeCommands(db *DBConnection, commands []any, tries int, backoffDelay int) error { - for i := 1; ; i++ { - var err error - for _, command := range commands { - log.Printf("[DEBUG] Executing (%d try) %s", i, command.(string)) - _, err = db.Query(command.(string)) - - if err != nil { - log.Println("[DEBUG] Error catched:", err) - if _, rollbackError := db.Query("ROLLBACK"); rollbackError != nil { - log.Println("[DEBUG] Rollback raised an error:", rollbackError) - } - if i >= tries { - return err - } - time.Sleep(time.Duration(backoffDelay) * time.Second) - break - } - } + for try := 1; ; try++ { + err := executeBatch(db, commands) if err == nil { return nil + } else { + if try >= tries { + return err + } + time.Sleep(time.Duration(backoffDelay) * time.Second) } } } +func executeBatch(db *DBConnection, commands []any) error { + for _, command := range commands { + log.Printf("[ERROR] Executing %s", command.(string)) + _, err := db.Query(command.(string)) + + if err != nil { + log.Println("[ERROR] Error catched:", err) + if _, rollbackError := db.Query("ROLLBACK"); rollbackError != nil { + log.Println("[ERROR] Rollback raised an error:", rollbackError) + } + return err + } + } + return nil +} + func shasumCommands(commands []any) string { sha := sha1.New() for _, command := range commands {