Skip to content

Cherry-pick e933df4: make search_path handling driver-agnostic - #51

Merged
atodorov merged 2 commits into
kiwitcms-v3.8.0from
backport/upstream-e933df4
Nov 23, 2026
Merged

atodorov merged 2 commits into
kiwitcms-v3.8.0from
backport/upstream-e933df4

Conversation

@kiwitcms-bot

Copy link
Copy Markdown
Member

Cherry-picks upstream e933df4 — “Make search_path handling driver-agnostic”.

What it changes:

  • Reuses the caller's cursor for SET search_path instead of always opening a raw connection cursor; a _setting_search_path flag prevents the re-entry recursion.
  • Moves the SET logic into _handle_search_path(), which branches only on whether a cursor was supplied.
  • Clears the cached search_path in rollback(), since PostgreSQL reverts a session-level SET when the issuing transaction aborts.
  • Validates every component of search_path (including PUBLIC_SCHEMA_NAME and PG_EXTRA_SEARCH_PATHS), not only self.schema_name.
  • Updates 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; upstream e933df4 still 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_tenants with EXECUTOR=standard: 106 tests, OK (1 skipped)
  • manage.py test django_tenants with EXECUTOR=multiprocessing: 106 tests, OK (1 skipped)
  • manage.py check: no issues

tomturner and others added 2 commits November 23, 2026 17:26
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.
@atodorov
atodorov merged commit 9c0a561 into kiwitcms-v3.8.0 Nov 23, 2026
98 checks passed
@atodorov
atodorov deleted the backport/upstream-e933df4 branch November 23, 2026 17:32
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants