Allow all known driver options to be passed on connection creation#6
Merged
Conversation
Ported from MasoniteFramework/orm#965. Connection classes now partition the "options" config dict instead of cherry-picking a hardcoded subset: - Postgres: keys that are valid psycopg2.connect() kwargs are passed through directly; all remaining keys are treated as PostgreSQL GUC parameters and appended to the options string as "-c key=value" (alongside the schema search_path). Especially useful for advanced connection details like timeouts. - SQLite: keys that are valid sqlite3.connect() kwargs are passed through; remaining keys are applied as PRAGMA statements after the connection opens. - MSSQL: known options keep their canonical ODBC mapping; any extra keys are appended verbatim as Key=Value pairs in the pyodbc connection string. - Port normalization unified across drivers (int(port) if port else None).
- Postgres: drop "async" from the psycopg2 pass-through whitelist — an asynchronous connection cannot be used by this synchronous ORM. - SQLite: validate leftover option keys are identifiers before interpolating them into PRAGMA statements, failing loudly on typos. - MSSQL: omit the port from the SERVER= segment when not configured instead of emitting "SERVER=host,None". - Add unit tests for the option partitioning of all three drivers (the original PR shipped without tests).
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.
What this does
Connection classes previously cherry-picked a hardcoded subset of the
optionsconfig dict and silently dropped everything else. They now partitionoptionsso every known driver argument is honored and the rest is passed through in the driver's native mechanism — especially useful for Postgres connections needing advanced details like timeouts:psycopg2.connect()kwargs (sslmode,connect_timeout,keepalives*,application_name, …) pass through directly; all remaining keys are treated as PostgreSQL GUC parameters and appended to theoptionsstring as-c key=value(alongside the schemasearch_path, which an explicitsearch_pathoption can now override).sqlite3.connect()kwargs (timeout,isolation_level,check_same_thread, …) pass through; remaining keys are applied asPRAGMA key = valueafter connecting (e.g.journal_mode: WAL).Key=Valuepairs in the pyodbc connection string (e.g.Encrypt,TrustServerCertificate).**optionsstraight intopymysql.connect(), so it only gets the unified port normalization.Hardening added on top of the original PR
asyncfrom the psycopg2 pass-through whitelist — an asynchronous connection cannot be used by this synchronous ORM; allowing it would produce confusing breakage at query time.PRAGMAstatement, so typos and malformed keys fail loudly at connect time instead of producing broken SQL.SERVER=segment when not configured, instead of emittingSERVER=host,None.Tests
Full suite: 1045 passed, 1 pre-existing failure (requires a live Postgres server — fails identically on
3.x).