Skip to content

Conversation

@aabmets
Copy link
Contributor

@aabmets aabmets commented Sep 5, 2025

Legacy Python projects which use psycopg2 driver cannot currently use py-pglite, because the
library does not support parametrizing the sqlalchemy connection string with driver name.

This PR fixes this issue by allowing a driver name argument to be specified when calling the
get_connection_string method of the PGliteConfig class and when calling the get_engine
method of the SQLAlchemyPGliteManager class.

The default value of this parameter is "psycopg", so this change is compatible with all existing
usages of this library and does not break any implementations. Please consider merging this PR and
releasing a new minor version, as my work on a legacy Python project depends on this change.

Here's some context of what I'm currently doing:

def pytest_sessionstart(session: pytest.Session) -> None:
    pglite_config = PGliteConfig()

    # get_engine() does not support driver arg, so we use monkeypatch
    orig_conn_str = pglite_config.get_connection_string()
    patched_conn_str = orig_conn_str.replace("psycopg", "psycopg2")
    mp = pytest.MonkeyPatch()
    mp.setattr(
        target=pglite_config,
        name="get_connection_string",
        value=lambda: patched_conn_str,
    )
    pglite_manager = SQLAlchemyPGliteManager(config=pglite_config)
    pglite_manager.start()

    engine = pglite_manager.get_engine()

    script_loc = (TESTS_DIR.parent / 'migration_scripts').as_posix()
    alembic_cfg = AlembicConfig()
    alembic_cfg.set_main_option('script_location', script_loc)
    
    with engine.connect() as conn:
        alembic_cfg.attributes['connection'] = conn
        alembic_command.upgrade(alembic_cfg, 'head')

Instead of using MonkeyPatch to achieve the desired result, this PR would enable to use the following elegant solution:

engine = pglite_manager.get_engine(driver="psycopg2")

Added driver parameter to get_connection_string method
@aabmets aabmets changed the title Added driver parameter to get_connection_string method. Added driver parameter to get_connection_string and get_engine methods Sep 5, 2025
@codecov
Copy link

codecov bot commented Sep 16, 2025

Codecov Report

❌ Patch coverage is 20.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.60%. Comparing base (e5ba3bd) to head (acae80f).

Files with missing lines Patch % Lines
src/py_pglite/config.py 33.33% 2 Missing ⚠️
src/py_pglite/sqlalchemy/manager.py 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #37      +/-   ##
==========================================
- Coverage   67.66%   67.60%   -0.06%     
==========================================
  Files          15       15              
  Lines        1206     1207       +1     
==========================================
  Hits          816      816              
- Misses        390      391       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Owner

@wey-gu wey-gu left a comment

Choose a reason for hiding this comment

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

Many thanks for the great contribution!

@wey-gu wey-gu merged commit d698ea4 into wey-gu:main Sep 16, 2025
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.

2 participants