Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support more sessionmaker kwargs #113

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions cacholote/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,20 @@ def make_cache_dir(self) -> "Settings":
fs.mkdirs(urlpath, exist_ok=True)
return self

@pydantic.model_validator(mode="after")
def check_mutually_exclusive(self) -> "Settings":
mutually_exclusive = (self.sessionmaker, self.cache_db_urlpath)
if all(mutually_exclusive) or not any(mutually_exclusive):
raise ValueError(
"Provide either `sessionmaker` or `cache_db_urlpath` (mutually exclusive)."
)
return self

@property
def instantiated_sessionmaker(self) -> sa.orm.sessionmaker: # type: ignore[type-arg]
if self.sessionmaker is None:
if self.cache_db_urlpath is None:
raise ValueError("Provide either `sessionmaker` or `cache_db_urlpath`.")
self.sessionmaker = database.cached_sessionmaker(
self.cache_db_urlpath, **self.create_engine_kwargs
)
self.cache_db_urlpath = None
self.create_engine_kwargs = {}
elif self.cache_db_urlpath is not None:
raise ValueError(
"`sessionmaker` and `cache_db_urlpath` are mutually exclusive."
)
return self.sessionmaker

@property
Expand Down
30 changes: 27 additions & 3 deletions cacholote/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import functools
import json
import warnings
from typing import Any
from typing import Any, Dict

import sqlalchemy as sa
import sqlalchemy.orm
Expand Down Expand Up @@ -77,8 +77,32 @@ def _commit_or_rollback(session: sa.orm.Session) -> None:
session.rollback()


def _encode_kwargs(**kwargs: Any) -> Dict[str, Any]:
encoded_kwargs = {}
for key, value in kwargs.items():
if isinstance(value, dict):
encoded_kwargs["_encoded_" + key] = json.dumps(value)
else:
encoded_kwargs[key] = value
return encoded_kwargs


def _decode_kwargs(**kwargs: Any) -> Dict[str, Any]:
decoded_kwargs = {}
for key, value in kwargs.items():
if key.startswith("_encoded_"):
decoded_kwargs[key.replace("_encoded_", "", 1)] = json.loads(value)
else:
decoded_kwargs[key] = value
return decoded_kwargs


@functools.lru_cache()
def cached_sessionmaker(url: str, **kwargs: Any) -> sa.orm.sessionmaker: # type: ignore[type-arg]
engine = sa.create_engine(url, **kwargs)
def _cached_sessionmaker(url: str, **kwargs: Any) -> sa.orm.sessionmaker: # type: ignore[type-arg]
engine = sa.create_engine(url, **_decode_kwargs(**kwargs))
Base.metadata.create_all(engine)
return sa.orm.sessionmaker(engine)


def cached_sessionmaker(url: str, **kwargs: Any) -> sa.orm.sessionmaker: # type: ignore[type-arg]
return _cached_sessionmaker(url, **_encode_kwargs(**kwargs))
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def set_cache(
) -> Iterator[str]:
param = getattr(request, "param", "file")
if param.lower() == "cads":
database.cached_sessionmaker.cache_clear()
database._cached_sessionmaker.cache_clear()
test_bucket_name = "test-bucket"
client_kwargs = create_test_bucket(s3_server, test_bucket_name)
with config.set(
Expand Down
6 changes: 6 additions & 0 deletions tests/test_01_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,9 @@ def test_set_expiration(
) -> None:
with raises:
config.set(expiration=expiration)


def test_create_engine_dict_kwargs() -> None:
old_session_maker = config.get().instantiated_sessionmaker
config.set(create_engine_kwargs={"connect_args": {"timeout": 30}})
assert config.get().instantiated_sessionmaker is not old_session_maker
Loading