42 lines
1.0 KiB
Python
42 lines
1.0 KiB
Python
import os
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from app.core.config import settings
|
|
|
|
# Use the DB_DIR from settings
|
|
DB_DIR = settings.DB_DIR
|
|
DB_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
# Ensure the directory has appropriate permissions
|
|
try:
|
|
os.chmod(DB_DIR, 0o777)
|
|
except Exception:
|
|
print(f"Warning: Could not set permissions on {DB_DIR}")
|
|
|
|
# Create the database file if it doesn't exist
|
|
DB_FILE = DB_DIR / "db.sqlite"
|
|
if not DB_FILE.exists():
|
|
DB_FILE.touch()
|
|
try:
|
|
os.chmod(DB_FILE, 0o666) # Read-write permissions for everyone
|
|
except Exception:
|
|
print(f"Warning: Could not set permissions on {DB_FILE}")
|
|
|
|
SQLALCHEMY_DATABASE_URL = settings.SQLALCHEMY_DATABASE_URL
|
|
|
|
engine = create_engine(
|
|
SQLALCHEMY_DATABASE_URL,
|
|
connect_args={"check_same_thread": False} # Needed for SQLite
|
|
)
|
|
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
# Dependency to get DB session
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close() |