summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Baines <mail@cbaines.net>2019-05-05 13:35:17 +0100
committerChristopher Baines <mail@cbaines.net>2019-05-05 13:35:17 +0100
commit051962b54d9a647adc8c09fb8ef33db2ac9b659a (patch)
treea096963723b804fbba18c2f0afc52b89c5c4332c
parenta171287f27c62b57cdb6dbc3dafa0a082cad8831 (diff)
downloaddata-service-051962b54d9a647adc8c09fb8ef33db2ac9b659a.tar
data-service-051962b54d9a647adc8c09fb8ef33db2ac9b659a.tar.gz
Add a with-postgresql-transaction procedure
To help with running tests.
-rw-r--r--guix-data-service/database.scm16
1 files changed, 15 insertions, 1 deletions
diff --git a/guix-data-service/database.scm b/guix-data-service/database.scm
index ba7cd64..ab6758b 100644
--- a/guix-data-service/database.scm
+++ b/guix-data-service/database.scm
@@ -17,7 +17,8 @@
(define-module (guix-data-service database)
#:use-module (squee)
- #:export (with-postgresql-connection))
+ #:export (with-postgresql-connection
+ with-postgresql-transaction))
;; TODO This isn't exported for some reason
(define pg-conn-finish
@@ -38,3 +39,16 @@
(lambda (key . args)
(pg-conn-finish conn)))))
+(define* (with-postgresql-transaction conn f
+ #:key always-rollback?)
+ (exec-query conn "BEGIN;")
+
+ (with-throw-handler #t
+ (lambda ()
+ (let ((result (f conn)))
+ (exec-query conn (if always-rollback?
+ "ROLLBACK;"
+ "COMMIT;"))
+ result))
+ (lambda (key . args)
+ (exec-query conn "ROLLBACK;"))))