Add admin user seeding functionality and corresponding tests

This commit is contained in:
2026-04-29 13:17:12 +02:00
parent e1d74fe163
commit da3ae822f6
2 changed files with 59 additions and 0 deletions
+23
View File
@@ -75,3 +75,26 @@ def _run_migrations(conn: duckdb.DuckDBPyConnection) -> None:
created_at TIMESTAMP DEFAULT now()
)
""")
_seed_admin(conn)
def _seed_admin(conn: duckdb.DuckDBPyConnection) -> None:
"""Insert the default admin user if it doesn't already exist."""
from passlib.context import CryptContext
_pwd = CryptContext(schemes=["bcrypt"], deprecated="auto")
email = os.getenv("ADMIN_EMAIL", "ai@allucanget.biz")
password = os.getenv("ADMIN_PASSWORD", "admin123")
existing = conn.execute(
"SELECT id FROM users WHERE email = ?", [email]
).fetchone()
if existing is None:
password_hash = _pwd.hash(password)
conn.execute(
"""
INSERT INTO users (email, password_hash, role)
VALUES (?, ?, 'admin')
""",
[email, password_hash],
)