def publish_relationships( db: PartitionedDatabase, tx: Transaction, relationship_name: RelationshipName, relationships: List[ModelRelationship], proxy_relationships: List[PackageProxyRelationship], config, s3, ) -> FileManifest: """ Export record relationships for all model relationships and package proxy relationships with the same name. TODO: it would be cleaner to export every model relationship to a distinct CSV. For example, when publishing a graph with (patient)-[attends]->(visit) and (patient)-[attends]->(event) relationships, we currently write these relationships to the same `attends.csv` file. If we wrote two separate `patient_attends_visit.csv` and `patient_attends_event.csv` files, we would not need to group relationships by name, nor add proxy relationships to these CSVs. The CSV for proxy relationships would just become `patient_belongs_to_file.csv` for every model with proxy packages. """ log.info( f"Writing record relationships for relationship '{relationship_name}'") assert all( r.name == relationship_name for r in relationships ), f"Relationships have different names: {[r.name for r in relationships]}" assert all( pp.relationship == relationship_name for pp in proxy_relationships ), f"Package proxy relationships have different names: {[pp.relationship for pp in proxy_relationships]}" output_file: OutputFile = OutputFile.csv_for_relationship( relationship_name).with_prefix( os.path.join(config.s3_publish_key, METADATA)) with s3_csv_writer(s3, config.s3_bucket, str(output_file), relationship_headers()) as writer: for relationship in relationships: for rr in db.get_record_relationships_by_model_tx( tx, relationship): writer.writerow([str(rr.from_), str(rr.to), str(rr.name)]) for pp in proxy_relationships: writer.writerow([str(pp.from_), str(pp.to), str(pp.relationship)]) return output_file.with_prefix(METADATA).as_manifest( size_of(s3, config.s3_bucket, output_file))
def get_concept_instance_relationships( db: PartitionedDatabase, relationship_id_or_name: ModelRelationshipId) -> List[JsonDict]: with db.transaction() as tx: try: stub = db.get_model_relationship_stub_tx( tx=tx, relation=relationship_id_or_name) except OperationError: stub = None if stub == None: return [ to_legacy_relationship_instance(r) for r in db.get_record_relationships_by_model_tx( tx, relationship_id_or_name) ] else: return [ to_legacy_relationship_instance(r) for r in db.get_record_relationships_by_model_with_relationship_stub_tx( tx, stub) ]