Melika Kheirieh commited on
Commit
8143b24
·
1 Parent(s): 50487ce

fix(sqlite): add safe fallback and migrate debug prints to logging

Browse files
Files changed (1) hide show
  1. app/routers/nl2sql.py +23 -18
app/routers/nl2sql.py CHANGED
@@ -8,6 +8,7 @@ import time
8
  import uuid
9
  from typing import Any, Dict, Optional, Union, cast, Callable, Tuple
10
  import hashlib
 
11
 
12
  # --- Third-party ---
13
  from fastapi import APIRouter, HTTPException, UploadFile, File, Depends, Query
@@ -27,7 +28,7 @@ from nl2sql.pipeline_factory import (
27
  )
28
  from nl2sql.prom import REGISTRY
29
 
30
-
31
  api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
32
 
33
 
@@ -166,9 +167,6 @@ def _select_adapter(db_id: Optional[str]) -> Union[PostgresAdapter, SQLiteAdapte
166
 
167
  if db_id:
168
  cleanup_stale_dbs()
169
- import logging
170
-
171
- log = logging.getLogger(__name__)
172
 
173
  candidates = [
174
  Path("/tmp/nl2sql_dbs") / f"{db_id}.sqlite",
@@ -181,27 +179,34 @@ def _select_adapter(db_id: Optional[str]) -> Union[PostgresAdapter, SQLiteAdapte
181
 
182
  for candidate in candidates:
183
  if candidate.exists():
184
- log.info(f"Using DB file: {candidate}")
185
  return SQLiteAdapter(str(candidate))
186
 
187
  raise HTTPException(status_code=404, detail=f"db_id not found: {db_id}")
188
 
 
189
  default_path = Path(DEFAULT_SQLITE_PATH)
190
-
191
- print("=== DEBUG DEFAULT SQLITE ===")
192
- print("DEFAULT_SQLITE_PATH env:", DEFAULT_SQLITE_PATH)
193
- print("CWD:", os.getcwd())
194
- print("ABS PATH:", Path(DEFAULT_SQLITE_PATH).resolve())
195
- print("EXISTS?:", Path(DEFAULT_SQLITE_PATH).exists())
196
- print("LIST DIR:", os.listdir(os.getcwd()))
197
- print(
198
- "LIST DATA:",
199
- os.listdir("data") if os.path.exists("data") else "NO DATA DIRECTORY",
200
- )
201
 
202
  if not default_path.exists():
203
- raise HTTPException(status_code=500, detail="default SQLite DB not found")
204
- return SQLiteAdapter(str(default_path))
 
 
 
 
 
 
205
 
206
 
207
  # -------------------------------
 
8
  import uuid
9
  from typing import Any, Dict, Optional, Union, cast, Callable, Tuple
10
  import hashlib
11
+ import logging
12
 
13
  # --- Third-party ---
14
  from fastapi import APIRouter, HTTPException, UploadFile, File, Depends, Query
 
28
  )
29
  from nl2sql.prom import REGISTRY
30
 
31
+ log = logging.getLogger(__name__)
32
  api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
33
 
34
 
 
167
 
168
  if db_id:
169
  cleanup_stale_dbs()
 
 
 
170
 
171
  candidates = [
172
  Path("/tmp/nl2sql_dbs") / f"{db_id}.sqlite",
 
179
 
180
  for candidate in candidates:
181
  if candidate.exists():
182
+ log.info(f"Using DB file: {candidate}")
183
  return SQLiteAdapter(str(candidate))
184
 
185
  raise HTTPException(status_code=404, detail=f"db_id not found: {db_id}")
186
 
187
+ # -------- Default SQLite Logic --------
188
  default_path = Path(DEFAULT_SQLITE_PATH)
189
+ db_path = str(default_path)
190
+
191
+ log.debug("DEFAULT SQLITE DEBUG INFO:")
192
+ log.debug(f"DEFAULT_SQLITE_PATH env: {DEFAULT_SQLITE_PATH}")
193
+ log.debug(f"CWD: {os.getcwd()}")
194
+ log.debug(f"ABS PATH: {default_path.resolve()}")
195
+ log.debug(f"EXISTS?: {default_path.exists()}")
196
+ if os.path.exists("data"):
197
+ log.debug(f"LIST DATA: {os.listdir('data')}")
198
+ else:
199
+ log.debug("LIST DATA: NO DATA DIRECTORY")
200
 
201
  if not default_path.exists():
202
+ fallback = Path("data/demo.db")
203
+ if fallback.exists():
204
+ log.warning("Default sqlite missing; using fallback demo.db")
205
+ db_path = str(fallback)
206
+ else:
207
+ raise HTTPException(status_code=500, detail="no sqlite database found")
208
+
209
+ return SQLiteAdapter(db_path)
210
 
211
 
212
  # -------------------------------