refactor(auth): 优化初始化数据库脚本

This commit is contained in:
jayhgq 2026-06-10 14:07:51 +08:00
parent fa88dd8ec2
commit cab1bf1f61

View File

@ -4,55 +4,6 @@ from models import Role, User, Permission, SystemSetting
from utils.password import get_password_hash from utils.password import get_password_hash
async def ensure_permission_table_structure(db: AsyncSession):
"""确保 permission 表结构符合最新要求"""
print("检查并更新 permission 表结构...")
# 检查表是否存在
result = await db.execute(
text("SELECT table_name FROM information_schema.tables WHERE table_name = 'permission'")
)
table_exists = result.scalar_one_or_none()
if not table_exists:
print("permission 表不存在,将由 SQLAlchemy 创建")
return
# 检查是否缺少 parentid 字段
result = await db.execute(
text("SELECT column_name FROM information_schema.columns WHERE table_name = 'permission'")
)
existing_columns = {row[0] for row in result.fetchall()}
# 添加 parentid 字段
if 'parentid' not in existing_columns:
try:
await db.execute(text("ALTER TABLE permission ADD COLUMN IF NOT EXISTS parentid INTEGER DEFAULT 0"))
print("添加 parentid 字段成功")
except Exception as e:
print(f"添加 parentid 字段失败: {e}")
# 添加 route 字段
if 'route' not in existing_columns:
try:
await db.execute(text("ALTER TABLE permission ADD COLUMN IF NOT EXISTS route VARCHAR(255)"))
print("添加 route 字段成功")
except Exception as e:
print(f"添加 route 字段失败: {e}")
# 删除旧字段(如果存在)
for old_column in ['module', 'submodule', 'action']:
if old_column in existing_columns:
try:
await db.execute(text(f"ALTER TABLE permission DROP COLUMN IF EXISTS {old_column}"))
print(f"删除旧字段 {old_column} 成功")
except Exception as e:
print(f"删除旧字段 {old_column} 失败: {e}")
await db.commit()
print("permission 表结构更新完成")
async def init_permissions(db: AsyncSession): async def init_permissions(db: AsyncSession):
"""初始化权限数据(树形结构)""" """初始化权限数据(树形结构)"""
# 定义需要初始化的权限(树形结构) # 定义需要初始化的权限(树形结构)
@ -70,13 +21,7 @@ async def init_permissions(db: AsyncSession):
{"name": "用户管理", "code": "perm:user", "parentid": 1, "route": "/admin/users", "description": "用户管理子模块"}, {"name": "用户管理", "code": "perm:user", "parentid": 1, "route": "/admin/users", "description": "用户管理子模块"},
{"name": "角色管理", "code": "perm:role", "parentid": 1, "route": "/admin/roles", "description": "角色管理子模块"}, {"name": "角色管理", "code": "perm:role", "parentid": 1, "route": "/admin/roles", "description": "角色管理子模块"},
{"name": "菜单管理", "code": "perm:menu", "parentid": 1, "route": "/admin/menu", "description": "菜单管理子模块"}, {"name": "菜单管理", "code": "perm:menu", "parentid": 1, "route": "/admin/menu", "description": "菜单管理子模块"},
# ========================================
# 系统设置 -> 二级子模块
# ========================================
{"name": "基础配置", "code": "system:basic", "parentid": 2, "route": "/admin/settings", "description": "系统基础配置"},
{"name": "高级配置", "code": "system:advanced", "parentid": 2, "route": "/admin/settings", "description": "系统高级配置"},
# ======================================== # ========================================
# 用户管理操作parentid=3 # 用户管理操作parentid=3
# ======================================== # ========================================
@ -451,7 +396,6 @@ async def init_system_settings(db: AsyncSession):
async def init_all_data(db: AsyncSession): async def init_all_data(db: AsyncSession):
"""初始化所有数据""" """初始化所有数据"""
print("开始初始化数据库数据...") print("开始初始化数据库数据...")
await ensure_permission_table_structure(db)
await init_roles(db) await init_roles(db)
await init_permissions(db) await init_permissions(db)
await init_role_permissions(db) await init_role_permissions(db)