diff --git a/app.py b/app.py index 181048e..4c8e498 100644 --- a/app.py +++ b/app.py @@ -552,7 +552,8 @@ def post_detail(post_id): # Get comments from cache comments = cached_comments.get(post_id, []) - + logger.info(f"Loading post {post_id}: found {len(comments)} comments") + # Sort comments by timestamp comments.sort(key=lambda x: x.get('timestamp', 0)) diff --git a/migrate_poll_source_fields.py b/migrate_poll_source_fields.py new file mode 100644 index 0000000..64bd4c6 --- /dev/null +++ b/migrate_poll_source_fields.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 +""" +Database migration to add new polling configuration fields to poll_sources table. +Run this once to add the new columns: max_posts, fetch_comments, priority +""" + +import sys +from app import app, db + +def migrate(): + """Add polling configuration columns to poll_sources table""" + with app.app_context(): + try: + # Check if columns already exist + from sqlalchemy import inspect + inspector = inspect(db.engine) + columns = [col['name'] for col in inspector.get_columns('poll_sources')] + + if 'max_posts' in columns and 'fetch_comments' in columns and 'priority' in columns: + print("✓ Polling configuration columns already exist") + return True + + # Add the new columns using raw SQL + with db.engine.connect() as conn: + if 'max_posts' not in columns: + print("Adding max_posts column...") + conn.execute(db.text( + "ALTER TABLE poll_sources ADD COLUMN max_posts INTEGER NOT NULL DEFAULT 100" + )) + conn.commit() + + if 'fetch_comments' not in columns: + print("Adding fetch_comments column...") + conn.execute(db.text( + "ALTER TABLE poll_sources ADD COLUMN fetch_comments BOOLEAN NOT NULL DEFAULT TRUE" + )) + conn.commit() + + if 'priority' not in columns: + print("Adding priority column...") + conn.execute(db.text( + "ALTER TABLE poll_sources ADD COLUMN priority VARCHAR(20) NOT NULL DEFAULT 'medium'" + )) + conn.commit() + + print("✓ Polling configuration columns added successfully") + print("\nUpdating existing poll sources with default values...") + + # Update existing rows to have default values + with db.engine.connect() as conn: + result = conn.execute(db.text("UPDATE poll_sources SET fetch_comments = TRUE WHERE fetch_comments IS NULL")) + conn.commit() + print(f"✓ Updated {result.rowcount} rows with default fetch_comments=TRUE") + + return True + + except Exception as e: + print(f"✗ Migration failed: {e}") + import traceback + traceback.print_exc() + return False + +if __name__ == '__main__': + print("Running poll source fields migration...") + success = migrate() + sys.exit(0 if success else 1)