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

From: Simen Heggestøyl <[email-masked]>
Date: Sat, 18 Feb 2017 18:01:55 +0100

On Wed, Jan 4, 2017 at 4:16 AM, Michael Mauger <[email-masked]> 
wrote:
> Just got back after the holiday. Makes sense that could be a problem. 
> I'm getting ready to push a change related to completing-read so let 
> me take a look.
> 
> Thanks for staying on top of this...

I think the attached patch fixes half of the problem. Then we need to
do something about the `completing-read' to make it possible to still
select a database when `sql-postgres-list-databases' returns nil.

-- Simen
>From 100fc2038511a10edbda1fbf0c877bef33efbe4c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simen=20Heggest=C3=B8yl?= <[email-masked]>
Date: Sat, 18 Feb 2017 17:50:16 +0100
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.

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

diff --git a/lisp/progmodes/sql.el b/lisp/progmodes/sql.el
index 8868343129..4a936a6b45 100644
--- a/lisp/progmodes/sql.el
+++ b/lisp/progmodes/sql.el
@@ -1087,9 +1087,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
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.11.0