Skip to content

sqlalchemy fixtures do not work with only psycopg2 #50

@ciferkey

Description

@ciferkey

The .get_engine methods on the managers allow you to set the driver through an arg.

However, wait_for_ready on SQLAlchemyPGliteManager calls get_engine without passing driver:

def wait_for_ready(self, max_retries: int = 15, delay: float = 1.0) -> bool:
"""Wait for database to be ready and responsive.
NOTE: This method requires SQLAlchemy to be installed.
Args:
max_retries: Maximum number of connection attempts
delay: Delay between attempts in seconds
Returns:
True if database becomes ready, False otherwise
Raises:
ImportError: If SQLAlchemy is not installed
"""
try:
from sqlalchemy import text
except ImportError as e:
raise ImportError(
"SQLAlchemy is required for wait_for_ready(). "
"Install with: pip install py-pglite[sqlalchemy]"
) from e
# Use the shared engine that get_engine() creates
engine = self.get_engine(pool_pre_ping=False)

Causing this failure:

pglite_config = PGliteConfig(timeout=30, cleanup_on_exit=True, log_level='INFO', socket_path='/var/folders/vr/cc8sq12930zbfwtzfrjzj6g4...k=True, auto_install_deps=True, extensions=None, node_options=None, use_tcp=F
alse, tcp_host='127.0.0.1', tcp_port=5432)

    @pytest.fixture(scope="session")
    def pglite_sqlalchemy_manager(
        pglite_config: PGliteConfig,
    ) -> Generator[SQLAlchemyPGliteManager, None, None]:
        """Pytest fixture providing an SQLAlchemy-enabled PGlite manager."""
        manager = SQLAlchemyPGliteManager(pglite_config)
        manager.start()

        # Wait for database to be ready
>       if not manager.wait_for_ready():

../.venv/lib/python3.12/site-packages/py_pglite/sqlalchemy/fixtures.py:55:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../.venv/lib/python3.12/site-packages/py_pglite/sqlalchemy/manager.py:132: in wait_for_ready
    engine = self.get_engine(pool_pre_ping=False)
../.venv/lib/python3.12/site-packages/py_pglite/sqlalchemy/manager.py:103: in get_engine
    self._shared_engine = create_engine(
../.venv/lib/python3.12/site-packages/sqlalchemy/util/deprecations.py:281: in warned
    return fn(*args, **kwargs)  # type: ignore[no-any-return]
../.venv/lib/python3.12/site-packages/sqlalchemy/engine/create.py:617: in create_engine
    dbapi = dbapi_meth(**dbapi_args)

I locally tried to patch the lib to:

`engine = self.get_engine(driver="psycopg2", pool_pre_ping=False)`


_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

cls = <class 'sqlalchemy.dialects.postgresql.psycopg.PGDialect_psycopg'>

    @classmethod
    def import_dbapi(cls):
>       import psycopg
E       ModuleNotFoundError: No module named 'psycopg'

I locally patched the library to have:

engine = self.get_engine(driver="psycopg2", pool_pre_ping=False)

and the fixture starts up fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions