import pymysql from app.core.config import settings def modify_schema(): print(f"Connecting to database...") # Parse DATABASE_URL for connection # Assuming jdbc like url: mysql+pymysql://user:pass@host:port/dbname # But since we use python-dotenv and models, let's just use raw pymysql based on known creds we used before # or better, parse it from settings if we can. # For simplicity in this script, I'll use the hardcoded creds from init_db.py style or parse similar to how we did before. # Reusing creds from init_db.py style for reliability as I know those work DB_HOST = "49.233.26.60" DB_PORT = 3306 DB_USER = "root" DB_PASSWORD = "dianyue" DB_NAME = "meloon_chat_bi" # The main db name connection = pymysql.connect( host=DB_HOST, port=DB_PORT, user=DB_USER, password=DB_PASSWORD, database=DB_NAME, cursorclass=pymysql.cursors.DictCursor ) with connection: with connection.cursor() as cursor: print("Checking indexes on 'users' table...") cursor.execute("SHOW INDEX FROM users WHERE Column_name = 'username'") indexes = cursor.fetchall() for index in indexes: index_name = index['Key_name'] if index['Non_unique'] == 0: # 0 means Unique print(f"Found unique index '{index_name}' on 'username'. Dropping it...") try: cursor.execute(f"DROP INDEX {index_name} ON users") print(f"Index '{index_name}' dropped successfully.") except Exception as e: print(f"Error dropping index: {e}") else: print(f"Index '{index_name}' is already non-unique or other type.") if not indexes: print("No index found on 'username' column.") connection.commit() print("Schema modification completed.") if __name__ == "__main__": modify_schema()