From 0e03f0cc6337d67b91dc0dbf6d20eab2b45dd609 Mon Sep 17 00:00:00 2001 From: Mario Munoz Date: Mon, 20 Jan 2025 17:37:56 -0800 Subject: [PATCH 1/2] add hash to config classes --- advanced_alchemy/config/asyncio.py | 6 ++++++ advanced_alchemy/config/common.py | 9 +++++++++ advanced_alchemy/config/sync.py | 6 ++++++ 3 files changed, 21 insertions(+) diff --git a/advanced_alchemy/config/asyncio.py b/advanced_alchemy/config/asyncio.py index e2ed4955..654c4580 100644 --- a/advanced_alchemy/config/asyncio.py +++ b/advanced_alchemy/config/asyncio.py @@ -71,6 +71,12 @@ class SQLAlchemyAsyncConfig(GenericSQLAlchemyConfig[AsyncEngine, AsyncSession, a The configuration options are documented in the Alembic documentation. """ + def __hash__(self) -> int: + return super().__hash__() + + def __eq__(self, other: object) -> bool: + return super().__eq__(other) + @asynccontextmanager async def get_session( self, diff --git a/advanced_alchemy/config/common.py b/advanced_alchemy/config/common.py index 8620ddbc..dd57d378 100644 --- a/advanced_alchemy/config/common.py +++ b/advanced_alchemy/config/common.py @@ -3,6 +3,7 @@ from dataclasses import dataclass, field from pathlib import Path from typing import TYPE_CHECKING, Callable, ClassVar, Generic, Union, cast +from uuid import NAMESPACE_DNS, uuid3 from typing_extensions import TypeVar @@ -202,6 +203,14 @@ def __post_init__(self) -> None: event.listen(Session, "before_flush", touch_updated_timestamp) + def __hash__(self) -> int: + return hash((uuid3(NAMESPACE_DNS, str(self)), self.__class__.__name__, self.metadata)) + + def __eq__(self, other: object) -> bool: + if not isinstance(other, type(self)): + return False + return self.__hash__() == other.__hash__() + @property def engine_config_dict(self) -> dict[str, Any]: """Return the engine configuration as a dict. diff --git a/advanced_alchemy/config/sync.py b/advanced_alchemy/config/sync.py index 0010e48a..61cb292c 100644 --- a/advanced_alchemy/config/sync.py +++ b/advanced_alchemy/config/sync.py @@ -55,6 +55,12 @@ class SQLAlchemySyncConfig(GenericSQLAlchemyConfig[Engine, Session, sessionmaker The configuration options are documented in the Alembic documentation. """ + def __hash__(self) -> int: + return super().__hash__() + + def __eq__(self, other: object) -> bool: + return super().__eq__(other) + @contextmanager def get_session(self) -> Generator[Session, None, None]: """Get a session context manager. From b739347407c9d849acefa6884a672453197d22c7 Mon Sep 17 00:00:00 2001 From: Mario Munoz Date: Mon, 20 Jan 2025 17:52:55 -0800 Subject: [PATCH 2/2] add bind key to hash --- advanced_alchemy/config/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced_alchemy/config/common.py b/advanced_alchemy/config/common.py index dd57d378..78fa644d 100644 --- a/advanced_alchemy/config/common.py +++ b/advanced_alchemy/config/common.py @@ -204,7 +204,7 @@ def __post_init__(self) -> None: event.listen(Session, "before_flush", touch_updated_timestamp) def __hash__(self) -> int: - return hash((uuid3(NAMESPACE_DNS, str(self)), self.__class__.__name__, self.metadata)) + return hash((uuid3(NAMESPACE_DNS, str(self)), self.__class__.__name__, self.metadata, self.bind_key)) def __eq__(self, other: object) -> bool: if not isinstance(other, type(self)):