Skip to content

Commit c9baa78

Browse files
Always create the new connection and close it after the commands
1 parent a660d1c commit c9baa78

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

postgresql/resource_postgresql_script.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,16 @@ func resourcePostgreSQLScriptCreateOrUpdate(ctx context.Context, db *DBConnectio
8787
// Get the target database connection
8888
database := getDatabase(d, db.client.databaseName)
8989

90-
targetDB := db
91-
if database != "" && database != db.client.databaseName {
92-
client := db.client.config.NewClient(database)
93-
newDB, err := client.Connect()
94-
if err != nil {
95-
return diag.Diagnostics{diag.Diagnostic{
96-
Severity: diag.Error,
97-
Summary: "Failed to connect to database",
98-
Detail: err.Error(),
99-
}}
100-
}
101-
targetDB = newDB
90+
client := db.client.config.NewClient(database)
91+
newDB, err := client.Connect()
92+
if err != nil {
93+
return diag.Diagnostics{diag.Diagnostic{
94+
Severity: diag.Error,
95+
Summary: "Failed to connect to database",
96+
Detail: err.Error(),
97+
}}
10298
}
99+
targetDB := newDB
103100

104101
if err := executeCommands(ctx, targetDB, commands, tries, backoffDelay, timeout); err != nil {
105102
return diag.Diagnostics{diag.Diagnostic{
@@ -109,6 +106,8 @@ func resourcePostgreSQLScriptCreateOrUpdate(ctx context.Context, db *DBConnectio
109106
}}
110107
}
111108

109+
defer targetDB.Close()
110+
112111
sum := shasumCommands(commands)
113112
d.SetId(sum)
114113

0 commit comments

Comments
 (0)