From 0b2da554b504b60b6e5835280fe63e1dd549c5c6 Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Tue, 21 Jul 2026 03:09:00 +0200 Subject: [PATCH 1/2] ci: Reuse Django test database across forked tests Every @pytest.mark.forked django_db test runs pytest-django's session-scoped database setup in its own child process, so each fork created and migrated a fresh postgres test database (~1s of fixed cost per test, ~110 such tests). This made the Test django step dominate the Web 1 CI job at ~2 minutes per tox env, run serially for 3 Django versions per Python version. Pass --reuse-db --no-migrations to pytest for django envs and give each env a stable database name (instead of the per-PID name) so all forks of an env share one database. Parallel tox envs still get distinct databases via {envname}. Locally this cuts a django env from ~92s to ~55s even on the sqlite-only path; the postgres win in CI is larger. Co-Authored-By: Claude Fable 5 --- scripts/populate_tox/tox.jinja | 7 +++++++ tox.ini | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/scripts/populate_tox/tox.jinja b/scripts/populate_tox/tox.jinja index 12a2038683..fef2a2fc37 100644 --- a/scripts/populate_tox/tox.jinja +++ b/scripts/populate_tox/tox.jinja @@ -221,6 +221,13 @@ setenv = py3.6: COVERAGE_RCFILE=.coveragerc36 django: DJANGO_SETTINGS_MODULE=tests.integrations.django.myapp.settings + # @pytest.mark.forked django_db tests run pytest-django's session-scoped + # database setup in each child process, so without --reuse-db every fork + # creates and migrates its own test database (~1s per test on postgres). + # A stable per-env database name lets all forks of an env share one + # database while keeping parallel tox envs from colliding. + django: PYTEST_ADDOPTS=--reuse-db --no-migrations + django: SENTRY_PYTHON_TEST_POSTGRES_NAME={env:SENTRY_PYTHON_TEST_POSTGRES_NAME:myapp_db_{envname}} spark-v{3.0.3,3.5.6}: JAVA_HOME=/usr/lib/jvm/temurin-11-jdk-amd64 # Ray 2.43+ auto-detects `uv run` drivers and spawns workers via uv, diff --git a/tox.ini b/tox.ini index 889af6d6bf..213bd7d324 100644 --- a/tox.ini +++ b/tox.ini @@ -18606,6 +18606,13 @@ setenv = py3.6: COVERAGE_RCFILE=.coveragerc36 django: DJANGO_SETTINGS_MODULE=tests.integrations.django.myapp.settings + # @pytest.mark.forked django_db tests run pytest-django's session-scoped + # database setup in each child process, so without --reuse-db every fork + # creates and migrates its own test database (~1s per test on postgres). + # A stable per-env database name lets all forks of an env share one + # database while keeping parallel tox envs from colliding. + django: PYTEST_ADDOPTS=--reuse-db --no-migrations + django: SENTRY_PYTHON_TEST_POSTGRES_NAME={env:SENTRY_PYTHON_TEST_POSTGRES_NAME:myapp_db_{envname}} spark-v{3.0.3,3.5.6}: JAVA_HOME=/usr/lib/jvm/temurin-11-jdk-amd64 # Ray 2.43+ auto-detects `uv run` drivers and spawns workers via uv, From 6147d47118a18233790679ca13393f7aafe9d074 Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Tue, 21 Jul 2026 03:15:49 +0200 Subject: [PATCH 2/2] test(django): Disable --reuse-db on pytest-django without databases support pytest-django < 4.3 has no databases kwarg on the django_db marker, so it only flushes the default database between transactional tests. Rows written to the secondary postgres database then leak across forked tests when the test database is reused, causing duplicate-key failures on the Django 1.11 and 2.2 envs. Fall back to recreating the database per fork there; newer envs keep the reuse speedup. Co-Authored-By: Claude Fable 5 --- tests/integrations/django/conftest.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/integrations/django/conftest.py diff --git a/tests/integrations/django/conftest.py b/tests/integrations/django/conftest.py new file mode 100644 index 0000000000..7e6a761776 --- /dev/null +++ b/tests/integrations/django/conftest.py @@ -0,0 +1,21 @@ +import pytest_django + +# pytest-django versions without `databases` support on the django_db marker +# (see `pytest_mark_django_db_decorator` in utils.py) only flush the default +# database between transactional tests, so rows written to the secondary +# postgres database would survive into the next test when the test database +# is reused. Disable --reuse-db there; those envs fall back to recreating +# the database in every forked test. +_reuse_db_supported = False +try: + pytest_django_version = tuple(map(int, pytest_django.__version__.split("."))) + _reuse_db_supported = pytest_django_version > (4, 2, 0) +except ValueError: + _reuse_db_supported = "dev" in pytest_django.__version__ +except AttributeError: + pass + + +def pytest_configure(config): + if not _reuse_db_supported and getattr(config.option, "reuse_db", False): + config.option.reuse_db = False