Skip to content

Commit 6167662

Browse files
committed
nit
1 parent edaca1f commit 6167662

File tree

3 files changed

+10
-46
lines changed

3 files changed

+10
-46
lines changed

backend/onyx/background/celery/tasks/connector_deletion/tasks.py

+5-23
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,8 @@
2525
from onyx.configs.constants import OnyxRedisSignals
2626
from onyx.db.connector import fetch_connector_by_id
2727
from onyx.db.connector_credential_pair import add_deletion_failure_message
28-
from onyx.db.connector_credential_pair import (
29-
delete_connector_credential_pair__no_commit,
30-
)
3128
from onyx.db.connector_credential_pair import get_connector_credential_pair_from_id
3229
from onyx.db.connector_credential_pair import get_connector_credential_pairs
33-
from onyx.db.document import (
34-
delete_all_documents_by_connector_credential_pair__no_commit,
35-
)
3630
from onyx.db.document import get_document_ids_for_connector_credential_pair
3731
from onyx.db.document_set import delete_document_set_cc_pair_relationship__no_commit
3832
from onyx.db.engine import get_session_with_current_tenant
@@ -449,27 +443,15 @@ def monitor_connector_deletion_taskset(
449443
connector_id_to_delete = cc_pair.connector_id
450444
credential_id_to_delete = cc_pair.credential_id
451445

452-
# Explicitly delete document by connector credential pair records before deleting the connector
453-
# This is needed because connector_id is a primary key in that table and cascading deletes won't work
454-
delete_all_documents_by_connector_credential_pair__no_commit(
455-
db_session=db_session,
456-
connector_id=connector_id_to_delete,
457-
credential_id=credential_id_to_delete,
458-
)
446+
# No need to explicitly delete DocumentByConnectorCredentialPair records anymore
447+
# as we have proper cascade relationships set up in the models
459448

460-
# Flush to ensure document deletion happens before connector deletion
449+
# Flush to ensure all operations happen in sequence
461450
db_session.flush()
462451

463-
# Expire the cc_pair to ensure SQLAlchemy doesn't try to manage its state
464-
# related to the deleted DocumentByConnectorCredentialPair during commit
465-
db_session.expire(cc_pair)
452+
# Delete the cc-pair directly
453+
db_session.delete(cc_pair)
466454

467-
# finally, delete the cc-pair
468-
delete_connector_credential_pair__no_commit(
469-
db_session=db_session,
470-
connector_id=connector_id_to_delete,
471-
credential_id=credential_id_to_delete,
472-
)
473455
# if there are no credentials left, delete the connector
474456
connector = fetch_connector_by_id(
475457
db_session=db_session,

backend/onyx/db/document.py

-22
Original file line numberDiff line numberDiff line change
@@ -555,28 +555,6 @@ def delete_documents_by_connector_credential_pair__no_commit(
555555
db_session.execute(stmt)
556556

557557

558-
def delete_all_documents_by_connector_credential_pair__no_commit(
559-
db_session: Session,
560-
connector_id: int,
561-
credential_id: int,
562-
) -> None:
563-
"""Deletes all document by connector credential pair entries for a specific connector and credential.
564-
This is primarily used during connector deletion to ensure all references are removed
565-
before deleting the connector itself. This is crucial because connector_id is part of the
566-
primary key in DocumentByConnectorCredentialPair, and attempting to delete the Connector
567-
would otherwise try to set the foreign key to NULL, which fails for primary keys.
568-
569-
NOTE: Does not commit the transaction, this must be done by the caller.
570-
"""
571-
stmt = delete(DocumentByConnectorCredentialPair).where(
572-
and_(
573-
DocumentByConnectorCredentialPair.connector_id == connector_id,
574-
DocumentByConnectorCredentialPair.credential_id == credential_id,
575-
)
576-
)
577-
db_session.execute(stmt)
578-
579-
580558
def delete_documents__no_commit(db_session: Session, document_ids: list[str]) -> None:
581559
db_session.execute(delete(DbDocument).where(DbDocument.id.in_(document_ids)))
582560

backend/onyx/db/models.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,11 @@ class Credential(Base):
752752
)
753753
documents_by_credential: Mapped[
754754
list["DocumentByConnectorCredentialPair"]
755-
] = relationship("DocumentByConnectorCredentialPair", back_populates="credential")
755+
] = relationship(
756+
"DocumentByConnectorCredentialPair",
757+
back_populates="credential",
758+
cascade="all, delete-orphan",
759+
)
756760

757761
user: Mapped[User | None] = relationship("User", back_populates="credentials")
758762

0 commit comments

Comments
 (0)