Kamixitong/add_ip_address_column.py
2025-11-19 22:49:24 +08:00

47 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pymysql
# 数据库连接信息
host = 'localhost'
user = 'root'
password = 'taiyi1224'
database = 'kamaxitong'
try:
# 连接数据库
connection = pymysql.connect(
host=host,
user=user,
password=password,
database=database,
charset='utf8mb4'
)
with connection.cursor() as cursor:
# 添加ip_address字段到device表
cursor.execute("ALTER TABLE device ADD COLUMN ip_address VARCHAR(45) NULL")
print("Added ip_address column to device table")
# 更新alembic_version到最新版本使用较短的版本号
cursor.execute("UPDATE alembic_version SET version_num = %s", ("20241119",))
print("Updated alembic_version to 20241119")
connection.commit()
# 验证字段已添加
cursor.execute("DESCRIBE device")
device_columns = cursor.fetchall()
has_ip_address = any(col[0] == 'ip_address' for col in device_columns)
print(f"Device table has ip_address column: {has_ip_address}")
# 验证alembic版本
cursor.execute("SELECT * FROM alembic_version")
version_result = cursor.fetchone()
print(f"Alembic version: {version_result}")
except Exception as e:
print(f"Error: {e}")
if 'connection' in locals():
connection.rollback()
finally:
if 'connection' in locals():
connection.close()