Jotzy/backend/app/database.py

28 lines
828 B
Python
Raw Normal View History

2026-01-06 16:47:18 +00:00
import os
2025-11-23 09:08:01 +00:00
from sqlmodel import Session, SQLModel, create_engine # type: ignore
2026-01-06 16:51:05 +00:00
# Get database URL from environment, with proper fallback
DATABASE_URL = os.getenv("DATABASE_URL")
# If DATABASE_URL is not set or empty, use default SQLite
if not DATABASE_URL or DATABASE_URL.strip() == "":
DATABASE_URL = "sqlite:////app/data/notes.db"
print(f"WARNING: DATABASE_URL not set, using default: {DATABASE_URL}")
else:
print(f"Using DATABASE_URL: {DATABASE_URL}")
2026-01-06 16:47:18 +00:00
# Only use check_same_thread for SQLite
connect_args = {"check_same_thread": False} if DATABASE_URL.startswith("sqlite") else {}
2025-11-23 09:08:01 +00:00
2026-01-06 16:47:18 +00:00
engine = create_engine(DATABASE_URL, echo=True, connect_args=connect_args)
2025-11-23 09:08:01 +00:00
def create_db_and_tables():
SQLModel.metadata.create_all(engine)
def get_session():
with Session(engine) as session:
yield session