From fbb4e9a6cc6a4ce62b3f4a97e24528028960c1ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Fri, 5 Jul 2024 17:36:37 +0200 Subject: [PATCH] Fallback for db_exists when the pg_catalog is protected --- click_odoo/env_options.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/click_odoo/env_options.py b/click_odoo/env_options.py index 6b60d56..3939b0d 100644 --- a/click_odoo/env_options.py +++ b/click_odoo/env_options.py @@ -5,6 +5,7 @@ from contextlib import closing import click +import psycopg2.errors from click.decorators import _param_memo # XXX undocumented click internal from .compat import environment_manage @@ -145,15 +146,26 @@ def _configure_odoo(self, ctx): odoo.tools.config.parse_config(odoo_args) odoo.cli.server.report_configuration() + def _can_connect_to_db(self, dbname): + try: + conn = odoo.sql_db.db_connect(dbname) + with closing(conn.cursor()): + return True + except Exception: + return False + def _db_exists(self, dbname): - conn = odoo.sql_db.db_connect("postgres") - with closing(conn.cursor()) as cr: - cr._obj.execute( - "SELECT datname FROM pg_catalog.pg_database " - "WHERE lower(datname) = lower(%s)", - (dbname,), - ) - return bool(cr.fetchone()) + try: + conn = odoo.sql_db.db_connect("postgres") + with closing(conn.cursor()) as cr: + cr._obj.execute( + "SELECT datname FROM pg_catalog.pg_database " + "WHERE lower(datname) = lower(%s)", + (dbname,), + ) + return bool(cr.fetchone()) + except psycopg2.errors.InsufficientPrivilege: + return self._can_connect_to_db(dbname) def _pop_params(self, ctx): ctx.params.pop("config", None)