예제 #1
0
def _create_record(
    db: Session,
    record: ds.Record,
) -> ds.Record:
    nested = db.begin_nested()
    try:
        db.add(record)
        db.commit()
    except IntegrityError:
        # Assuming this is a UNIQUE constraint failure due to an existing
        # record_info with the same name and ensemble. Try to fetch the
        # record_info
        nested.rollback()
        record_info = record.record_info
        old_record_info = (
            db.query(ds.RecordInfo)
            .filter_by(ensemble=record_info.ensemble, name=record_info.name)
            .one()
        )

        # Check that the parameters match
        if record_info.record_class != old_record_info.record_class:
            raise exc.ConflictError(
                "Record class of new record does not match previous record class",
                new_record_class=record_info.record_class,
                old_record_class=old_record_info.record_class,
            )
        if record_info.record_type != old_record_info.record_type:
            raise exc.ConflictError(
                "Record type of new record does not match previous record type",
                new_record_type=record_info.record_type,
                old_record_type=old_record_info.record_type,
            )

        record = ds.Record(
            record_info=old_record_info,
            f64_matrix=record.f64_matrix,
            file=record.file,
            realization_index=record.realization_index,
        )
        db.add(record)
        db.commit()

    return record
예제 #2
0
def _create_record(
    db: Session,
    ensemble: ds.Ensemble,
    name: str,
    record_type: ds.RecordType,
    record_class: ds.RecordClass = ds.RecordClass.other,
    prior: Optional[ds.Prior] = None,
    **kwargs: Any,
) -> ds.Record:
    record_info = ds.RecordInfo(
        ensemble=ensemble,
        name=name,
        record_class=record_class,
        record_type=record_type,
        prior=prior,
    )
    record = ds.Record(record_info=record_info, **kwargs)

    nested = db.begin_nested()
    try:
        db.add(record)
        db.commit()
    except IntegrityError:
        # Assuming this is a UNIQUE constraint failure due to an existing
        # record_info with the same name and ensemble. Try to fetch the
        # record_info
        nested.rollback()
        old_record_info = (db.query(ds.RecordInfo).filter_by(ensemble=ensemble,
                                                             name=name).one())

        # Check that the parameters match
        if record_info.record_class != old_record_info.record_class:
            raise HTTPException(
                status_code=status.HTTP_409_CONFLICT,
                detail={
                    "error":
                    "Record class of new record does not match previous record class",
                    "new_record_class": str(record_info.record_class),
                    "old_record_class": str(old_record_info.record_class),
                    "name": name,
                    "ensemble_id": str(ensemble.id),
                },
            )
        if record_info.record_type != old_record_info.record_type:
            raise HTTPException(
                status_code=status.HTTP_409_CONFLICT,
                detail={
                    "error":
                    "Record type of new record does not match previous record type",
                    "new_record_type": str(record_info.record_type),
                    "old_record_type": str(old_record_info.record_type),
                    "name": name,
                    "ensemble_id": str(ensemble.id),
                },
            )

        record = ds.Record(record_info=old_record_info, **kwargs)
        db.add(record)
        db.commit()

    return record