Skip to content

Commit dddaf26

Browse files
committed
Deprecate parse_uri()
1 parent 4411a08 commit dddaf26

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

django_mongodb_backend/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import copy
22
import time
3+
import warnings
34

45
import django
56
from django.conf import settings
67
from django.core.exceptions import ImproperlyConfigured, ValidationError
78
from django.db.backends.utils import logger
9+
from django.utils.deprecation import RemovedInDjango60Warning
810
from django.utils.functional import SimpleLazyObject
911
from django.utils.text import format_lazy
1012
from django.utils.version import get_version_tuple
@@ -33,6 +35,11 @@ def parse_uri(uri, *, db_name=None, options=None, test=None):
3335
Convert the given uri into a dictionary suitable for Django's DATABASES
3436
setting.
3537
"""
38+
warnings.warn(
39+
'parse_uri() is deprecated. Put the connection string in DATABASES["HOST"] instead.',
40+
RemovedInDjango60Warning,
41+
stacklevel=2,
42+
)
3643
uri = pymongo_parse_uri(uri)
3744
host = None
3845
port = None

docs/internals.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,36 @@ merge of a "Add support for Django X.Y" pull request. Before merging that pull
6262
request, a branch is created off of main to track the previous feature release.
6363
For example, the 5.1.x branch is created shortly after the release of Django
6464
5.2, and main starts tracking the Django 5.2.x series.
65+
66+
Deprecation Timeline
67+
====================
68+
69+
This document outlines when various pieces of Django MongoDB Backend will be
70+
removed or altered in a backward incompatible way, following their deprecation.
71+
72+
6.0
73+
---
74+
75+
.. _parse-uri-deprecation:
76+
77+
``parse_uri()`` will be removed
78+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79+
80+
``django_mongodb_backend.utils.parse_uri()`` is deprecated in favor of putting
81+
the connection string in ``DATABASES["HOST"]``.
82+
83+
For example, instead of::
84+
85+
DATABASES = {
86+
"default": django_mongodb_backend.parse_uri("mongodb://localhost:27017/", db_name="db"),
87+
}
88+
89+
use::
90+
91+
DATABASES = {
92+
'default': {
93+
'ENGINE': 'django_mongodb_backend',
94+
'HOST': 'mongodb://localhost:27017/',
95+
'NAME': 'db',
96+
},
97+
}

docs/ref/utils.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ following parts can be considered stable.
1414

1515
.. function:: parse_uri(uri, db_name=None, options=None, test=None)
1616

17+
.. deprecated:: 5.2.2
18+
19+
``parse_uri()`` is deprecated in favor of putting the connection string
20+
in ``DATABASES["HOST"]``. See :ref:`parse-uri-deprecation` for upgrade
21+
instructions.
22+
1723
Parses a MongoDB `connection string`_ into a dictionary suitable for
1824
Django's :setting:`DATABASES` setting.
1925

docs/releases/5.2.x.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ Bug fixes
1717

1818
- ...
1919

20+
Deprecations
21+
------------
22+
23+
- ``django_mongod_backend.utils.parse_uri()`` is deprecated in favor of
24+
putting the connection string in ``DATABASES["HOST"]``. See
25+
:ref:`parse-uri-deprecation` for upgrade instructions.
26+
2027
5.2.1
2128
=====
2229

tests/backend_/utils/test_parse_uri.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import pymongo
44
from django.core.exceptions import ImproperlyConfigured
55
from django.test import SimpleTestCase
6+
from django.test.utils import ignore_warnings
7+
from django.utils.deprecation import RemovedInDjango60Warning
68

79
from django_mongodb_backend import parse_uri
810

911

12+
@ignore_warnings(category=RemovedInDjango60Warning)
1013
class ParseURITests(SimpleTestCase):
1114
def test_simple_uri(self):
1215
settings_dict = parse_uri("mongodb://cluster0.example.mongodb.net/myDatabase")
@@ -105,3 +108,10 @@ def test_invalid_credentials(self):
105108
def test_no_scheme(self):
106109
with self.assertRaisesMessage(pymongo.errors.InvalidURI, "Invalid URI scheme"):
107110
parse_uri("cluster0.example.mongodb.net")
111+
112+
113+
class ParseURIDeprecationTests(SimpleTestCase):
114+
def test_message(self):
115+
msg = 'parse_uri() is deprecated. Put the connection string in DATABASES["HOST"] instead.'
116+
with self.assertRaisesMessage(RemovedInDjango60Warning, msg):
117+
parse_uri("mongodb://cluster0.example.mongodb.net/")

0 commit comments

Comments
 (0)