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

Postgresql cluster URI support #355

Merged
Merged
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
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 @@ -501,13 +502,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