Skip to content

fix(mssql): connect per database when Azure SQL rejects USE - #325

Merged
Maxteabag merged 1 commit into
Maxteabag:mainfrom
CosX:fix/azure-sql-use-statement
Sep 3, 2026
Merged

Maxteabag merged 1 commit into
Maxteabag:mainfrom
CosX:fix/azure-sql-use-statement

Conversation

@CosX

@CosX CosX commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Fixes #324.

Browsing any database on an Azure SQL Database server fails with:

Error loading: Driver Error: Syntax error or access violation; DDBC Error: USE statement is not supported to switch between databases. Use a new connection to connect to a different database.

Azure SQL Database (EngineEdition 5) forbids USE — a connection is bound to one database for its lifetime — but _get_cursor_for_database always issues it.

Why the existing Azure path never engages

detect_capabilities() already flags EngineEdition 5/6 and apply_database_override() already rebuilds the config with DATABASE= swapped. But process_worker.py (and query_runner.py) read the capability before connecting, while detect_capabilities runs inside post_connect — after the branch is taken. The worker also builds a fresh adapter per request, so the override never carries over. The default (True) wins every time and USE is issued.

This change

Handle it where the failure actually surfaces, in SQLServerAdapter:

  • Regular SQL Server keeps the USE path untouched.
  • On the "USE statement is not supported" error, open a connection bound to the target database (via the existing apply_database_override) and use that cursor. Cached per (parent connection, database), so it costs one extra connection per database browsed, not one per lookup.
  • The first rejection sets _supports_cross_database_queries_override = False, so later lookups skip the doomed probe.
  • A lookup against the database the connection is already bound to issues no USE at all — Azure rejects even that.
  • Unrelated errors from USE still propagate.
  • New disconnect() override closes the extra connections; a weakref callback clears the same bookkeeping if a connection is collected without disconnect(), so the id()-keyed entries can't outlive their connection and bind a later one to the wrong database.
  • Connections that didn't come from this adapter's connect() (no stored config, e.g. the MagicMocks in the existing tests) fall back to plain USE.

Self-healing regardless of whether detect_capabilities() has run, so it works in the fresh-adapter worker processes too.

Tests

New tests/unit/test_mssql_azure_database_switch.py — a fake driver whose USE raises the Azure error — covers: fallback connection opened, reuse across lookups, one connection per database, no USE/reconnect for the current database, disconnect closing siblings, GC purging the bookkeeping, the capability flip, unrelated errors propagating, and regular SQL Server still using USE with no extra connections.

uv run --group dev pytest tests/unit tests/cli -q
1271 passed, 2 skipped

The same patch is applied to my installed 1.6.1 against a live Azure SQL Database server (ad_interactive auth); I'll report back once I've confirmed the explorer expands databases there.

Azure SQL Database forbids USE, so every explorer lookup against a
database other than the connected one failed with "USE statement is not
supported to switch between databases".

detect_capabilities already spots Azure (EngineEdition 5/6) and
apply_database_override already rebuilds the config with DATABASE=
swapped, but the capability is read before the connection is made — and
post_connect, where detection runs, comes after. The worker builds a
fresh adapter per request too, so the override never got a chance to
apply.

Handle it where the failure surfaces instead: keep USE for regular SQL
Server, and on the "USE statement is not supported" error open a
connection bound to the target database, cached per parent connection
and closed on disconnect. Unrelated errors still propagate. The first
rejection also flips the cross-database capability off, so later lookups
skip the doomed probe.

Fixes Maxteabag#324

Claude-Session: https://claude.ai/code/session_01VHstETxLs6WytrQjmHBcXK
@Maxteabag

Copy link
Copy Markdown
Owner

Takk for PR-en, Karl!

@Maxteabag
Maxteabag merged commit 5817714 into Maxteabag:main Sep 3, 2026
21 checks passed
@CosX
CosX deleted the fix/azure-sql-use-statement branch September 9, 2026 13:37
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.

Azure SQL Database: browsing databases fails with "USE statement is not supported to switch between databases"

2 participants