Add SQLAlchemy ORM with Alembic migrations

- Added SQLAlchemy 2.0 and Alembic 1.13 dependencies
- Created models.py with Channel and VideoEntry ORM models
- Created database.py for database configuration and session management
- Initialized Alembic migration system with initial migration
- Updated feed_parser.py with save_to_db() method for persistence
- Updated main.py with database initialization and new API routes:
  - /api/feed now saves to database by default
  - /api/channels lists all tracked channels
  - /api/history/<channel_id> returns video history
- Updated .gitignore to exclude database files
- Updated CLAUDE.md with comprehensive ORM and migration documentation

Database uses SQLite (yottob.db) with upsert logic to avoid duplicates.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-26 13:58:10 -05:00
parent f460fb4daf
commit 4892bec986
13 changed files with 766 additions and 13 deletions

71
database.py Normal file
View File

@@ -0,0 +1,71 @@
"""Database configuration and session management."""
from contextlib import contextmanager
from typing import Generator
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session
from models import Base
# Database configuration
DATABASE_URL = "sqlite:///yottob.db"
# Create engine
engine = create_engine(
DATABASE_URL,
echo=False, # Set to True for SQL query logging
connect_args={"check_same_thread": False} # Needed for SQLite
)
# Session factory
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def init_db() -> None:
"""Initialize the database by creating all tables.
This function should be called when the application starts.
It creates all tables defined in the models if they don't exist.
"""
Base.metadata.create_all(bind=engine)
@contextmanager
def get_db_session() -> Generator[Session, None, None]:
"""Context manager for database sessions.
Usage:
with get_db_session() as session:
# Use session here
session.query(...)
The session is automatically committed on success and rolled back on error.
"""
session = SessionLocal()
try:
yield session
session.commit()
except Exception:
session.rollback()
raise
finally:
session.close()
def get_db() -> Generator[Session, None, None]:
"""Dependency function for Flask routes to get a database session.
Usage in Flask:
session = next(get_db())
try:
# Use session
finally:
session.close()
"""
db = SessionLocal()
try:
yield db
finally:
db.close()