[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.
This commit is contained in:
Christophe Monniez
2017-11-17 15:23:14 +01:00
parent 21dd24c071
commit 0fdf291305
+2 -7
View File
@@ -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):