From 2780dab8e2e6e54d72d6b0311ba65998f9dab63f Mon Sep 17 00:00:00 2001 From: lezi-fun <177434121+lezi-fun@users.noreply.github.com> Date: Tue, 11 Aug 2026 20:05:27 +0800 Subject: [PATCH] feat: auto-promote earliest-registered user to super admin (idempotent) users PK is UUID (no uid=1), so bootstrap super admin via semantics: if no admin exists, promote the user with earliest created_at. Idempotent (no-op when an admin already exists), safe on any existing database. Matches the admin-list ordering (earliest user at the bottom). --- schema.sql | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/schema.sql b/schema.sql index 8ad0065..a2b4e11 100644 --- a/schema.sql +++ b/schema.sql @@ -227,3 +227,14 @@ CREATE TABLE IF NOT EXISTS recovery_codes ( ); CREATE INDEX IF NOT EXISTS idx_recovery_codes_user ON recovery_codes(user_id); + + +-- ============================================================ +-- 超级管理员自动设立(数据库兼容,幂等) +-- 若没有任何 admin,将最早注册的用户提升为超级管理员。 +-- 不依赖自增 uid(users 主键是 UUID),任何现有数据库都可安全执行。 +-- ============================================================ +UPDATE users SET role = 'admin' +WHERE role <> 'admin' + AND NOT EXISTS (SELECT 1 FROM users WHERE role = 'admin') + AND id = (SELECT id FROM users ORDER BY created_at ASC, id ASC LIMIT 1);