fix: 已有用户自动迁移 admin 角色

当 role 列通过 ALTER TABLE 新增后,已有用户全部为 'user'。
新增迁移逻辑:若无 admin 用户,自动将首个注册用户升为 admin。
This commit is contained in:
root
2026-04-28 10:38:42 +08:00
parent b4456a9c5b
commit 05fc252db0

View File

@ -111,6 +111,15 @@ class EmbeddedGraphDB:
if 'role' not in columns:
cursor.execute("ALTER TABLE web_users ADD COLUMN role TEXT NOT NULL DEFAULT 'user'")
# 确保至少有一个 admin当 role 列刚添加时,已有用户都是 user
cursor.execute("SELECT COUNT(*) as cnt FROM web_users WHERE role = 'admin'")
has_admin = cursor.fetchone()[0] > 0
if not has_admin:
cursor.execute("SELECT id, username FROM web_users ORDER BY created_at ASC LIMIT 1")
first_user = cursor.fetchone()
if first_user:
cursor.execute("UPDATE web_users SET role = 'admin' WHERE id = ?", (first_user[0],))
self.conn.commit()
def ensure_constraints(self):