Add a with-postgresql-transaction procedure
To help with running tests.
This commit is contained in:
parent
a171287f27
commit
051962b54d
1 changed files with 15 additions and 1 deletions
|
|
@ -17,7 +17,8 @@
|
||||||
|
|
||||||
(define-module (guix-data-service database)
|
(define-module (guix-data-service database)
|
||||||
#:use-module (squee)
|
#:use-module (squee)
|
||||||
#:export (with-postgresql-connection))
|
#:export (with-postgresql-connection
|
||||||
|
with-postgresql-transaction))
|
||||||
|
|
||||||
;; TODO This isn't exported for some reason
|
;; TODO This isn't exported for some reason
|
||||||
(define pg-conn-finish
|
(define pg-conn-finish
|
||||||
|
|
@ -38,3 +39,16 @@
|
||||||
(lambda (key . args)
|
(lambda (key . args)
|
||||||
(pg-conn-finish conn)))))
|
(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;"))))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue