From 5e31502d42e0bf3f71d0251168d7d67fb76f2cfc Mon Sep 17 00:00:00 2001 From: Gupta Arpit Date: Fri, 8 Nov 2024 18:30:32 +0530 Subject: [PATCH] fix: Improve error message for SQLitePersister #417 --- burr/core/application.py | 4 ++++ burr/core/persistence.py | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/burr/core/application.py b/burr/core/application.py index 30c92b9a..06cc9f41 100644 --- a/burr/core/application.py +++ b/burr/core/application.py @@ -2207,6 +2207,10 @@ def with_state_persister( """ if on_every != "step": raise ValueError(f"on_every {on_every} not supported") + if hasattr(persister, 'initialize') and not persister.is_initialized(): + raise RuntimeError( + "Uninitialized persister. Make sure to call .initialize() before passing it to the ApplicationBuilder." + ) if not isinstance(persister, persistence.BaseStateSaver): self.lifecycle_adapters.append(persister) else: diff --git a/burr/core/persistence.py b/burr/core/persistence.py index 22c26174..966c0c52 100644 --- a/burr/core/persistence.py +++ b/burr/core/persistence.py @@ -164,6 +164,7 @@ def __init__( db_path, **connect_kwargs if connect_kwargs is not None else {} ) self.serde_kwargs = serde_kwargs or {} + self._initialized = False def create_table_if_not_exists(self, table_name: str): """Helper function to create the table where things are stored if it doesn't exist.""" @@ -192,6 +193,11 @@ def initialize(self): """Creates the table if it doesn't exist""" # Usage self.create_table_if_not_exists(self.table_name) + self._initialized = True + + def is_initialized(self) -> bool: + """Check if the persister is initialized.""" + return self._initialized def list_app_ids(self, partition_key: Optional[str], **kwargs) -> list[str]: partition_key = (