Skip to content

Commit a9386ec

Browse files
concatenate commands
1 parent ab2f4fb commit a9386ec

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

postgresql/resource_postgresql_script.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,27 @@ func executeCommands(ctx context.Context, db *DBConnection, commands []string, t
168168
func executeBatch(ctx context.Context, db *DBConnection, commands []string, timeout int) error {
169169
timeoutContext, timeoutCancel := context.WithTimeout(ctx, time.Duration(timeout)*time.Second)
170170
defer timeoutCancel()
171-
for _, command := range commands {
172-
log.Printf("[DEBUG] Executing %s", command)
173-
_, err := db.ExecContext(timeoutContext, command)
174-
log.Printf("[DEBUG] Result %s: %v", command, err)
175-
if err != nil {
176-
log.Println("[DEBUG] Error catched:", err)
177-
if _, rollbackError := db.Query("ROLLBACK"); rollbackError != nil {
178-
log.Println("[DEBUG] Rollback raised an error:", rollbackError)
179-
}
180-
return err
171+
172+
// Concatenate all commands into a single SQL statement to ensure they run on one connection
173+
concatenatedSQL := ""
174+
for i, command := range commands {
175+
if i > 0 {
176+
concatenatedSQL += "; "
181177
}
178+
concatenatedSQL += command
182179
}
180+
181+
log.Printf("[DEBUG] Executing concatenated SQL: %s", concatenatedSQL)
182+
_, err := db.ExecContext(timeoutContext, concatenatedSQL)
183+
log.Printf("[DEBUG] Result: %v", err)
184+
if err != nil {
185+
log.Println("[DEBUG] Error catched:", err)
186+
if _, rollbackError := db.Query("ROLLBACK"); rollbackError != nil {
187+
log.Println("[DEBUG] Rollback raised an error:", rollbackError)
188+
}
189+
return err
190+
}
191+
183192
return nil
184193
}
185194

0 commit comments

Comments
 (0)