Subject: bug#25236: 26.0.50; sql-postgres not working on master

From: Simen Heggestøyl <[email-masked]>
Date: Sat, 29 Jul 2017 18:26:22 +0200

On Wed, Jan 4, 2017 at 4:16 AM, Michael Mauger <[email-masked]> 
wrote:
> I'm getting ready to push a change related to completing-read so let 
> me take a look.

Hi Michael, any progress on this? Otherwise, does the attached patch
look OK to install? I think it should fix the described problem.

-- Simen
>From 6a203bdf63bda37a28d5f4ae51386f552e337895 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simen=20Heggest=C3=B8yl?= <[email-masked]>
Date: Sat, 29 Jul 2017 18:20:43 +0200
Subject: [PATCH] Handle non-zero exit status from psql more gracefully

* lisp/progmodes/sql.el (sql-postgres-list-databases): Handle non-zero
exit statuses from `psql -ltX' more gracefully by returning nil.
(sql-get-login-ext): Don't require a database name match.

* test/lisp/progmodes/sql-tests.el
(sql-tests-postgres-list-databases-error): New test.
---
 lisp/progmodes/sql.el            |  9 +++++----
 test/lisp/progmodes/sql-tests.el | 10 ++++++++++
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/lisp/progmodes/sql.el b/lisp/progmodes/sql.el
index 68ca37207e..05370ba0aa 100644
--- a/lisp/progmodes/sql.el
+++ b/lisp/progmodes/sql.el
@@ -1090,9 +1090,10 @@ sql-postgres-list-databases
   "Return a list of available PostgreSQL databases."
   (when (executable-find sql-postgres-program)
     (let ((res '()))
-      (dolist (row (process-lines sql-postgres-program "-ltX"))
-        (when (string-match "^ \\([[:alnum:]-_]+\\) +|.*" row)
-          (push (match-string 1 row) res)))
+      (ignore-errors
+        (dolist (row (process-lines sql-postgres-program "-ltX"))
+          (when (string-match "^ \\([[:alnum:]-_]+\\) +|.*" row)
+            (push (match-string 1 row) res))))
       (nreverse res))))
 
 ;; Customization for Interbase
@@ -2971,7 +2972,7 @@ sql-get-login-ext
            (expand-file-name file-name))))
 
       ((plist-member plist :completion)
-       (completing-read prompt-def (plist-get plist :completion) nil t
+       (completing-read prompt-def (plist-get plist :completion) nil nil
                         last-value history-var default))
 
       ((plist-get plist :number)
diff --git a/test/lisp/progmodes/sql-tests.el b/test/lisp/progmodes/sql-tests.el
index 27a72aa2c2..f75005f737 100644
--- a/test/lisp/progmodes/sql-tests.el
+++ b/test/lisp/progmodes/sql-tests.el
@@ -43,5 +43,15 @@
     (should (equal (sql-postgres-list-databases)
                    '("db-name-1" "db_name_2")))))
 
+(ert-deftest sql-tests-postgres-list-databases-error ()
+  "Test that nil is returned when `psql -ltX' fails."
+  (cl-letf
+      (((symbol-function 'executable-find)
+        (lambda (_command) t))
+       ((symbol-function 'process-lines)
+        (lambda (_program &rest _args)
+          (error))))
+    (should-not (sql-postgres-list-databases))))
+
 (provide 'sql-tests)
 ;;; sql-tests.el ends here
-- 
2.13.2