import os from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker, scoped_session from flask import g, current_app from models import Base, Account, ActiveSession, ChannelId, QuizMaster def get_engine(): return create_engine(f"sqlite:///{current_app.config['DATABASE']}") def get_db(): if 'db' not in g: engine = get_engine() g.db = scoped_session(sessionmaker(bind=engine)) return g.db def close_connection(exception): db = g.pop('db', None) if db is not None: db.remove() def initialize_db(logger): engine = get_engine() Base.metadata.create_all(engine) logger.info("データベースの初期化が完了しました") def reset_active_sessions(logger): db = get_db() db.query(ActiveSession).delete() db.commit() logger.info("アクティブセッションをリセットしました")