From 05fc252db0a84177a130fe4fe92c0245c497ac63 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 28 Apr 2026 10:38:42 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B7=B2=E6=9C=89=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E8=BF=81=E7=A7=BB=20admin=20=E8=A7=92?= =?UTF-8?q?=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 当 role 列通过 ALTER TABLE 新增后,已有用户全部为 'user'。 新增迁移逻辑:若无 admin 用户,自动将首个注册用户升为 admin。 --- core/embedded_db.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/embedded_db.py b/core/embedded_db.py index 2919cee..8563370 100644 --- a/core/embedded_db.py +++ b/core/embedded_db.py @@ -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):