diff options
Diffstat (limited to 'guix-data-service/database.scm')
-rw-r--r-- | guix-data-service/database.scm | 83 |
1 files changed, 11 insertions, 72 deletions
diff --git a/guix-data-service/database.scm b/guix-data-service/database.scm index 7270e90..8af53da 100644 --- a/guix-data-service/database.scm +++ b/guix-data-service/database.scm @@ -29,10 +29,7 @@ with-postgresql-connection open-postgresql-connection - - with-postgresql-connection-per-thread - with-thread-postgresql-connection - close-thread-postgresql-connection + close-postgresql-connection with-postgresql-transaction @@ -116,6 +113,10 @@ conn)) +(define (close-postgresql-connection conn name) + (pg-conn-finish conn) + (decrement-connection-gauge name)) + (define (run-sqitch) (with-postgresql-connection "sqitch" @@ -168,11 +169,12 @@ params))))) (simple-format #t "running command: ~A\n" (string-join command)) - (unless (zero? (apply system* command)) - (simple-format - (current-error-port) - "error: sqitch command failed\n") - (exit 1)))))))) + (let ((pid (spawn (%config 'sqitch) command))) + (unless (= 0 (status:exit-val (cdr (waitpid pid)))) + (simple-format + (current-error-port) + "error: sqitch command failed\n") + (primitive-exit 1))))))))) (define* (with-postgresql-connection name f #:key (statement-timeout #f)) (let ((conn (open-postgresql-connection name statement-timeout))) @@ -201,69 +203,6 @@ (define %postgresql-connections-name (make-parameter #f)) -(define* (with-postgresql-connection-per-thread name thunk - #:key (statement-timeout #f)) - (parameterize ((%postgresql-connection-parameters - (list name statement-timeout)) - (%postgresql-connections-hash-table - (make-hash-table)) - (%postgresql-connections-name - name)) - (call-with-values - thunk - (lambda vals - (hash-for-each - (lambda (thread conn) - (pg-conn-finish conn) - (decrement-connection-gauge name)) - (%postgresql-connections-hash-table)) - - (apply values vals))))) - -(define %thread-postgresql-connection - (make-thread-local-fluid)) - -(define (with-thread-postgresql-connection f) - (define (set-current-thread-connection conn) - (if conn - (hash-set! (%postgresql-connections-hash-table) - (current-thread) - conn) - (hash-remove! (%postgresql-connections-hash-table) - (current-thread))) - (fluid-set! %thread-postgresql-connection - conn)) - - (let ((conn (fluid-ref %thread-postgresql-connection))) - (if conn - ;; Assume an exception here could mean the connection has failed, so - ;; close it - (with-exception-handler - (lambda (exn) - (pg-conn-finish conn) - (decrement-connection-gauge - (%postgresql-connections-name)) - (set-current-thread-connection #f) - (raise-exception exn)) - (lambda () - (f conn))) - - (let ((conn (apply open-postgresql-connection - (%postgresql-connection-parameters)))) - (set-current-thread-connection conn) - - (f conn))))) - -(define (close-thread-postgresql-connection) - (let ((conn (fluid-ref %thread-postgresql-connection))) - (when conn - (pg-conn-finish conn) - (hash-remove! (%postgresql-connections-hash-table) - (current-thread)) - (fluid-set! %thread-postgresql-connection #f) - (decrement-connection-gauge - (%postgresql-connections-name))))) - (define* (with-postgresql-transaction conn f #:key always-rollback?) (exec-query conn "BEGIN;") |