Skip to content

Commit

Permalink
Merge pull request #355 from Kapellmeister/postgres-db-cluster-parsing
Browse files Browse the repository at this point in the history
Postgresql cluster URI support
  • Loading branch information
sergeyklay authored Jan 2, 2022
2 parents dc3b775 + 6e74baa commit 840e426
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
22 changes: 20 additions & 2 deletions environ/environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""

import ast
import itertools
import logging
import os
import re
Expand Down Expand Up @@ -502,13 +503,30 @@ def db_url_config(cls, url, engine=None):
if url.port:
path += ':{port}'.format(port=url.port)

user_host = url.netloc.rsplit('@', 1)
if url.scheme in cls.POSTGRES_FAMILY and ',' in user_host[-1]:
# Parsing postgres cluster dsn
hinfo = list(
itertools.zip_longest(
*(
host.rsplit(':', 1)
for host in user_host[-1].split(',')
)
)
)
hostname = ','.join(hinfo[0])
port = ','.join(filter(None, hinfo[1])) if len(hinfo) == 2 else ''
else:
hostname = url.hostname
port = url.port

# Update with environment configuration.
config.update({
'NAME': path or '',
'USER': _cast_urlstr(url.username) or '',
'PASSWORD': _cast_urlstr(url.password) or '',
'HOST': url.hostname or '',
'PORT': _cast_int(url.port) or '',
'HOST': hostname or '',
'PORT': _cast_int(port) or '',
})

if (
Expand Down
18 changes: 18 additions & 0 deletions tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@
'uf07k1i6d8ia0v',
'wegauwhgeuioweg',
5431),
('postgres://username:p@ss:12,wor:34d@host1:111,22.55.44.88:222,[2001:db8::1234]:333/db',
DJANGO_POSTGRES,
'db',
'host1,22.55.44.88,[2001:db8::1234]',
'username',
'p@ss:12,wor:34d',
'111,222,333'
),
('postgres://node1,node2,node3/db',
DJANGO_POSTGRES,
'db',
'node1,node2,node3',
'',
'',
''
),
('mysqlgis://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.'
'compute-1.amazonaws.com:5431/d8r82722r2kuvn',
'django.contrib.gis.db.backends.mysql',
Expand Down Expand Up @@ -97,6 +113,8 @@
'postgres',
'postgres_unix_domain',
'postgis',
'postgres_cluster',
'postgres_no_ports',
'mysqlgis',
'cleardb',
'mysql_no_password',
Expand Down

0 comments on commit 840e426

Please sign in to comment.