
- Create app/db/base.py with SQLAlchemy Base definition - Create app/db/session.py with database engine and session setup using absolute path /app/storage/db/db.sqlite - Create app/models/todo.py with Todo model including id, title, description, completed, created_at, updated_at fields - Create app/schemas/todo.py with Pydantic schemas for TodoCreate, TodoUpdate, and TodoResponse - Add necessary __init__.py files for proper package structure
29 lines
699 B
Python
29 lines
699 B
Python
from pathlib import Path
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from .base import Base
|
|
|
|
# Database setup with absolute path as specified in guidelines
|
|
DB_DIR = Path("/app") / "storage" / "db"
|
|
DB_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_DIR}/db.sqlite"
|
|
|
|
engine = create_engine(
|
|
SQLALCHEMY_DATABASE_URL,
|
|
connect_args={"check_same_thread": False}
|
|
)
|
|
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
# Create tables
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
# Dependency to get database session
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close() |