#!/usr/bin/env python3 """ 删除现有的RBAC相关表 """ import pymysql from dotenv import load_dotenv import os load_dotenv() def get_db_connection(): """获取数据库连接""" try: config = { 'host': os.getenv('DB_HOST', 'localhost'), 'port': int(os.getenv('DB_PORT', 3306)), 'user': os.getenv('DB_USER', 'root'), 'password': os.getenv('DB_PASSWORD', 'admin'), 'database': os.getenv('DB_NAME', 'lq_db'), 'charset': 'utf8mb4', 'autocommit': True } return pymysql.connect(**config) except Exception as e: print(f"数据库连接失败: {e}") return None def drop_rbac_tables(): """删除RBAC相关表""" print("🗑️ 删除现有RBAC相关表...") print("=" * 50) conn = get_db_connection() if not conn: print("❌ 数据库连接失败") return False cursor = conn.cursor() try: # 按依赖关系顺序删除表 tables_to_drop = [ 'user_roles', 'role_permissions', 'role_menus', 'permissions', 'roles', 'menus' ] for table in tables_to_drop: try: cursor.execute(f"DROP TABLE IF EXISTS {table}") print(f" ✅ 删除表: {table}") except Exception as e: print(f" ⚠️ 删除表 {table} 失败: {e}") conn.commit() cursor.close() conn.close() print("\n✅ RBAC表删除完成") return True except Exception as e: print(f"❌ 删除表失败: {e}") cursor.close() conn.close() return False if __name__ == "__main__": drop_rbac_tables()