def register_release(category: str, release_id: int): insert_stmt = ( insert(ReleaseCategory.__table__) .values( release_id=release_id, process_name=category ) .on_conflict_do_nothing( index_elements=[ ReleaseCategory.release_id, ReleaseCategory.process_name, ] ) ) session = Session() try: session.begin() session.add(insert_stmt) session.commit() except Exception as err: session.rollback() raise err finally: session.close() return None
def get_release_id(datestamp: datetime, process_name: str) -> Tuple[int, datetime]: query = ( select([ ReleaseReference.id, ReleaseReference.timestamp ]) .select_from( join( ReleaseReference, ReleaseCategory, ReleaseReference.id == ReleaseCategory.release_id ) ) .where( and_( func.DATE(ReleaseReference.timestamp) == func.DATE(datestamp.isoformat()), ReleaseCategory.process_name == process_name ) ) ) session = Session() try: response = session.execute(query) result = response.fetchone() if result is not None: return result except Exception as err: session.rollback() raise err finally: session.close() try: release = ReleaseReference(timestamp=datestamp) session.begin() session.add(release) session.commit() category = ReleaseCategory(release_id=release.id, process_name=process_name) session.begin() session.add(category) session.commit() except Exception as err: session.rollback() raise err finally: session.close() return get_release_id(datestamp, process_name)
def register_file(filepath: str, timestamp: datetime, instance_id: str, release_id=None) -> True: parsed_filepath = parse_filepath(filepath) processed_file = ProcessedFile(file_path=filepath, type=category_label(parsed_filepath), timestamp=timestamp, release_id=release_id, process_id=instance_id) session = Session() try: session.add(processed_file) session.flush() except IntegrityError as err: session.rollback() query = session.execute( select([ ProcessedFile.id, ]).where( and_(ProcessedFile.file_path == filepath, ProcessedFile.process_id == instance_id))) result = query.fetchone() if result is not None: return True logging.info("Record already exists.") raise err except Exception as err: session.rollback() raise err finally: session.close() return True
def get_release_id(datestamp: datetime, process_name: str) -> Tuple[int, datetime]: """ Generates or retrieves the `release_id` for the process. Parameters ---------- datestamp : datetime Datestamp for the data. process_name : str Name of the process - must match the ENUM defined in the database. Returns ------- Tuple[int, datetime] Tuple of `release_id` and the timestamp associated with the release. """ query = ( select([ ReleaseReference.id, ReleaseReference.timestamp ]) .select_from( join( ReleaseReference, ReleaseCategory, ReleaseReference.id == ReleaseCategory.release_id ) ) .where( and_( func.DATE(ReleaseReference.timestamp) == datestamp.date(), ReleaseCategory.process_name == process_name ) ) ) session = Session() try: response = session.execute(query) result = response.fetchone() if result is not None: return result except Exception as err: session.rollback() raise err finally: session.close() session = Session(autocommit=True) try: release = ReleaseReference(timestamp=datestamp) session.add(release) session.flush() category = ReleaseCategory( release_id=release.id, process_name=process_name ) session.add(category) session.flush() except Exception as err: session.rollback() raise err finally: session.close() return get_release_id(datestamp, process_name)