Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit c4318b2

Browse files
Merge pull request #173 from stacklok/change-db-setup
Avoid DB conflicts when schema change
2 parents 17b8e1e + 1a614f0 commit c4318b2

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/codegate/cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from codegate.codegate_logging import LogFormat, LogLevel, setup_logging
1111
from codegate.config import Config, ConfigurationError
12+
from codegate.db.connection import init_db_sync
1213
from codegate.server import init_app
1314

1415

@@ -164,6 +165,7 @@ def serve(
164165
},
165166
)
166167

168+
init_db_sync()
167169
app = init_app()
168170

169171
import uvicorn

src/codegate/db/connection.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ def __init__(self, sqlite_path: Optional[str] = None):
3535
}
3636
self._async_db_engine = create_async_engine(**engine_dict)
3737
self._db_engine = create_engine(**engine_dict)
38-
3938
if not self.does_db_exist():
4039
logger.info(f"Database does not exist at {self._db_path}. Creating..")
4140
asyncio.run(self.init_db())
@@ -153,7 +152,6 @@ async def record_output_stream(
153152
if output_chunks:
154153
# Record the output chunks
155154
output_str = json.dumps(output_chunks)
156-
logger.info(f"Recorded chunks: {output_chunks}. Str: {output_str}")
157155
await self._record_output(prompt, output_str)
158156

159157
async def record_output_non_stream(
@@ -181,7 +179,12 @@ async def record_output_non_stream(
181179

182180
def init_db_sync():
183181
"""DB will be initialized in the constructor in case it doesn't exist."""
184-
DbRecorder()
182+
db = DbRecorder()
183+
# Remove the DB file if exists for the moment to not cause issues at schema change.
184+
# We can replace this in the future with migrations or something similar.
185+
if db.does_db_exist():
186+
db._db_path.unlink()
187+
asyncio.run(db.init_db())
185188

186189

187190
if __name__ == "__main__":

0 commit comments

Comments
 (0)