Fixed #36663 -- Defaulted to running checks against all databases.

Regression in 0b83c8cc4d which added
support for running checks only against databases but also defaulted to
running against no databases if not specified.

Note that this continues to *not* default to runing database tagged
checks for all management commands except the migrate one as whether or
not we should change this must be discussed further.

Thanks Tim Graham for surfacing how this was a problematic default.
This commit is contained in:
Simon Charette 2025-09-29 17:47:56 -04:00 committed by Mariusz Felisiak
parent 96ee097bd6
commit 3aba1fced8
2 changed files with 39 additions and 7 deletions

View file

@ -1,6 +1,7 @@
from collections.abc import Iterable
from itertools import chain
from django.db import connections
from django.utils.inspect import func_accepts_kwargs
@ -84,6 +85,14 @@ class CheckRegistry:
if tags is not None:
checks = [check for check in checks if not set(check.tags).isdisjoint(tags)]
elif not databases:
# By default, 'database'-tagged checks are not run if an alias
# is not explicitly specified as they do more than mere static
# code analysis.
checks = [check for check in checks if Tags.database not in check.tags]
if databases is None:
databases = list(connections)
for check in checks:
new_errors = check(app_configs=app_configs, databases=databases)

View file

@ -92,6 +92,21 @@ class SystemCheckFrameworkTests(SimpleTestCase):
with self.assertRaisesMessage(TypeError, msg):
registry.run_checks()
def test_run_checks_database_exclusion(self):
registry = CheckRegistry()
database_errors = [checks.Warning("Database Check")]
@registry.register(Tags.database)
def database_system_check(**kwargs):
return database_errors
errors = registry.run_checks()
self.assertEqual(errors, [])
errors = registry.run_checks(databases=["default"])
self.assertEqual(errors, database_errors)
class MessageTests(SimpleTestCase):
def test_printing(self):
@ -190,10 +205,12 @@ class CheckCommandTests(SimpleTestCase):
def test_simple_call(self):
call_command("check")
self.assertEqual(
simple_system_check.kwargs, {"app_configs": None, "databases": None}
simple_system_check.kwargs,
{"app_configs": None, "databases": ["default", "other"]},
)
self.assertEqual(
tagged_system_check.kwargs, {"app_configs": None, "databases": None}
tagged_system_check.kwargs,
{"app_configs": None, "databases": ["default", "other"]},
)
@override_system_checks([simple_system_check, tagged_system_check])
@ -203,11 +220,17 @@ class CheckCommandTests(SimpleTestCase):
admin_config = apps.get_app_config("admin")
self.assertEqual(
simple_system_check.kwargs,
{"app_configs": [auth_config, admin_config], "databases": None},
{
"app_configs": [auth_config, admin_config],
"databases": ["default", "other"],
},
)
self.assertEqual(
tagged_system_check.kwargs,
{"app_configs": [auth_config, admin_config], "databases": None},
{
"app_configs": [auth_config, admin_config],
"databases": ["default", "other"],
},
)
@override_system_checks([simple_system_check, tagged_system_check])
@ -215,7 +238,8 @@ class CheckCommandTests(SimpleTestCase):
call_command("check", tags=["simpletag"])
self.assertIsNone(simple_system_check.kwargs)
self.assertEqual(
tagged_system_check.kwargs, {"app_configs": None, "databases": None}
tagged_system_check.kwargs,
{"app_configs": None, "databases": ["default", "other"]},
)
@override_system_checks([simple_system_check, tagged_system_check])
@ -273,8 +297,7 @@ class CheckCommandTests(SimpleTestCase):
with override_system_checks([database_check]):
call_command("check")
database_check.assert_called_once_with(app_configs=None, databases=None)
database_check.reset_mock()
database_check.assert_not_called()
call_command("check", databases=["default"])
database_check.assert_called_once_with(
app_configs=None, databases=["default"]