Repository navigation
Cherry-pick e933df4: make search_path handling driver-agnostic - #51
Merged
Merged
Conversation
Fixes django-tenants#1244. `_cursor()` special-cased psycopg3, never reusing the caller's cursor for `SET search_path` in order to avoid the recursion 340f137 describes. Opening a raw `self.connection.cursor()` instead meant the statement bypassed Django's cursor wrapper -- so on psycopg3 it was invisible to assertNumQueries while on psycopg2 it was counted. That is why test_switching_search_path failed on psycopg2: the assertions had been written against psycopg3's accounting rather than the actual behaviour. Following the approach django-pgschemas takes, a `_setting_search_path` flag makes the re-entry a no-op instead of recursing, so the caller's cursor can be reused on both drivers and the statement is visible on both. The SET logic moves out of `_cursor()` into `_handle_search_path()`, which now branches only on whether a cursor was supplied -- never on the driver. This changes no database traffic. With `log_statement='all'`, master and this branch both send exactly 646 `SET search_path` statements for the same test; only their visibility to the cursor wrapper differs. The updated counts in test_switching_search_path{,_limited_calls} therefore make those assertions more accurate rather than looser -- they now count statements that were always executed. Also included, both from the same comparison: - `rollback()` clears the cached search_path. A session-level `SET` is transactional in PostgreSQL, so aborting the transaction that issued it reverts the search_path; with TENANT_LIMIT_SET_CALLS on, the cache could otherwise outlive a value the database had already discarded. - `_get_cursor_search_paths()` validates every part of the search path rather than only `self.schema_name`, which brings PG_EXTRA_SEARCH_PATHS under the same check before it is interpolated into the SET statement. CI gains a `psycopg-version` axis so the psycopg2 fallback is actually exercised. Verified with ./run_tests.sh -- both executors plus the create_tenant and clone_tenant integration steps -- on Python 3.12 / PostgreSQL 17: Dj 5.2.17 psycopg3 exit 0 | 130 tests | exec 2/2 | FAIL 0 | clone 1 Dj 5.2.17 psycopg2 exit 0 | 130 tests | exec 2/2 | FAIL 0 | clone 1 Dj 6.0.8 psycopg3 exit 0 | 130 tests | exec 2/2 | FAIL 0 | clone 1 Dj 6.0.8 psycopg2 exit 0 | 130 tests | exec 2/2 | FAIL 0 | clone 1 Dj 6.1rc1 psycopg3 exit 0 | 130 tests | exec 2/2 | FAIL 0 | clone 1 Dj 6.1rc1 psycopg2 exit 0 | 130 tests | exec 2/2 | FAIL 0 | clone 1 An earlier revision of this branch also failed on Django 4.2 + psycopg3 with `9 != 6`, because Django's own compose_sql/mogrify opened a second cursor per statement there and each one triggered a SET. Dropping 4.2 in django-tenants#1248 removed that case, so no version-conditional assertion is needed. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Upstream e933df4 still builds the SET statement with string interpolation. The fork quotes each schema name with quote_schema_name() and composes the statement with psycopg's sql module, so keep that here instead of regressing the 4.0.3 escaping fix. No change in behaviour for valid schema names.
kiwitcms-bot
pushed a commit
that referenced
this pull request
Nov 23, 2026
The fork restricts schema names to [a-z0-9]{1,63}, so rename the new
fixtures in test_tenants.py and assert that '$user2', 'my$user' and '$'
are rejected here too. No production code changes; search-path
validation already comes from the upstream refactor in #51.
kiwitcms-bot
pushed a commit
that referenced
this pull request
Nov 23, 2026
The fork restricts schema names to [a-z0-9]{1,63}, so rename the new
fixtures in test_tenants.py and assert that '$user2', 'my$user' and '$'
are rejected here too. No production code changes; search-path
validation already comes from the upstream refactor in #51.
atodorov
added a commit
that referenced
this pull request
Nov 23, 2026
The fork restricts schema names to [a-z0-9]{1,63}, so rename the new
fixtures in test_tenants.py and assert that '$user2', 'my$user' and '$'
are rejected here too. No production code changes; search-path
validation already comes from the upstream refactor in #51.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cherry-picks upstream
e933df4— “Make search_path handling driver-agnostic”.What it changes:
SET search_pathinstead of always opening a raw connection cursor; a_setting_search_pathflag prevents the re-entry recursion._handle_search_path(), which branches only on whether a cursor was supplied.rollback(), since PostgreSQL reverts a session-levelSETwhen the issuing transaction aborts.search_path(includingPUBLIC_SCHEMA_NAMEandPG_EXTRA_SEARCH_PATHS), not onlyself.schema_name.test_switching_search_path{,_limited_calls}query counts to include the SET statements that were already executed but previously invisible to the cursor wrapper.The cherry-pick preserves the original commit message and authorship. A second commit keeps the fork's psycopg
sql/quote_schema_name()composition for the SET statement; upstreame933df4still used string interpolation there, and the fork must not regress its 4.0.3 escaping fix.Fork adaptation: the upstream CI change adds a psycopg2/psycopg3 axis. This fork removed the psycopg2 fallback, so that workflow hunk was not applied.
Note: the all-search-path validation also appears in PR #50. Whichever lands second will need a trivial rebase.
Verification:
manage.py test django_tenantswithEXECUTOR=standard: 106 tests, OK (1 skipped)manage.py test django_tenantswithEXECUTOR=multiprocessing: 106 tests, OK (1 skipped)manage.py check: no issues