#!/usr/bin/env python3 """ Database migration script to add voting_deadline column to motmadminsettings table. This script adds the voting_deadline column to support countdown timers for MOTM voting. It's safe to run multiple times - it will skip if the column already exists. """ from db_config import db_config from sqlalchemy import text import sys def add_voting_deadline_column(): """Add voting_deadline column to motmadminsettings or admin_settings table.""" try: # Get database connection engine = db_config.engine # Try both possible table names table_names = ['motmadminsettings', 'admin_settings'] for table_name in table_names: try: with engine.connect() as connection: # Check if table exists and column already exists try: result = connection.execute(text(f"SELECT votingdeadline FROM {table_name} LIMIT 1")) print(f"✓ Column 'votingdeadline' already exists in {table_name} table") return True except Exception as e: # Column or table doesn't exist pass # Try to add the column try: # PostgreSQL syntax connection.execute(text(f"ALTER TABLE {table_name} ADD COLUMN votingdeadline TIMESTAMP")) connection.commit() print(f"✓ Successfully added 'votingdeadline' column to {table_name} table (PostgreSQL)") return True except Exception as e: # Try SQLite syntax try: connection.rollback() connection.execute(text(f"ALTER TABLE {table_name} ADD COLUMN votingdeadline DATETIME")) connection.commit() print(f"✓ Successfully added 'votingdeadline' column to {table_name} table (SQLite)") return True except Exception as e2: # This table doesn't exist, try next one continue except Exception as e: continue print(f"✗ Error: Could not find admin settings table (tried: {', '.join(table_names)})") print(" The database may not be initialized yet. Please run database setup first.") return False except Exception as e: print(f"✗ Error connecting to database: {str(e)}") return False if __name__ == "__main__": print("=" * 60) print("MOTM Voting Deadline - Database Migration") print("=" * 60) print("\nThis script will add the 'votingdeadline' column to the") print("motmadminsettings table to support countdown timers.\n") result = add_voting_deadline_column() if result: print("\n✓ Migration completed successfully!") print("\nYou can now set voting deadlines in the admin interface.") sys.exit(0) else: print("\n✗ Migration failed!") print("Please check the error messages above.") sys.exit(1)