fix: 修复多worker并发初始化时默认管理员唯一键冲突
This commit is contained in:
+14
-18
@@ -62,6 +62,7 @@ def get_db():
|
|||||||
conn = sqlite3.connect(DB_PATH)
|
conn = sqlite3.connect(DB_PATH)
|
||||||
conn.row_factory = sqlite3.Row
|
conn.row_factory = sqlite3.Row
|
||||||
conn.execute("PRAGMA foreign_keys = ON")
|
conn.execute("PRAGMA foreign_keys = ON")
|
||||||
|
conn.execute("PRAGMA busy_timeout = 5000")
|
||||||
return conn
|
return conn
|
||||||
|
|
||||||
|
|
||||||
@@ -70,24 +71,19 @@ def init_db():
|
|||||||
conn = get_db()
|
conn = get_db()
|
||||||
try:
|
try:
|
||||||
conn.executescript(SCHEMA)
|
conn.executescript(SCHEMA)
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT OR IGNORE INTO users
|
||||||
|
(username, password_hash, display_name, is_admin, is_active, created_at)
|
||||||
|
VALUES (?, ?, ?, 1, 1, ?)
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
"admin",
|
||||||
|
generate_password_hash("admin123"),
|
||||||
|
"管理员",
|
||||||
|
now_iso(),
|
||||||
|
),
|
||||||
|
)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
admin = conn.execute(
|
|
||||||
"SELECT id FROM users WHERE username = ?", ("admin",)
|
|
||||||
).fetchone()
|
|
||||||
if admin is None:
|
|
||||||
conn.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO users (username, password_hash, display_name, is_admin, is_active, created_at)
|
|
||||||
VALUES (?, ?, ?, 1, 1, ?)
|
|
||||||
""",
|
|
||||||
(
|
|
||||||
"admin",
|
|
||||||
generate_password_hash("admin123"),
|
|
||||||
"管理员",
|
|
||||||
now_iso(),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
conn.commit()
|
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user