refactor(models): remove legacy db.session property wrappers on DocumentSegment and DatasetQuery (#41794)

This commit is contained in:
Keith 2026-09-07 05:24:37 +00:00 committed by GitHub
parent d91672c6d3
commit 62a286332a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 3 additions and 23 deletions

View File

@ -922,18 +922,10 @@ class DocumentSegment(TypeBase):
stopped_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, default=None)
hit_count: Mapped[int] = mapped_column(sa.Integer, nullable=False, default=0)
@property
def dataset(self) -> Dataset | None:
return self.get_dataset(session=db.session())
def get_dataset(self, *, session: Session) -> Dataset | None:
"""Load the owning dataset with the caller-owned database session."""
return session.get(Dataset, self.dataset_id)
@property
def document(self) -> Document | None:
return self.get_document(session=db.session())
def get_document(self, *, session: Session) -> Document | None:
"""Load the owning document with the caller-owned database session."""
return session.get(Document, self.document_id)
@ -952,10 +944,6 @@ class DocumentSegment(TypeBase):
)
)
@property
def child_chunks(self):
return self.get_child_chunks(session=db.session(), include_full_doc=False)
def get_child_chunks(self, *, session: Session, include_full_doc: bool = True) -> Sequence["ChildChunk"]:
"""Load hierarchical child chunks with the caller-owned database session."""
document = session.get(Document, self.document_id)
@ -1042,10 +1030,6 @@ class DocumentSegment(TypeBase):
return text
@property
def attachments(self) -> list[AttachmentItem]:
return self.get_attachments(session=db.session())
def get_attachments(self, *, session: Session) -> list[AttachmentItem]:
"""Load attachment metadata with the caller-owned database session."""
# Use JOIN to fetch attachments in a single query instead of two separate queries
@ -1195,10 +1179,6 @@ class DatasetQuery(TypeBase):
DateTime, nullable=False, server_default=sa.func.current_timestamp(), init=False
)
@property
def queries(self) -> list[dict[str, Any]]:
return self.get_queries(session=db.session())
def get_queries(self, *, session: Session) -> list[dict[str, Any]]:
try:
queries = json.loads(self.content)
@ -1467,7 +1447,7 @@ class ExternalKnowledgeApis(TypeBase):
return None
@property
def dataset_bindings(self) -> list[DatasetBindingItem]:
def dataset_bindings(self):
return self.get_dataset_bindings(session=db.session())
def get_dataset_bindings(self, *, session: Session) -> list[DatasetBindingItem]:

View File

@ -322,7 +322,7 @@ class TestDocumentSegmentNavigationProperties:
db_session_with_containers.flush()
# Act
related_dataset = segment.dataset
related_dataset = segment.get_dataset(session=db_session_with_containers)
# Assert
assert related_dataset is not None
@ -369,7 +369,7 @@ class TestDocumentSegmentNavigationProperties:
db_session_with_containers.flush()
# Act
related_document = segment.document
related_document = segment.get_document(session=db_session_with_containers)
# Assert
assert related_document is not None