Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback for db_exists when the pg_catalog is protected #57

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions click_odoo/env_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@
from contextlib import closing

import click
import psycopg2
from click.decorators import _param_memo # XXX undocumented click internal

from .compat import environment_manage
from .env import OdooEnvironment, odoo

try:
from psycopg2.errors import InsufficientPrivilege
except ImportError:
# for psycopg2 < 2.8
from psycopg2 import ProgrammingError as InsufficientPrivilege

_logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -145,15 +152,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 psycopg2.OperationalError:
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 InsufficientPrivilege:
return self._can_connect_to_db(dbname)

def _pop_params(self, ctx):
ctx.params.pop("config", None)
Expand Down
3 changes: 3 additions & 0 deletions newsfragments/57.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Support deployments where access to ``pg_catalog.pg_databases`` is restricted
(this is the case on ``odoo.sh``). If the normal mechanism fails, we attempt to
connect to the database, and if that fail we assume it does not exist.
Loading