Skip to content

Fix URL encoding and optimize database connection handling#1

Merged
NihaalNO merged 1 commit into
mainfrom
claude/beautiful-galileo-WVFYt
Jun 1, 2026
Merged

Fix URL encoding and optimize database connection handling#1
NihaalNO merged 1 commit into
mainfrom
claude/beautiful-galileo-WVFYt

Conversation

@NihaalNO

@NihaalNO NihaalNO commented Jun 1, 2026

Copy link
Copy Markdown
Owner

Summary

This PR addresses URL encoding issues in database connection strings and optimizes database connection handling by removing unnecessary connection pool configuration.

Key Changes

  • URL Encoding Fix: Changed from quote_plus() to quote(safe='') for encoding database credentials and database name in connection strings. This prevents issues where SQLAlchemy's unquote() function doesn't convert '+' back to spaces, which could cause authentication failures with special characters.
  • Connection Pooling: Removed pool_pre_ping=True from engine creation in both schema_service.py and sql_executor.py to reduce unnecessary overhead.
  • Connection Reuse: Modified get_external_schema() to use a single persistent connection via engine.connect() context manager instead of creating new connections for each inspector call, improving performance during schema reflection.

Implementation Details

  • The connection string now properly encodes the database name in addition to user and password
  • Added clarifying comments explaining why quote() is preferred over quote_plus() for SQLAlchemy compatibility
  • Schema inspection now batches all reflection calls within a single connection context, reducing connection overhead

https://claude.ai/code/session_016qjAVBXoVuhQD9MNdHQ6Fd

1. Replace quote_plus with quote(safe='') for URL-encoding credentials
   in the connection string. SQLAlchemy decodes with urllib.parse.unquote
   (not unquote_plus), so quote_plus-encoded spaces ('+') were passed
   literally to psycopg2 and caused authentication failures.

2. Use a single engine.connect() context in get_external_schema() and
   inspect(conn) instead of inspect(engine). In SQLAlchemy 2.0, inspect(engine)
   opens a new DB connection for every reflection method call (get_table_names,
   get_columns per table), leading to N+1 connection attempts. Using a
   single connection context eliminates this overhead and gives a well-defined
   connection lifecycle.

3. Remove pool_pre_ping=True from both get_external_schema() and
   execute_sql_on_external(). These engines are created, used once, and
   disposed — pool_pre_ping adds a redundant SELECT 1 round-trip before
   every use. On cold Neon instances this extra round-trip consumed part of
   the 10-second connect_timeout budget, causing spurious timeouts.

https://claude.ai/code/session_016qjAVBXoVuhQD9MNdHQ6Fd
Copilot AI review requested due to automatic review settings June 1, 2026 16:06
@NihaalNO
NihaalNO merged commit b72c33c into main Jun 1, 2026
1 of 2 checks passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

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