Skip to content

Commit

Permalink
fix: Resolve issue when metadata is updated
Browse files Browse the repository at this point in the history
Resolve issue when attempting to update metadata related to data

Fix
  • Loading branch information
dexters1 committed Dec 4, 2024
1 parent 3699b0d commit 0a0b030
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
26 changes: 19 additions & 7 deletions cognee/modules/data/operations/write_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()


Expand Down
1 change: 0 additions & 1 deletion cognee/tasks/ingestion/ingest_data_with_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit 0a0b030

Please sign in to comment.