def session_scope(self): session = Session() try: yield session session.commit() except: session.rollback() raise finally: session.close()
arg_parser.add_argument("--truncate", action="store_true", help="truncate the database before fetching new data") args = arg_parser.parse_args() if args.truncate: session.execute('''TRUNCATE TABLE application CASCADE''') logger.info("Truncated the database") session.commit() start = time.time() data_fetcher = DataFetcher(session, test_mode=args.test_mode) try: app_ids = data_fetcher.fetch_all_data() session.commit() end = time.time() logger.info(f""" ==================================================================================================================== Finished: Saved {len(app_ids)} applications metadata into database. List of the application_id's: {app_ids} Elapsed time: {(end-start):.3f} seconds ==================================================================================================================== """) except Exception as ex: logger.exception(f"Caught an exception: {ex}") session.rollback() finally: session.close()