#!/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)