Skip to content

Commit 23d6cae

Browse files
committed
fix: support legacy request log schemas
1 parent 9016bd7 commit 23d6cae

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/core/database.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,8 @@ async def _migrate_request_logs(self, db):
685685
if not await self._column_exists(db, "request_logs", "progress"):
686686
await db.execute("ALTER TABLE request_logs ADD COLUMN progress INTEGER DEFAULT 0")
687687
if not await self._column_exists(db, "request_logs", "updated_at"):
688-
await db.execute("ALTER TABLE request_logs ADD COLUMN updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
688+
await db.execute("ALTER TABLE request_logs ADD COLUMN updated_at TIMESTAMP")
689+
await db.execute("UPDATE request_logs SET updated_at = created_at WHERE updated_at IS NULL")
689690
except Exception as e:
690691
print(f"?? request_logs?????: {e}")
691692
# Continue even if migration fails
@@ -1238,6 +1239,12 @@ async def get_logs(self, limit: int = 100, token_id: Optional[int] = None, inclu
12381239
async with aiosqlite.connect(self.db_path) as db:
12391240
db.row_factory = aiosqlite.Row
12401241
payload_columns = "rl.request_body, rl.response_body," if include_payload else ""
1242+
has_status_text = await self._column_exists(db, "request_logs", "status_text")
1243+
has_progress = await self._column_exists(db, "request_logs", "progress")
1244+
has_updated_at = await self._column_exists(db, "request_logs", "updated_at")
1245+
status_text_column = "rl.status_text," if has_status_text else "'' as status_text,"
1246+
progress_column = "rl.progress," if has_progress else "0 as progress,"
1247+
updated_at_column = "rl.updated_at," if has_updated_at else "rl.created_at as updated_at,"
12411248

12421249
if token_id:
12431250
cursor = await db.execute(f"""
@@ -1248,10 +1255,10 @@ async def get_logs(self, limit: int = 100, token_id: Optional[int] = None, inclu
12481255
{payload_columns}
12491256
rl.status_code,
12501257
rl.duration,
1251-
rl.status_text,
1252-
rl.progress,
1258+
{status_text_column}
1259+
{progress_column}
12531260
rl.created_at,
1254-
rl.updated_at,
1261+
{updated_at_column}
12551262
t.email as token_email,
12561263
t.name as token_username
12571264
FROM request_logs rl
@@ -1269,10 +1276,10 @@ async def get_logs(self, limit: int = 100, token_id: Optional[int] = None, inclu
12691276
{payload_columns}
12701277
rl.status_code,
12711278
rl.duration,
1272-
rl.status_text,
1273-
rl.progress,
1279+
{status_text_column}
1280+
{progress_column}
12741281
rl.created_at,
1275-
rl.updated_at,
1282+
{updated_at_column}
12761283
t.email as token_email,
12771284
t.name as token_username
12781285
FROM request_logs rl
@@ -1288,7 +1295,13 @@ async def get_log_detail(self, log_id: int) -> Optional[Dict[str, Any]]:
12881295
"""Get single request log detail including payload fields"""
12891296
async with aiosqlite.connect(self.db_path) as db:
12901297
db.row_factory = aiosqlite.Row
1291-
cursor = await db.execute("""
1298+
has_status_text = await self._column_exists(db, "request_logs", "status_text")
1299+
has_progress = await self._column_exists(db, "request_logs", "progress")
1300+
has_updated_at = await self._column_exists(db, "request_logs", "updated_at")
1301+
status_text_column = "rl.status_text," if has_status_text else "'' as status_text,"
1302+
progress_column = "rl.progress," if has_progress else "0 as progress,"
1303+
updated_at_column = "rl.updated_at," if has_updated_at else "rl.created_at as updated_at,"
1304+
cursor = await db.execute(f"""
12921305
SELECT
12931306
rl.id,
12941307
rl.token_id,
@@ -1297,10 +1310,10 @@ async def get_log_detail(self, log_id: int) -> Optional[Dict[str, Any]]:
12971310
rl.response_body,
12981311
rl.status_code,
12991312
rl.duration,
1300-
rl.status_text,
1301-
rl.progress,
1313+
{status_text_column}
1314+
{progress_column}
13021315
rl.created_at,
1303-
rl.updated_at,
1316+
{updated_at_column}
13041317
t.email as token_email,
13051318
t.name as token_username
13061319
FROM request_logs rl

0 commit comments

Comments
 (0)