Skip to content

Commit

Permalink
Allow blank DB host value, reuse backend code
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCoding committed Jan 13, 2025
1 parent 6228fe9 commit c5b420f
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions awx/main/dispatch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from django.conf import settings
from django.db import connection as pg_connection
from django.db.backends.postgresql.base import DatabaseWrapper as PsycopgDatabaseWrapper

NOT_READY = ([], [], [])

Expand Down Expand Up @@ -95,24 +96,25 @@ def close(self):


def create_listener_connection():
conf = deepcopy(settings.DATABASES['default'])
conf['OPTIONS'] = deepcopy(conf.get('OPTIONS', {}))
# Variable is called settings_dict to correspond to internal use in Django itself
settings_dict = deepcopy(settings.DATABASES['default'])
settings_dict['OPTIONS'] = deepcopy(settings_dict.get('OPTIONS', {}))

Check warning on line 101 in awx/main/dispatch/__init__.py

View check run for this annotation

Codecov / codecov/patch

awx/main/dispatch/__init__.py#L100-L101

Added lines #L100 - L101 were not covered by tests

# Modify the application name to distinguish from other connections the process might use
conf['OPTIONS']['application_name'] = get_application_name(settings.CLUSTER_HOST_ID, function='listener')
settings_dict['OPTIONS']['application_name'] = get_application_name(settings.CLUSTER_HOST_ID, function='listener')

Check warning on line 104 in awx/main/dispatch/__init__.py

View check run for this annotation

Codecov / codecov/patch

awx/main/dispatch/__init__.py#L104

Added line #L104 was not covered by tests

# Apply overrides specifically for the listener connection
for k, v in settings.LISTENER_DATABASES.get('default', {}).items():
if k != 'OPTIONS':
conf[k] = v
settings_dict[k] = v

Check warning on line 109 in awx/main/dispatch/__init__.py

View check run for this annotation

Codecov / codecov/patch

awx/main/dispatch/__init__.py#L109

Added line #L109 was not covered by tests
for k, v in settings.LISTENER_DATABASES.get('default', {}).get('OPTIONS', {}).items():
conf['OPTIONS'][k] = v
settings_dict['OPTIONS'][k] = v

Check warning on line 111 in awx/main/dispatch/__init__.py

View check run for this annotation

Codecov / codecov/patch

awx/main/dispatch/__init__.py#L111

Added line #L111 was not covered by tests

# Allow password-less authentication
if 'PASSWORD' in conf:
conf['OPTIONS']['password'] = conf.pop('PASSWORD')
# Reuse the Django postgres DB backend to create params for the psycopg library
psycopg_conn_params = PsycopgDatabaseWrapper(settings_dict).get_connection_params()
psycopg_conn_params['autocommit'] = True

Check warning on line 115 in awx/main/dispatch/__init__.py

View check run for this annotation

Codecov / codecov/patch

awx/main/dispatch/__init__.py#L114-L115

Added lines #L114 - L115 were not covered by tests

connection_data = f"dbname={conf['NAME']} host={conf['HOST']} user={conf['USER']} port={conf['PORT']}"
return psycopg.connect(connection_data, autocommit=True, **conf['OPTIONS'])
return psycopg.connect(**psycopg_conn_params)

Check warning on line 117 in awx/main/dispatch/__init__.py

View check run for this annotation

Codecov / codecov/patch

awx/main/dispatch/__init__.py#L117

Added line #L117 was not covered by tests


@contextmanager
Expand Down

0 comments on commit c5b420f

Please sign in to comment.