diff options
| author | Tim Graham <timograham@gmail.com> | 2025-12-23 19:34:32 -0500 |
|---|---|---|
| committer | Jacob Walls <jacobtylerwalls@gmail.com> | 2026-02-09 17:46:33 -0500 |
| commit | 226ca7b5ce283e1f891dbbbefb0afaebdaa09f28 (patch) | |
| tree | a9b1068ee193a1d6de95c8216986dd533ce0930e | |
| parent | 7cf1c22d4dfdd46f2082cfc55b714b68c4fd2de3 (diff) | |
Added DatabaseFeatures.supports_inspectdb.
Needed by MongoDB.
| -rw-r--r-- | django/core/management/commands/inspectdb.py | 4 | ||||
| -rw-r--r-- | django/db/backends/base/features.py | 3 | ||||
| -rw-r--r-- | docs/releases/6.1.txt | 3 | ||||
| -rw-r--r-- | tests/gis_tests/inspectapp/tests.py | 1 | ||||
| -rw-r--r-- | tests/inspectdb/tests.py | 22 |
5 files changed, 29 insertions, 4 deletions
diff --git a/django/core/management/commands/inspectdb.py b/django/core/management/commands/inspectdb.py index 06adfe39ed..8bd6c4bc0c 100644 --- a/django/core/management/commands/inspectdb.py +++ b/django/core/management/commands/inspectdb.py @@ -44,10 +44,10 @@ class Command(BaseCommand): ) def handle(self, **options): - try: + if connections[options["database"]].features.supports_inspectdb: for line in self.handle_inspection(options): self.stdout.write(line) - except NotImplementedError: + else: raise CommandError( "Database inspection isn't supported for the currently selected " "database backend." diff --git a/django/db/backends/base/features.py b/django/db/backends/base/features.py index 2031beaf8a..e8fa82aa21 100644 --- a/django/db/backends/base/features.py +++ b/django/db/backends/base/features.py @@ -400,6 +400,9 @@ class BaseDatabaseFeatures: supports_on_delete_db_default = True supports_on_delete_db_null = True + # Does the backend support the inspectdb management command? + supports_inspectdb = True + # Collation names for use by the Django test suite. test_collations = { "ci": None, # Case-insensitive. diff --git a/docs/releases/6.1.txt b/docs/releases/6.1.txt index a26b2be07f..0d35982543 100644 --- a/docs/releases/6.1.txt +++ b/docs/releases/6.1.txt @@ -367,6 +367,9 @@ backends. ``db_on_delete``) as values. ``db_on_delete`` is one of the database-level delete options e.g. :attr:`~django.db.models.DB_CASCADE`. +* Set the new ``DatabaseFeatures.supports_inspectdb`` attribute to ``False`` + if the management command isn't supported. + :mod:`django.contrib.gis` ------------------------- diff --git a/tests/gis_tests/inspectapp/tests.py b/tests/gis_tests/inspectapp/tests.py index 43227d232d..fd6fec32d9 100644 --- a/tests/gis_tests/inspectapp/tests.py +++ b/tests/gis_tests/inspectapp/tests.py @@ -14,6 +14,7 @@ from ..test_data import TEST_DATA from .models import AllOGRFields +@skipUnlessDBFeature("supports_inspectdb") class InspectDbTests(TestCase): def test_geom_columns(self): """ diff --git a/tests/inspectdb/tests.py b/tests/inspectdb/tests.py index 8175c52e4e..b677af23df 100644 --- a/tests/inspectdb/tests.py +++ b/tests/inspectdb/tests.py @@ -2,11 +2,16 @@ import re from io import StringIO from unittest import mock, skipUnless -from django.core.management import call_command +from django.core.management import CommandError, call_command from django.core.management.commands import inspectdb from django.db import connection from django.db.backends.base.introspection import TableInfo -from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature +from django.test import ( + TestCase, + TransactionTestCase, + skipIfDBFeature, + skipUnlessDBFeature, +) from .models import PeopleMoreData, test_collation @@ -40,6 +45,7 @@ def cursor_execute(*queries): return results +@skipUnlessDBFeature("supports_inspectdb") class InspectDBTestCase(TestCase): unique_re = re.compile(r".*unique_together = \((.+),\).*") @@ -519,6 +525,7 @@ class InspectDBTestCase(TestCase): ) +@skipUnlessDBFeature("supports_inspectdb") class InspectDBTransactionalTests(TransactionTestCase): available_apps = ["inspectdb"] @@ -671,3 +678,14 @@ class InspectDBTransactionalTests(TransactionTestCase): out = StringIO() call_command("inspectdb", "inspectdb_compositepkmodel", stdout=out) self.assertNotIn("unique_together", out.getvalue()) + + +@skipIfDBFeature("supports_inspectdb") +class InspectDBNotSupportedTests(TestCase): + def test_not_supported(self): + msg = ( + "Database inspection isn't supported for the currently selected " + "database backend." + ) + with self.assertRaisesMessage(CommandError, msg): + call_command("inspectdb") |
