From 0fdf2913052eecd426be6515d8f77cbd176dcee1 Mon Sep 17 00:00:00 2001 From: Christophe Monniez Date: Fri, 10 Nov 2017 13:40:59 +0100 Subject: [PATCH] [REF] db: simplify list_dbs When trying to list db's, the db_user is searched and verified before searching for the databases. As it's enclosed in a db.cursor context manager, we can assume that there is a db user at this very moment. This commit uses a single query to find databases that belongs to this cursor user. Also, this commit adds a message in the logs when an exception occurs to facilitate debugging instead of silently returning an empty list. --- odoo/service/db.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/odoo/service/db.py b/odoo/service/db.py index 117dae69baa..69f61736c95 100644 --- a/odoo/service/db.py +++ b/odoo/service/db.py @@ -361,16 +361,11 @@ def list_dbs(force=False): db = odoo.sql_db.db_connect('postgres') with closing(db.cursor()) as cr: try: - cr.execute('SELECT current_user') - db_user = cr.fetchone() - if db_user: - cr.execute("select datname from pg_database where datdba=(select usesysid from pg_user where usename=%s) and not datistemplate and datallowconn and datname not in %s order by datname", (db_user, templates_list)) - else: - cr.execute("select datname from pg_database where not datistemplate and datallowconn and datname not in %s order by datname", (templates_list,)) + cr.execute("select datname from pg_database where datdba=(select usesysid from pg_user where usename=current_user) and not datistemplate and datallowconn and datname not in %s order by datname", (templates_list,)) res = [odoo.tools.ustr(name) for (name,) in cr.fetchall()] except Exception: + _logger.exception('Listing databases failed:') res = [] - res.sort() return res def list_db_incompatible(databases):