37 lines
1.4 KiB
Python
37 lines
1.4 KiB
Python
"""Идемпотентная подготовка БД стенда: расширение pg_stat_statements и админ admin/admin."""
|
|
import asyncio
|
|
import os
|
|
|
|
import asyncpg
|
|
from argon2 import PasswordHasher
|
|
|
|
# .local отклоняется email-validator в ответе /users/me (500), поэтому обычный домен
|
|
EMAIL = "admin@perf-stand.com"
|
|
DSN = os.environ.get("PERF_DB_DSN", "postgresql://perf:perf@localhost:55432/perf")
|
|
|
|
|
|
async def main() -> None:
|
|
conn = await asyncpg.connect(DSN)
|
|
try:
|
|
await conn.execute("CREATE EXTENSION IF NOT EXISTS pg_stat_statements")
|
|
# Хеш в формате src/core/security.py (argon2-cffi PasswordHasher() по умолчанию)
|
|
hashed = PasswordHasher().hash("admin")
|
|
exists = await conn.fetchval("SELECT id FROM v3.app_user WHERE username = 'admin'")
|
|
if exists:
|
|
await conn.execute(
|
|
"UPDATE v3.app_user SET hashed_password=$1, email=$2, is_active=true, role_id=1 WHERE id=$3",
|
|
hashed, EMAIL, exists,
|
|
)
|
|
else:
|
|
await conn.execute(
|
|
"INSERT INTO v3.app_user (email, username, hashed_password, full_name, role_id, is_active) "
|
|
"VALUES ($2, 'admin', $1, 'Perf Admin', 1, true)",
|
|
hashed, EMAIL,
|
|
)
|
|
print("bootstrap OK")
|
|
finally:
|
|
await conn.close()
|
|
|
|
|
|
asyncio.run(main())
|