Zok213 commited on
Commit
b93719a
·
1 Parent(s): e74e546

Fix: Add SQLite fallback when DATABASE_URL not set

Browse files
Files changed (1) hide show
  1. src/database/db.py +15 -3
src/database/db.py CHANGED
@@ -6,11 +6,23 @@ from dotenv import load_dotenv
6
 
7
  load_dotenv()
8
 
 
9
  DATABASE_URL = os.getenv("DATABASE_URL")
10
- if DATABASE_URL and DATABASE_URL.startswith("postgres://"):
11
- DATABASE_URL = DATABASE_URL.replace("postgres://", "postgresql://", 1)
12
 
13
- engine = create_engine(DATABASE_URL)
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
15
 
16
  Base = declarative_base()
 
6
 
7
  load_dotenv()
8
 
9
+ # Get DATABASE_URL from environment, fallback to SQLite for local/HuggingFace deployment
10
  DATABASE_URL = os.getenv("DATABASE_URL")
 
 
11
 
12
+ if DATABASE_URL:
13
+ # Fix Heroku-style postgres:// URLs
14
+ if DATABASE_URL.startswith("postgres://"):
15
+ DATABASE_URL = DATABASE_URL.replace("postgres://", "postgresql://", 1)
16
+ else:
17
+ # Fallback to SQLite for HuggingFace Spaces / local development without database
18
+ DATABASE_URL = "sqlite:///./data/road_damage.db"
19
+ print(f"[DB] No DATABASE_URL set, using SQLite: {DATABASE_URL}")
20
+
21
+ # Create engine
22
+ engine = create_engine(
23
+ DATABASE_URL,
24
+ connect_args={"check_same_thread": False} if DATABASE_URL.startswith("sqlite") else {}
25
+ )
26
  SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
27
 
28
  Base = declarative_base()