Skip to content

Commit 2d2e978

Browse files
committed
up
1 parent aad84a4 commit 2d2e978

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dependencies = [
99
"jinja2>=3.1.4",
1010
"pydantic-ai>=0.0.26",
1111
"pydantic-settings>=2.6.1",
12+
"pydantic[email]>=2.10.2",
1213
"rich>=13.9.4",
1314
"sqlalchemy[asyncio]>=2.0.36",
1415
"typer>=0.15.1",

src/marvin/__init__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# necessary imports
44
from marvin.settings import settings
5-
from marvin.database import ensure_sqlite_memory_tables_exist
5+
from marvin.database import init_database
66

77
# core classes
88
from marvin.thread import Thread
@@ -31,7 +31,8 @@
3131
from marvin.fns.summarize import summarize, summarize_async
3232
from marvin.fns.plan import plan, plan_async
3333

34-
ensure_sqlite_memory_tables_exist()
34+
# Initialize the database on import
35+
init_database()
3536

3637
__version__ = _version("marvin")
3738

src/marvin/database.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -254,16 +254,8 @@ def ensure_sqlite_memory_tables_exist():
254254
created if they don't exist.
255255
"""
256256

257-
db_url = settings.database_url
258-
if db_url is None:
259-
raise ValueError("Database URL is not configured")
260-
261-
if db_url == ":memory:" or db_url.endswith(":memory:"):
262-
# We're using run_sync from another module, so keep it as is
263-
asyncio.run(create_db_and_tables(force=False))
264-
else:
265-
# For non-memory databases, ensure tables exist
266-
asyncio.run(create_db_and_tables(force=False))
257+
# Call init_database which handles all database types
258+
init_database()
267259

268260

269261
@asynccontextmanager
@@ -303,3 +295,18 @@ async def create_db_and_tables(*, force: bool = False) -> None:
303295

304296
await conn.run_sync(Base.metadata.create_all)
305297
logger.debug("Database tables created.")
298+
299+
300+
def init_database():
301+
"""Initialize the database.
302+
303+
This function should be called during application startup to ensure
304+
database tables exist before they are accessed.
305+
"""
306+
from marvin.utilities.logging import get_logger
307+
308+
logger = get_logger(__name__)
309+
310+
logger.debug("Initializing database...")
311+
asyncio.run(create_db_and_tables(force=False))
312+
logger.debug("Database initialization complete.")

0 commit comments

Comments
 (0)