From 0a0b030df5f82788cc6946d4d6f078a04aaefc1f Mon Sep 17 00:00:00 2001 From: Igor Ilic Date: Wed, 4 Dec 2024 14:03:01 +0100 Subject: [PATCH] fix: Resolve issue when metadata is updated Resolve issue when attempting to update metadata related to data Fix --- .../modules/data/operations/write_metadata.py | 26 ++++++++++++++----- .../ingestion/ingest_data_with_metadata.py | 1 - 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/cognee/modules/data/operations/write_metadata.py b/cognee/modules/data/operations/write_metadata.py index a2ea644a..43031cdc 100644 --- a/cognee/modules/data/operations/write_metadata.py +++ b/cognee/modules/data/operations/write_metadata.py @@ -4,6 +4,7 @@ import warnings from typing import Any from uuid import UUID +from sqlalchemy import select from typing import Any, BinaryIO, Union from cognee.infrastructure.databases.relational import get_relational_engine @@ -15,13 +16,24 @@ async def write_metadata(data_item: Union[BinaryIO, str, Any], data_id: UUID, fi metadata_dict = get_metadata_dict(data_item, file_metadata) db_engine = get_relational_engine() async with db_engine.get_async_session() as session: - metadata = Metadata( - id=data_id, - metadata_repr=json.dumps(metadata_dict), - metadata_source=parse_type(type(data_item)), - data_id=data_id - ) - session.add(metadata) + + metadata = ( + await session.execute(select(Metadata).filter(Metadata.data_id == data_id)) + ).scalar_one_or_none() + + if metadata is not None: + metadata.metadata_repr = json.dumps(metadata_dict) + metadata.metadata_source = parse_type(type(data_item)) + await session.merge(metadata) + else: + metadata = Metadata( + id=data_id, + metadata_repr=json.dumps(metadata_dict), + metadata_source=parse_type(type(data_item)), + data_id=data_id + ) + session.add(metadata) + await session.commit() diff --git a/cognee/tasks/ingestion/ingest_data_with_metadata.py b/cognee/tasks/ingestion/ingest_data_with_metadata.py index b820e623..0a238b54 100644 --- a/cognee/tasks/ingestion/ingest_data_with_metadata.py +++ b/cognee/tasks/ingestion/ingest_data_with_metadata.py @@ -75,7 +75,6 @@ async def data_storing(data: Any, dataset_name: str, user: User): ).scalar_one_or_none() if data_point is not None: - await delete_metadata(data_point.metadata_id) data_point.name = file_metadata["name"] data_point.raw_data_location = file_metadata["file_path"] data_point.extension = file_metadata["extension"]