forked from kodorvan/stcs
25 lines
761 B
Python
25 lines
761 B
Python
import logging.config
|
|
|
|
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine, AsyncSession
|
|
|
|
from database.models import Base
|
|
|
|
from config import DATABASE_URL
|
|
from logger_helper.logger_helper import LOGGING_CONFIG
|
|
|
|
logging.config.dictConfig(LOGGING_CONFIG)
|
|
logger = logging.getLogger("database")
|
|
|
|
async_engine = create_async_engine(DATABASE_URL, echo=False)
|
|
|
|
async_session = async_sessionmaker(async_engine, class_=AsyncSession, expire_on_commit=False)
|
|
|
|
|
|
async def init_db():
|
|
try:
|
|
async with async_engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
logger.info("Database initialized.")
|
|
except Exception as e:
|
|
logger.error("Database initialization failed: %s", e, exc_info=True)
|