Skip to content

Commit

Permalink
Enhance Query String Parsing for Server-Side Binding in Django 4.2 wi…
Browse files Browse the repository at this point in the history
…th psycopg 3.1.8+ Resolves #234 (#235)

* Refactor parsing of query string to handle int, bool, and text types
  • Loading branch information
truongvan authored Dec 9, 2023
1 parent 9a74cc5 commit 0879f12
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
11 changes: 7 additions & 4 deletions dj_database_url/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,13 @@ def parse(
options["ssl"] = {"ca": values[-1]}
continue

try:
options[key] = int(values[-1])
except (TypeError, ValueError):
options[key] = values[-1]
value = values[-1]
if value.isdigit():
options[key] = int(value)
elif value.lower() in ("true", "false"):
options[key] = value.lower() == "true"
else:
options[key] = value

if ssl_require:
options["sslmode"] = "require"
Expand Down
15 changes: 15 additions & 0 deletions tests/test_dj_database_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ def test_postgres_parsing_with_special_characters(self):
assert url["PASSWORD"] == "#password"
assert url["PORT"] == 5431

def test_postgres_parsing_with_int_bool_str_query_string(self):
url = "postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn?server_side_binding=true&timeout=20&service=my_service&passfile=.my_pgpass"
url = dj_database_url.parse(url)

assert url["ENGINE"] == "django.db.backends.postgresql"
assert url["NAME"] == "d8r82722r2kuvn"
assert url["HOST"] == "ec2-107-21-253-135.compute-1.amazonaws.com"
assert url["USER"] == "uf07k1i6d8ia0v"
assert url["PASSWORD"] == "wegauwhgeuioweg"
assert url["PORT"] == 5431
assert url["OPTIONS"]["server_side_binding"] is True
assert url["OPTIONS"]["timeout"] == 20
assert url["OPTIONS"]["service"] == "my_service"
assert url["OPTIONS"]["passfile"] == ".my_pgpass"

def test_postgis_parsing(self):
url = "postgis://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn"
url = dj_database_url.parse(url)
Expand Down

0 comments on commit 0879f12

Please sign in to comment.