diff --git a/django_mongodb_backend/utils.py b/django_mongodb_backend/utils.py index 634ec234b..0240250cf 100644 --- a/django_mongodb_backend/utils.py +++ b/django_mongodb_backend/utils.py @@ -1,10 +1,12 @@ import copy import time +import warnings import django from django.conf import settings from django.core.exceptions import ImproperlyConfigured, ValidationError from django.db.backends.utils import logger +from django.utils.deprecation import RemovedInDjango60Warning from django.utils.functional import SimpleLazyObject from django.utils.text import format_lazy from django.utils.version import get_version_tuple @@ -33,6 +35,11 @@ def parse_uri(uri, *, db_name=None, options=None, test=None): Convert the given uri into a dictionary suitable for Django's DATABASES setting. """ + warnings.warn( + 'parse_uri() is deprecated. Put the connection string in DATABASES["HOST"] instead.', + RemovedInDjango60Warning, + stacklevel=2, + ) uri = pymongo_parse_uri(uri) host = None port = None diff --git a/docs/internals/deprecation.rst b/docs/internals/deprecation.rst index 84d53c1c6..76d4adb42 100644 --- a/docs/internals/deprecation.rst +++ b/docs/internals/deprecation.rst @@ -4,3 +4,30 @@ Deprecation Timeline This document outlines when various pieces of Django MongoDB Backend will be removed or altered in a backward incompatible way, following their deprecation. + +6.0 +--- + +.. _parse-uri-deprecation: + +``parse_uri()`` will be removed +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +``django_mongodb_backend.utils.parse_uri()`` is deprecated in favor of putting +the connection string in ``DATABASES["HOST"]``. + +For example, instead of:: + + DATABASES = { + "default": django_mongodb_backend.parse_uri("mongodb://localhost:27017/", db_name="db"), + } + +use:: + + DATABASES = { + 'default': { + 'ENGINE': 'django_mongodb_backend', + 'HOST': 'mongodb://localhost:27017/', + 'NAME': 'db', + }, + } diff --git a/docs/ref/utils.rst b/docs/ref/utils.rst index 8075da94d..5cdb0ccf3 100644 --- a/docs/ref/utils.rst +++ b/docs/ref/utils.rst @@ -14,6 +14,12 @@ following parts can be considered stable. .. function:: parse_uri(uri, db_name=None, options=None, test=None) + .. deprecated:: 5.2.2 + + ``parse_uri()`` is deprecated in favor of putting the connection string + in ``DATABASES["HOST"]``. See :ref:`the deprecation timeline + ` for upgrade instructions. + Parses a MongoDB `connection string`_ into a dictionary suitable for Django's :setting:`DATABASES` setting. diff --git a/docs/releases/5.2.x.rst b/docs/releases/5.2.x.rst index cf92e6368..c2a559f3f 100644 --- a/docs/releases/5.2.x.rst +++ b/docs/releases/5.2.x.rst @@ -17,6 +17,14 @@ Bug fixes - ... +Deprecated features +------------------- + +- ``django_mongodb_backend.utils.parse_uri()`` is deprecated in favor of + putting the connection string in ``DATABASES["HOST"]``. See + :ref:`the deprecation timeline ` for upgrade + instructions. + 5.2.1 ===== diff --git a/tests/backend_/utils/test_parse_uri.py b/tests/backend_/utils/test_parse_uri.py index 804c4efcb..a87154162 100644 --- a/tests/backend_/utils/test_parse_uri.py +++ b/tests/backend_/utils/test_parse_uri.py @@ -3,10 +3,13 @@ import pymongo from django.core.exceptions import ImproperlyConfigured from django.test import SimpleTestCase +from django.test.utils import ignore_warnings +from django.utils.deprecation import RemovedInDjango60Warning from django_mongodb_backend import parse_uri +@ignore_warnings(category=RemovedInDjango60Warning) class ParseURITests(SimpleTestCase): def test_simple_uri(self): settings_dict = parse_uri("mongodb://cluster0.example.mongodb.net/myDatabase") @@ -105,3 +108,10 @@ def test_invalid_credentials(self): def test_no_scheme(self): with self.assertRaisesMessage(pymongo.errors.InvalidURI, "Invalid URI scheme"): parse_uri("cluster0.example.mongodb.net") + + +class ParseURIDeprecationTests(SimpleTestCase): + def test_message(self): + msg = 'parse_uri() is deprecated. Put the connection string in DATABASES["HOST"] instead.' + with self.assertRaisesMessage(RemovedInDjango60Warning, msg): + parse_uri("mongodb://cluster0.example.mongodb.net/")