From 4de94ca3dc661733de802c7bb2052faa48590a85 Mon Sep 17 00:00:00 2001 From: Alejandro Ponce Date: Tue, 3 Dec 2024 15:03:14 +0200 Subject: [PATCH 1/3] Avoid conflicts when schema change --- src/codegate/cli.py | 2 ++ src/codegate/db/connection.py | 11 ++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/codegate/cli.py b/src/codegate/cli.py index ec32a89d..49b798c1 100644 --- a/src/codegate/cli.py +++ b/src/codegate/cli.py @@ -9,6 +9,7 @@ from codegate.codegate_logging import LogFormat, LogLevel, setup_logging from codegate.config import Config, ConfigurationError +from codegate.db.connection import init_db_sync from codegate.server import init_app @@ -164,6 +165,7 @@ def serve( }, ) + init_db_sync() app = init_app() import uvicorn diff --git a/src/codegate/db/connection.py b/src/codegate/db/connection.py index 34cb67e5..e68bd2d2 100644 --- a/src/codegate/db/connection.py +++ b/src/codegate/db/connection.py @@ -36,10 +36,6 @@ def __init__(self, sqlite_path: Optional[str] = None): self._async_db_engine = create_async_engine(**engine_dict) self._db_engine = create_engine(**engine_dict) - if not self.does_db_exist(): - logger.info(f"Database does not exist at {self._db_path}. Creating..") - asyncio.run(self.init_db()) - def does_db_exist(self): return self._db_path.is_file() @@ -181,7 +177,12 @@ async def record_output_non_stream( def init_db_sync(): """DB will be initialized in the constructor in case it doesn't exist.""" - DbRecorder() + db = DbRecorder() + # Remove the DB file if exists for the moment to not cause issues at schema change. + # We can replace this in the future with migrations or something similar. + if db.does_db_exist(): + db._db_path.unlink() + asyncio.run(db.init_db()) if __name__ == "__main__": From 939a464aa244e2174cb5458f18deb63a5ed5d497 Mon Sep 17 00:00:00 2001 From: Alejandro Ponce Date: Tue, 3 Dec 2024 15:05:30 +0200 Subject: [PATCH 2/3] remove debug loggging line --- src/codegate/db/connection.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/codegate/db/connection.py b/src/codegate/db/connection.py index e68bd2d2..27d320b1 100644 --- a/src/codegate/db/connection.py +++ b/src/codegate/db/connection.py @@ -149,7 +149,6 @@ async def record_output_stream( if output_chunks: # Record the output chunks output_str = json.dumps(output_chunks) - logger.info(f"Recorded chunks: {output_chunks}. Str: {output_str}") await self._record_output(prompt, output_str) async def record_output_non_stream( From 1a614f0b4ea7f2f08581037e246717d9241fe761 Mon Sep 17 00:00:00 2001 From: Alejandro Ponce Date: Tue, 3 Dec 2024 15:12:55 +0200 Subject: [PATCH 3/3] add init of DB for tests --- src/codegate/db/connection.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/codegate/db/connection.py b/src/codegate/db/connection.py index 27d320b1..098c3007 100644 --- a/src/codegate/db/connection.py +++ b/src/codegate/db/connection.py @@ -35,6 +35,9 @@ def __init__(self, sqlite_path: Optional[str] = None): } self._async_db_engine = create_async_engine(**engine_dict) self._db_engine = create_engine(**engine_dict) + if not self.does_db_exist(): + logger.info(f"Database does not exist at {self._db_path}. Creating..") + asyncio.run(self.init_db()) def does_db_exist(self): return self._db_path.is_file()