[bug#36451,v2] gnu: services: postgresql: Don't initdb when directory exists
Commit Message
From: Robert Vollmert <rob@vllmrt.net>
* gnu/services/databases.scm (postgresql-activation): Check if
directory exists.
--------------
Dale Mellor:
- Modified to make patch apply to head of current master branch.
- Verified working, does not break an existing system.
- Code change is clean.
Reviewed-by: Dale Mellor <guix-devel-0brg6b@rdmp.org>
---
gnu/services/databases.scm | 68 ++++++++++++++++++++------------------
1 file changed, 35 insertions(+), 33 deletions(-)
Comments
Dale Mellor <guix-devel-0brg6b@rdmp.org> writes:
> From: Robert Vollmert <rob@vllmrt.net>
>
> * gnu/services/databases.scm (postgresql-activation): Check if
> directory exists.
>
> --------------
> Dale Mellor:
>
> - Modified to make patch apply to head of current master branch.
> - Verified working, does not break an existing system.
> - Code change is clean.
I think what I'm missing here is why this change is being made? I think
the PostgreSQL service works at the moment, so what does this change
mean?
Hi,
Dale Mellor <guix-devel-0brg6b@rdmp.org> skribis:
> From: Robert Vollmert <rob@vllmrt.net>
>
> * gnu/services/databases.scm (postgresql-activation): Check if
> directory exists.
Finally applied, thank you!
Ludo’.
@@ -235,20 +235,7 @@ (define postgresql-activation
(use-modules (guix build utils)
(ice-9 match))
- (let ((user (getpwnam "postgres"))
- (initdb (string-append
- #$(final-postgresql postgresql
- extension-packages)
- "/bin/initdb"))
- (initdb-args
- (append
- (if #$locale
- (list (string-append "--locale=" #$locale))
- '()))))
- ;; Create db state directory.
- (mkdir-p #$data-directory)
- (chown #$data-directory (passwd:uid user) (passwd:gid user))
-
+ (let ((user (getpwnam "postgres")))
;; Create the socket directory.
(let ((socket-directory
#$(postgresql-config-file-socket-directory config-file)))
@@ -261,25 +248,40 @@ (define postgresql-activation
(mkdir-p #$log-directory)
(chown #$log-directory (passwd:uid user) (passwd:gid user)))
- ;; Drop privileges and init state directory in a new
- ;; process. Wait for it to finish before proceeding.
- (match (primitive-fork)
- (0
- ;; Exit with a non-zero status code if an exception is thrown.
- (dynamic-wind
- (const #t)
- (lambda ()
- (setgid (passwd:gid user))
- (setuid (passwd:uid user))
- (primitive-exit
- (apply system*
- initdb
- "-D"
- #$data-directory
- initdb-args)))
- (lambda ()
- (primitive-exit 1))))
- (pid (waitpid pid))))))))
+ (unless (file-exists? #$data-directory)
+ (let ((initdb (string-append
+ #$(final-postgresql postgresql
+ extension-packages)
+ "/bin/initdb"))
+ (initdb-args
+ (append
+ (if #$locale
+ (list (string-append "--locale=" #$locale))
+ '()))))
+ ;; Create db state directory.
+ (mkdir-p #$data-directory)
+ (chown #$data-directory (passwd:uid user) (passwd:gid user))
+
+ ;; Drop privileges and init state directory in a new
+ ;; process. Wait for it to finish before proceeding.
+ (match (primitive-fork)
+ (0
+ ;; Exit with a non-zero status code if an exception is
+ ;; thrown.
+ (dynamic-wind
+ (const #t)
+ (lambda ()
+ (setgid (passwd:gid user))
+ (setuid (passwd:uid user))
+ (primitive-exit
+ (apply system*
+ initdb
+ "-D"
+ #$data-directory
+ initdb-args)))
+ (lambda ()
+ (primitive-exit 1))))
+ (pid (waitpid pid))))))))))
(define postgresql-shepherd-service
(match-lambda