def database_middleware(app): dsn = get_dsn(app.config) app_config = app.config engine = create_engine(dsn) session = sessionmaker(autocommit=False, bind=engine) app_config.SQLALCHEMY_DATABASE_URI = dsn async def db_session_middleware(request: Request, call_next): response = Response("Internal server error", status_code=500) try: request.state.db = session() response = await call_next(request) finally: request.state.db.rollback() request.state.db.close() return response return db_session_middleware
# -*- coding: utf-8 -*- from sqlalchemy import create_engine from apps.people.generators.generate_people import PeopleFactory from core.db import common from core.utils.init_config import init_config from core.utils.init_db import get_dsn if __name__ == '__main__': config = init_config() dsn = get_dsn(config) engine = create_engine(dsn) common.Session.configure(bind=engine) session = common.Session() for i in range(config.RESOURCE_NUMBER_ELEMENTS): p = PeopleFactory() session.commit()
def setup_database(app_config): dsn = get_dsn(app_config) engine = create_engine(dsn) session = sessionmaker(autocommit=False, autoflush=False, bind=engine) return session, dsn
import importlib from logging.config import fileConfig from alembic import context from sqlalchemy import engine_from_config from sqlalchemy import pool # this is the Alembic Config object, which provides # access to the values within the .ini file in use. from core.utils.init_config import init_config from core.utils.init_db import get_dsn config = context.config config_app = init_config() dsn = get_dsn(config_app) # Interpret the config file for Python logging. # This line sets up loggers basically. fileConfig(config.config_file_name) # set endpoint from config config.set_main_option('sqlalchemy.url', dsn) # add your model's MetaData object here # for 'autogenerate' support # from myapp import mymodel # target_metadata = mymodel.Base.metadata target_metadata = [] for app in config_app.APPS: i = importlib.import_module('apps.{}.models'.format(app))
def pytest_sessionfinish(session): os.environ.setdefault('TEST_RUN', '1') from main import app _delete_database(get_dsn(app.config))
def pytest_sessionstart(session): os.environ.setdefault('TEST_RUN', '1') from main import app _create_database(get_dsn(app.config)) _create_database_connection(app)
import importlib from logging.config import fileConfig from alembic import context from sqlalchemy import engine_from_config from sqlalchemy import pool # this is the Alembic Config object, which provides # access to the values within the .ini file in use. from core.utils.init_config import init_config from core.utils.init_db import get_dsn config = context.config config_app = init_config() dsn = get_dsn(config_app).replace('%', '%%') # Interpret the config file for Python logging. # This line sets up loggers basically. fileConfig(config.config_file_name) print('qwer', dsn) # set endpoint from config config.set_main_option('sqlalchemy.url', dsn) # add your model's MetaData object here # for 'autogenerate' support # from myapp import mymodel # target_metadata = mymodel.Base.metadata target_metadata = [] for app in config_app.APPS: