fix(mssql): connect per database when Azure SQL rejects USE - #325
Merged
Merged
Conversation
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
Owner
|
Takk for PR-en, Karl! |
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.
Fixes #324.
Browsing any database on an Azure SQL Database server fails with:
Azure SQL Database (EngineEdition 5) forbids
USE— a connection is bound to one database for its lifetime — but_get_cursor_for_databasealways issues it.Why the existing Azure path never engages
detect_capabilities()already flags EngineEdition 5/6 andapply_database_override()already rebuilds the config withDATABASE=swapped. Butprocess_worker.py(andquery_runner.py) read the capability before connecting, whiledetect_capabilitiesruns insidepost_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 andUSEis issued.This change
Handle it where the failure actually surfaces, in
SQLServerAdapter:USEpath untouched.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._supports_cross_database_queries_override = False, so later lookups skip the doomed probe.USEat all — Azure rejects even that.USEstill propagate.disconnect()override closes the extra connections; a weakref callback clears the same bookkeeping if a connection is collected withoutdisconnect(), so theid()-keyed entries can't outlive their connection and bind a later one to the wrong database.connect()(no stored config, e.g. theMagicMocks in the existing tests) fall back to plainUSE.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 whoseUSEraises the Azure error — covers: fallback connection opened, reuse across lookups, one connection per database, noUSE/reconnect for the current database,disconnectclosing siblings, GC purging the bookkeeping, the capability flip, unrelated errors propagating, and regular SQL Server still usingUSEwith no extra connections.The same patch is applied to my installed 1.6.1 against a live Azure SQL Database server (
ad_interactiveauth); I'll report back once I've confirmed the explorer expands databases there.