Álvaro Valenzuela Valdes commited on
Commit
6ebf594
·
1 Parent(s): 0295563

🚀 Fix: Move SQLite to /tmp on Linux for HF compatibility

Browse files
Files changed (2) hide show
  1. backend/app/database.py +10 -1
  2. backend/app/main.py +18 -4
backend/app/database.py CHANGED
@@ -4,8 +4,17 @@ from sqlalchemy.orm import sessionmaker
4
  from app.config import settings
5
 
6
  import os
 
 
7
  BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
8
- default_db_path = f"sqlite:///{os.path.join(BASE_DIR, 'andesops.db')}"
 
 
 
 
 
 
 
9
  SQLALCHEMY_DATABASE_URL = settings.database_url or default_db_path
10
 
11
  # SQLite specific config for FastAPI multi-threading
 
4
  from app.config import settings
5
 
6
  import os
7
+ import platform
8
+
9
  BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
10
+
11
+ # Use /tmp on Linux (HF Spaces) to ensure write permissions
12
+ if platform.system() == "Linux":
13
+ db_path = "/tmp/andesops.db"
14
+ else:
15
+ db_path = os.path.join(BASE_DIR, "andesops.db")
16
+
17
+ default_db_path = f"sqlite:///{db_path}"
18
  SQLALCHEMY_DATABASE_URL = settings.database_url or default_db_path
19
 
20
  # SQLite specific config for FastAPI multi-threading
backend/app/main.py CHANGED
@@ -1,21 +1,35 @@
1
  import sys
2
  import os
 
 
 
 
 
3
  sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
4
 
5
  from fastapi import FastAPI
6
  from fastapi.middleware.cors import CORSMiddleware
7
  from app.routers import analysis, company, health, tenders, documents, oc
8
- from app.database import engine, Base, SessionLocal
9
  from app.models.tender import TenderModel
10
  from app.models.analysis import AnalysisHistoryModel
11
  from app.models.company import CompanyProfileModel
12
  from app.models.oc import OCModel
13
  from app.config import settings
14
- from datetime import datetime, timedelta
15
- import json
 
 
 
 
 
 
16
 
17
  # Create tables
18
- Base.metadata.create_all(bind=engine)
 
 
 
19
 
20
  app = FastAPI(title="AndesOps AI")
21
 
 
1
  import sys
2
  import os
3
+ import json
4
+ import shutil
5
+ from datetime import datetime, timedelta
6
+
7
+ # Ensure parent directory is in path
8
  sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9
 
10
  from fastapi import FastAPI
11
  from fastapi.middleware.cors import CORSMiddleware
12
  from app.routers import analysis, company, health, tenders, documents, oc
13
+ from app.database import engine, Base, SessionLocal, SQLALCHEMY_DATABASE_URL
14
  from app.models.tender import TenderModel
15
  from app.models.analysis import AnalysisHistoryModel
16
  from app.models.company import CompanyProfileModel
17
  from app.models.oc import OCModel
18
  from app.config import settings
19
+
20
+ # Copy database to /tmp if needed (Linux/HF Spaces)
21
+ if SQLALCHEMY_DATABASE_URL.startswith("sqlite:////tmp/"):
22
+ src_db = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "andesops.db")
23
+ dest_db = "/tmp/andesops.db"
24
+ if os.path.exists(src_db) and not os.path.exists(dest_db):
25
+ print(f"!!! HF DETECTED: Copying initial database from {src_db} to {dest_db} !!!")
26
+ shutil.copy2(src_db, dest_db)
27
 
28
  # Create tables
29
+ try:
30
+ Base.metadata.create_all(bind=engine)
31
+ except Exception as e:
32
+ print(f"!!! Database creation error: {e} !!!")
33
 
34
  app = FastAPI(title="AndesOps AI")
35