31 lines
728 B
Python
31 lines
728 B
Python
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker, DeclarativeBase
|
|
from app.config import get_settings
|
|
|
|
settings = get_settings()
|
|
|
|
engine = create_engine(
|
|
settings.database_url,
|
|
connect_args={"check_same_thread": False},
|
|
)
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
def init_db():
|
|
"""Create all tables."""
|
|
# import models so SQLAlchemy registers them
|
|
from app.models import admin_user, upstream, snapshot, webhook_config, notification_log, custom_page # noqa: F401
|
|
Base.metadata.create_all(bind=engine)
|