Files
vidconf/backend/scripts/seed.py

52 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Идемпотентный seed скрипт: единственный админ-пользователь.
Комнаты (100 демо-комнат) больше не существуют как сущность (ADR-001) —
seed сокращён до создания администратора.
Запустить с: `uv run python -m scripts.seed`
"""
import asyncio
from argon2 import PasswordHasher
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from core.config import get_settings
from core.db import async_session_maker, engine
from models.user import User
_hasher = PasswordHasher()
async def _seed_admin(session: AsyncSession) -> None:
settings = get_settings()
email = settings.seed_admin_email
password = settings.seed_admin_password
existing = await session.scalar(select(User).where(User.email == email))
if existing is not None:
return
session.add(
User(
email=email,
name_user="Admin",
password_hash=_hasher.hash(password),
role="admin",
email_verified=True,
)
)
async def seed() -> None:
"""Upsert админ-пользователя; безопасно запускать несколько раз."""
async with async_session_maker() as session:
await _seed_admin(session)
await session.commit()
await engine.dispose()
if __name__ == "__main__":
asyncio.run(seed())