Example #1
0
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
Example #2
0
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 set_file_releaseid(filepath: str, release_id: int) -> True:
    session = Session()

    try:
        session.begin()

        session.query(ProcessedFile).filter(
            ProcessedFile.file_path == filepath).update(
                {"release_id": release_id})

        session.commit()
        session.flush()
    except IntegrityError as err:
        session.rollback()
        logging.info("Record already exists.")
        raise err
    except Exception as err:
        session.rollback()
        raise err
    finally:
        session.close()

    return True