File size: 2,103 Bytes
4a91ea9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 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()
|