changeset 4955:5c43427fc2bf fairway-marks-import

Do not rollback savepoint if statement returned no rows Statements that might or might not return rows can modify the database in both cases e.g. through DML in functions, CTEs or triggers. Not returning any rows but modifying the database is not an error in such a case. Thus, prevent ROLLBACK.
author Tom Gottfried <tom@intevation.de>
date Wed, 26 Feb 2020 11:25:28 +0100
parents 331a59ebaf54
children 7cc79c65a9e5
files pkg/imports/misc.go
diffstat 1 files changed, 7 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/imports/misc.go	Tue Feb 25 15:20:42 2020 +0100
+++ b/pkg/imports/misc.go	Wed Feb 26 11:25:28 2020 +0100
@@ -37,7 +37,7 @@
 // of managing database SAVEPOINTs.
 // If the returned function is called with a callback
 // the callback is run in a SAVEPOINT.
-// If the callback returns w/o an error the SAVEPOINT
+// If the callback returns w/o an error or with sql.ErrNoRows the SAVEPOINT
 // is released. Otherwise the SAVEPOINT is rolled back.
 func Savepoint(
 	ctx context.Context,
@@ -64,9 +64,13 @@
 				}
 			}
 		}()
-		if err = fn(); err == nil {
+
+		// Release SAVEPOINT if statements run in fn returned no database error
+		if err = fn(); err == nil || err == sql.ErrNoRows {
 			done = true
-			_, err = tx.ExecContext(ctx, release)
+			if _, err2 := tx.ExecContext(ctx, release); err2 != nil {
+				err = err2
+			}
 		}
 		return
 	}