Skip to content

Conversation

@theradP
Copy link

@theradP theradP commented Jan 12, 2026

Feature added
Added connect_args to rx.Config to allow passing custom arguments to SQLAlchemy create_engine.

Implemented steps

  • Updated BaseConfig dataclass with the new argument "connect_args"
  • Updated get_engine_args function to include connect_args from configuration and ensured check_same_thread argument is preserved.

Need for change
Allows supporting private key authentication for Snowflake and other advanced DB configurations

@theradP
Copy link
Author

theradP commented Jan 12, 2026

closes #6072

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Jan 12, 2026

Greptile Overview

Greptile Summary

This PR adds support for custom SQLAlchemy connection arguments through a new connect_args configuration field. The implementation enables advanced database configurations like Snowflake private key authentication.

Changes Made

Configuration (reflex/config.py)

  • Added connect_args: dict[str, Any] field to BaseConfig with proper default factory initialization

Database Engine (reflex/model.py)

  • Updated get_engine_args() to merge user-provided connect_args with Reflex's internal requirements
  • Preserved existing SQLite-specific behavior: check_same_thread: False is always set for SQLite databases (required for admin dashboard)
  • Both sync (create_engine) and async (create_async_engine) database engines benefit from this change

Testing (tests/units/test_connect_args.py)

  • Comprehensive test coverage for PostgreSQL and SQLite scenarios
  • Verifies custom args are passed through correctly
  • Confirms SQLite-specific settings are preserved alongside custom args

Implementation Quality

The implementation is solid with proper handling of edge cases:

  • Uses .copy() to avoid mutating the original config dictionary
  • Correctly handles empty dictionaries and preserves SQLite requirements
  • Short-circuit evaluation prevents errors with falsy values
  • Both sync and async engines use the same get_engine_args() function, ensuring consistency

Minor Improvement

The Config class docstring could be updated to mention connect_args in the "Key Configuration Areas" section for better discoverability.

Confidence Score: 4/5

  • This PR is safe to merge with only minor documentation improvements suggested
  • The implementation is well-designed with proper edge case handling, comprehensive tests, and no logic errors or security issues. The score is 4/5 (not 5/5) only due to a minor documentation gap in the Config class docstring - the new field should be mentioned in the "Key Configuration Areas" section for better discoverability
  • No files require special attention - all changes are straightforward and well-tested

Important Files Changed

File Analysis

Filename Score Overview
reflex/config.py 5/5 Added connect_args field to BaseConfig for passing custom SQLAlchemy engine arguments; implementation is safe with proper default factory
reflex/model.py 4/5 Modified get_engine_args to merge user connect_args with SQLite-specific settings; logic is sound but documentation could be clearer
tests/units/test_connect_args.py 5/5 Comprehensive tests covering PostgreSQL and SQLite cases with and without custom connect_args; all edge cases properly tested

Sequence Diagram

sequenceDiagram
    participant User
    participant Config
    participant Model
    participant SQLAlchemy

    User->>Config: Set connect_args in rxconfig.py
    Note over Config: connect_args = {"application_name": "myapp"}
    
    User->>Model: Request database connection
    Model->>Config: get_config()
    Config-->>Model: Returns config with connect_args
    
    Model->>Model: get_engine_args(url)
    Note over Model: Copy connect_args from config
    
    alt URL is SQLite
        Model->>Model: Add check_same_thread: False
        Note over Model: Required for admin dashboard
    end
    
    Model->>Model: Merge connect_args into kwargs
    Model->>SQLAlchemy: create_engine(url, **kwargs)
    Note over SQLAlchemy: Receives custom connect_args
    SQLAlchemy-->>Model: Returns engine with custom settings
    Model-->>User: Database connection ready
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Jan 12, 2026

Additional Comments (1)

reflex/config.py
The new connect_args field should be documented in the "Key Configuration Areas" section. This field allows users to pass custom connection arguments to SQLAlchemy's create_engine, which is important for advanced database configurations like Snowflake private key authentication.

    - **Database**: `db_url`, `async_db_url`, `connect_args`, `redis_url`

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: reflex/config.py
Line: 318:318

Comment:
The new `connect_args` field should be documented in the "Key Configuration Areas" section. This field allows users to pass custom connection arguments to SQLAlchemy's create_engine, which is important for advanced database configurations like Snowflake private key authentication.

```suggestion
    - **Database**: `db_url`, `async_db_url`, `connect_args`, `redis_url`
```

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

@codspeed-hq
Copy link

codspeed-hq bot commented Jan 14, 2026

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing theradP:feature/set_connect_args (5075b84) with main (ce9b21a)

Summary

✅ 8 untouched benchmarks

Copy link
Collaborator

@masenf masenf left a comment

Choose a reason for hiding this comment

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

the team does not want to take the patch at this time to avoid adding yet another configuration knob for a special case which expands API surface area and testing matrix.

for custom behavior, the current recommendation is to implement your own rx.session-like helper in your app that creates the sqlalchemy connection and use that in your app.

in this particular case, i agree it would be nicer if that didn't involve also reimplementing get_engine and get_engine_args. there's also the wrinkle of how deeply integrated the db subcommands and alembic are in the framework. we're actually moving away from the reflex db * commands and will be recommending the direct use of alembic by end users for managing db migrations prior to our 1.0 release.

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