async def pg_engine(app: web.Application): engine = None pg_cfg: PostgresSettings = app[APP_CONFIG_KEY].STORAGE_POSTGRES dsn = DataSourceName( application_name=f"{__name__}_{id(app)}", database=pg_cfg.POSTGRES_DB, user=pg_cfg.POSTGRES_USER, password=pg_cfg.POSTGRES_PASSWORD.get_secret_value(), host=pg_cfg.POSTGRES_HOST, port=pg_cfg.POSTGRES_PORT, ) # type: ignore log.info("Creating pg engine for %s", dsn) async for attempt in AsyncRetrying( **PostgresRetryPolicyUponInitialization(log).kwargs ): with attempt: engine = await create_pg_engine( dsn, minsize=pg_cfg.POSTGRES_MINSIZE, maxsize=pg_cfg.POSTGRES_MAXSIZE ) await raise_if_not_responsive(engine) if app[APP_CONFIG_KEY].STORAGE_TESTING: log.info("Initializing tables for %s", dsn) init_pg_tables(dsn, schema=metadata) assert engine # nosec app[APP_DB_ENGINE_KEY] = engine yield # ---------- if engine is not app.get(APP_DB_ENGINE_KEY): log.critical("app does not hold right db engine. Somebody has changed it??") if engine: engine.close() await engine.wait_closed() log.debug( "engine '%s' after shutdown: closed=%s, size=%d", engine.dsn, engine.closed, engine.size, )
async def postgres_cleanup_ctx(app: web.Application): engine = None pg_cfg: PostgresSettings = app[APP_CONFIG_KEY].STORAGE_POSTGRES dsn = DataSourceName( application_name=f"{__name__}_{id(app)}", database=pg_cfg.POSTGRES_DB, user=pg_cfg.POSTGRES_USER, password=pg_cfg.POSTGRES_PASSWORD.get_secret_value(), host=pg_cfg.POSTGRES_HOST, port=pg_cfg.POSTGRES_PORT, ) # type: ignore log.info("Creating pg engine for %s", dsn) engine = await _ensure_pg_ready(dsn, min_size=pg_cfg.POSTGRES_MINSIZE, max_size=pg_cfg.POSTGRES_MAXSIZE) if app[APP_CONFIG_KEY].STORAGE_TESTING: log.info("Initializing tables for %s", dsn) init_pg_tables(dsn, schema=metadata) assert engine # nosec app[APP_DB_ENGINE_KEY] = engine yield # ---------- if engine is not app.get(APP_DB_ENGINE_KEY): log.critical( "app does not hold right db engine. Somebody has changed it??") if engine: await close_engine(engine) log.debug( "engine '%s' after shutdown: closed=%s, size=%d", engine.dsn, engine.closed, engine.size, )
async def test_engine_when_idle_for_some_time(): # NOTE: this test needs a docker swarm and a running postgres service dsn = DataSourceName( user="******", password="******", host="127.0.0.1", port=5432, database="db", application_name="test-app", ) engine = await create_pg_engine(dsn, minsize=1, maxsize=1) init_pg_tables(dsn, metadata) # import pdb; pdb.set_trace() assert not engine.closed # does not mean anything!!! # pylint: disable=no-value-for-parameter async with engine.acquire() as conn: # writes await conn.execute(tbl.insert().values(val="first")) # by default docker swarm kills connections that are idle for more than 15 minutes await asyncio.sleep(901) async with engine.acquire() as conn: await conn.execute(tbl.insert().values(val="third"))
def test_init_tables(postgres_service_with_fake_data): dsn = postgres_service_with_fake_data init_pg_tables(dsn, metadata)