MultiCustomerMessenger supporting Telegram (python-telegram-bot), WhatsApp (Green API) and SMS (python-gsmmodem-new). REST API with Bearer-token auth, SQLAlchemy models for MariaDB, APScheduler for background polling, and Textual TUI running in same asyncio event-loop.
39 lines
855 B
Python
39 lines
855 B
Python
from sqlalchemy import create_engine
|
||
from sqlalchemy.orm import DeclarativeBase, sessionmaker
|
||
from sqlalchemy.pool import StaticPool
|
||
from config import settings
|
||
|
||
|
||
class Base(DeclarativeBase):
|
||
pass
|
||
|
||
|
||
_connect_args = {}
|
||
_pool_class = None
|
||
|
||
if settings.database_url.startswith("sqlite"):
|
||
_connect_args = {"check_same_thread": False}
|
||
_pool_class = StaticPool
|
||
|
||
engine = create_engine(
|
||
settings.database_url,
|
||
connect_args=_connect_args,
|
||
**({"poolclass": _pool_class} if _pool_class else {}),
|
||
echo=settings.debug,
|
||
)
|
||
|
||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||
|
||
|
||
def get_db():
|
||
db = SessionLocal()
|
||
try:
|
||
yield db
|
||
finally:
|
||
db.close()
|
||
|
||
|
||
def init_db() -> None:
|
||
from db import models # noqa: F401 – Modelle müssen importiert sein
|
||
Base.metadata.create_all(bind=engine)
|