@@ -144,6 +144,7 @@ def django_db_setup(
144144 db_cfg = setup_databases (
145145 verbosity = request .config .option .verbose ,
146146 interactive = False ,
147+ serialized_aliases = _get_serialized_aliases (request .session .items ),
147148 ** setup_databases_args ,
148149 )
149150
@@ -301,6 +302,34 @@ def handle(self, *args, **kwargs):
301302 migrate .Command = MigrateSilentCommand
302303
303304
305+ def _get_serialized_aliases (tests : list ) -> set :
306+ """
307+ Get a set of `serialized_aliases` to be used with `setup_databases`.
308+
309+ The `serialized_aliases` argument of the `setup_databases` function determines what subset of aliases test
310+ databases should have their state serialized to allow usage of the `serialized_rollback` feature. If it's not
311+ provided, it defaults to aliases.
312+
313+ Django 5 removed the `SERIALIZE` test setting, as it can be inferred from the databases with the
314+ `serialized_rollback` option enabled. Django's default test runner, `DiscoverRunner`, uses logic similar to this to
315+ build the `serialized_aliases` set and pass it to `setup_databases`, as implemented in
316+ `django.test.runner.DiscoverRunner.run_tests`.
317+ """
318+ from django .db import connections
319+
320+ databases = {}
321+ for test in tests :
322+ if not (marker := test .get_closest_marker ("django_db" )):
323+ continue
324+ (_ , _ , test_databases , serialized_rollback , _ ) = validate_django_db (marker )
325+ if test_databases == "__all__" :
326+ test_databases = connections
327+ if test_databases :
328+ databases .update ((alias , serialized_rollback or databases .get (alias , False )) for alias in test_databases )
329+ serialized_aliases = {alias for alias , serialize in databases .items () if serialize }
330+ return serialized_aliases
331+
332+
304333def _set_suffix_to_test_databases (suffix : str ) -> None :
305334 from django .conf import settings
306335
0 commit comments