init
This commit is contained in:
33
db/__init__.py
Normal file
33
db/__init__.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from sqlmodel import select
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
from sqlalchemy.ext.asyncio import create_async_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from ..config import config
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger('sqlalchemy.engine')
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
async_engine = create_async_engine(config.DATABASE_URI)
|
||||
async_session = sessionmaker[AsyncSession](
|
||||
async_engine, class_=AsyncSession, expire_on_commit=False
|
||||
)
|
||||
|
||||
async def init_db(session: AsyncSession) -> None:
|
||||
|
||||
from app.models import User, Language,Role
|
||||
|
||||
user = (await session.exec(
|
||||
select(User).where(User.id == config.ADMIN_TELEGRAM_ID)
|
||||
)).first()
|
||||
if not user:
|
||||
await User.create(session, User(
|
||||
id = config.ADMIN_TELEGRAM_ID,
|
||||
lang = Language.DEFAULT,
|
||||
roles = [Role.SUPER_USER],
|
||||
name = "Admin"), commit = True)
|
||||
|
||||
async def get_db() -> AsyncSession: # type: ignore
|
||||
async with async_session() as session:
|
||||
yield session
|
||||
BIN
db/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
db/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
Reference in New Issue
Block a user