def get_db() -> Generator: try: db = SessionLocal() yield db finally: # db.commit() db.close()
def get_db(): db = SessionLocal() try: logging.debug('Creating DB Session.') yield db finally: logging.debug('Closing DB Session.') db.close()
def setup_db() -> None: session = SessionLocal() if not database_exists(engine.url): create_database(engine.url) else: Base.metadata.drop_all(engine) init_db(session) session.close()
def end_auction(cls, id: int): try: db = SessionLocal() english = EnglishAuction() auction = auction_repo.get(db, id=id) english.end(db, db_obj=auction) finally: db.close()
def get_db() -> Generator: """ Get database """ try: db = SessionLocal() yield db finally: db.close()
def get_db() -> Generator: try: db = SessionLocal() yield db # TODO rollback after error? # except: # db.rollback() finally: db.close()
def get_db() -> Generator: """ 获取sqlalchemy会话对象 :return: """ try: db = SessionLocal() yield db finally: db.close()
def get_db() -> Generator: try: db = SessionLocal() yield db finally: try: db.commit() except exc.SQLAlchemyError: db.rollback() backend_logger.error("ERROR DB COMMIT", exc_info=True) db.close()
async def authenticate_user(self, environ: Dict) -> schemas.UserInDB: token = environ.get("HTTP_AUTHORIZATION") if token is None: raise ConnectionRefusedError("Not authenticated") db = SessionLocal() try: return await deps.get_current_user(db, self.server.cache, token) except HTTPException as e: raise ConnectionRefusedError(e.detail) finally: db.close()
def get_db() -> Generator: try: db = SessionLocal() yield db except Exception as e: print(str(e)) traceback.print_exc() db.rollback() finally: db.commit() db.close()
def get_db(): """ 获取sqlalchemy会话对象 :return: """ try: db = SessionLocal() print('获取数据库会话') yield db finally: db.close() print('数据库关闭')
def create_providers_task(providers: List[Dict[str, str]]): db = SessionLocal() try: providers_db = crud.provider.create_multi( db=db, objs_in=[ schemas.ProviderCreate(name=provider["name"], url=provider["url"]) for provider in providers ], ) return jsonable_encoder(providers_db) finally: db.close()
def get_warehouses() -> Dict[str, Any]: """NOTE: this assumes Zillion Web DB is same as Zillion DB""" global warehouses if warehouses: # TODO: cache control? return warehouses print("Building warehouses...") db = SessionLocal() try: result = db.query(Warehouses).all() for row in result: warehouses[row.id] = Warehouse.load(row.id) pp(warehouses) return warehouses finally: db.close()
def create_licenses_task(licenses: List[Dict[str, str]]) -> List[Dict[str, str]]: db = SessionLocal() objs_in = [ schemas.LicenseCreate(name=license["name"], url=license["url"]) for license in licenses ] try: licenses_db = crud.license.create_multi( db=db, objs_in=objs_in, ) return jsonable_encoder(licenses_db) except IntegrityError: db.rollback() licenses_db = [ crud.license.create(db=db, obj_in=obj_in, merge=True) for obj_in in objs_in ] return jsonable_encoder((licenses_db)) finally: db.close()
def migrate_json() -> None: # Read json # For each record in file, add to DB with open(TED_CAPTIONS_DATASET_PATH, "r") as json_file: json_list = list(json_file) captions = [json.loads(json_str) for json_str in json_list] print(f"{len(captions)} captions in JSONL") db = SessionLocal() print("Inserting data") for caption in tqdm(captions): create_video_caption( db, VideoCaption(title=caption["title"], caption=caption["captions"]), ) db.close()
def get_db(): """ We need to have an independent database session/connection (Session) per request, then use the same session throughout the request lifecycle and, finally, close it after the request is finished. Technical details: - Only code prior to and including the yield statement is executed before sending a response. - The yielded value is what is injected into path operations and other dependencies. - The code following the yield statement is executed after the response has been delivered. """ db = SessionLocal() try: yield db finally: db.close()
def check_all_machines() -> None: logger.debug("started check machine statuses") skip = 0 limit = 100 db = SessionLocal() try: while True: checked_machines: List[models.Machine] = asyncio.run( crud.machine.get_multi_with_check_online(db, skip=skip, limit=limit) ) if not checked_machines: break logger.debug(f"checked {len(checked_machines)} machines") for ma in checked_machines: asyncio.run(crud.machine_process.get_multi_with_poll(db, ma.id)) skip += limit logger.debug("checked all machines") finally: db.close()
def import_track_task(track: Dict[str, str]): db = SessionLocal() try: track_in = schemas.TrackCreate( title=track.get("title") or track.get("name"), artist=track.get("artist"), url=track.get("url"), provider_name=track.get("provider_name") or track.get("provider"), license_name=track.get("license_name") or track.get("license"), media_url=track.get("media_url") or track.get("mp3"), s3_preview_key=track.get("s3_preview_key") or track.get("internal_preview_uri"), ) if track_in.s3_preview_key is None: track_in.s3_preview_key = canonical_preview_uri(track_in) track_db = import_track(db=db, track=track_in) return jsonable_encoder(track_db) except Exception as e: _logger.error( f"Skipped track {track.get('title')} because of error", exc_info=e, ) finally: db.close()
def get_db() -> Generator[Session, None, None]: try: db = SessionLocal() yield db finally: db.close()
def get_db(): try: db = SessionLocal() return db finally: db.close()
def get_db(): db = SessionLocal() try: yield db finally: db.close()
def db() -> Generator: db = SessionLocal() yield db db.close()
def db() -> Generator: session = SessionLocal() yield session session.close()
def cancel_missed_reservations_task() -> None: db = SessionLocal() services.reservation.cancel_missed_reservations(db) db.close()
def get_db_session() -> Generator: try: db = SessionLocal() yield db finally: db.close()
async def on_startup() -> None: app.state.redis = await aioredis.create_redis_pool(settings.APP_REDIS_DSN) app.state.lock = aioredlock.Aioredlock([app.state.redis]) db = SessionLocal() await crud.user_cachedb.load(db, app.state.redis) db.close()
def get_db() -> Generator: db = SessionLocal() try: yield db finally: db.close()
async def get_session(): session = SessionLocal() try: yield session finally: session.close()