Files
wehub-resource-sync c889a57b6b
Test Suites / Build CI Environment (push) Has been cancelled
Test Suites / Basic Tests (push) Has been cancelled
Test Suites / End-to-End Tests (push) Has been cancelled
Test Suites / CLI Tests (push) Has been cancelled
Test Suites / Slow End-to-End Tests (push) Has been cancelled
Test Suites / Graph Database Tests (push) Has been cancelled
Test Suites / Vector DB Tests (push) Has been cancelled
Test Suites / Temporal Graph Test (push) Has been cancelled
Test Suites / Search Test on Different DBs (push) Has been cancelled
Test Suites / Example Tests (push) Has been cancelled
Test Suites / Notebook Tests (push) Has been cancelled
Test Suites / OS and Python Tests Ubuntu (push) Has been cancelled
Test Suites / OS and Python Tests Extended (push) Has been cancelled
Test Suites / LLM Test Suite (push) Has been cancelled
Test Suites / S3 File Storage Test (push) Has been cancelled
Test Suites / Run Integration Tests (push) Has been cancelled
Test Suites / MCP Tests (push) Has been cancelled
Test Suites / Docker Compose Test (push) Has been cancelled
Test Suites / Docker CI test (push) Has been cancelled
Test Suites / Relational DB Migration Tests (push) Has been cancelled
Test Suites / Distributed Cognee Test (push) Has been cancelled
Test Suites / DB Examples Tests (push) Has been cancelled
Test Suites / Test Completion Status (push) Has been cancelled
Test Suites / Claude Code Review (push) Has been cancelled
Test Suites / basic checks (push) Has been cancelled
build | Build and Push Cognee MCP Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
build | Build and Push Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.11) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.12) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (kuzu, kuzu) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (neo4j, neo4j) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Examples (push) Has been cancelled
Weighted Edges Tests / Code Quality for Weighted Edges (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:02:24 +08:00

81 lines
2.7 KiB
Python

from uuid import UUID
from typing import Optional
from sqlalchemy.orm import aliased
from sqlalchemy import and_, exists, or_, select
from sqlalchemy.ext.asyncio import AsyncSession
from cognee.infrastructure.databases.relational import with_async_session
from cognee.modules.graph.models import Node
@with_async_session
async def get_data_related_nodes(dataset_id: UUID, data_id: UUID, session: AsyncSession):
"""Return nodes owned by (dataset_id, data_id) that no other data in the
same dataset owns. Used in multi-user mode where each dataset has its
own graph/vector database, so cross-dataset slug collisions cannot
occur and only intra-dataset sharing is considered."""
NodeAlias = aliased(Node)
subq = select(NodeAlias.id).where(
and_(
NodeAlias.slug == Node.slug,
NodeAlias.dataset_id == dataset_id,
NodeAlias.data_id != data_id,
)
)
query_statement = select(Node).where(
and_(Node.data_id == data_id, Node.dataset_id == dataset_id, ~exists(subq))
)
data_related_nodes = await session.scalars(query_statement)
return data_related_nodes.all()
@with_async_session
async def get_global_data_related_nodes(
data_id: UUID,
session: AsyncSession,
dataset_id: Optional[UUID] = None,
):
"""Return nodes safe to hard-delete for a single-DB (non-multi-user)
deployment where the same data item may be linked to multiple datasets.
When `dataset_id` is provided, a slug is only considered removable if
NO other `(dataset_id, data_id)` pair owns it — protects shared data
items so deleting one dataset's link doesn't wipe the graph/vector
rows that another dataset still references.
When `dataset_id` is None, falls back to the legacy global semantics
(slug is exclusive to this `data_id` globally) for callers that don't
yet plumb the dataset through.
"""
NodeAlias = aliased(Node)
if dataset_id is None:
subq = select(NodeAlias.id).where(
and_(
NodeAlias.slug == Node.slug,
NodeAlias.data_id != data_id,
)
)
query_statement = select(Node).where(and_(Node.data_id == data_id, ~exists(subq)))
else:
subq = select(NodeAlias.id).where(
and_(
NodeAlias.slug == Node.slug,
or_(NodeAlias.dataset_id != dataset_id, NodeAlias.data_id != data_id),
)
)
query_statement = select(Node).where(
and_(
Node.dataset_id == dataset_id,
Node.data_id == data_id,
~exists(subq),
)
)
data_related_nodes = await session.scalars(query_statement)
return data_related_nodes.all()