danswer/backend/danswer/db/deletion_attempt.py
pablodanswer 97ba71e1b3
Db search (#2235)
* k

* update enum imports

* add functional types + model swaps

* remove a log

* remove kv

* fully functional + robustified for kv swap

* validated with hosted + cloud

* ensure not updating current search settings when reindexing

* add instance check

* revert back to updating search settings (will need a slight refactor for endpoint)

* protect advanced config override1

* run pretty

* fix typing

* update typing

* remove unnecessary function

* update model name

* clearer interface names

* validated foreign key constaint

* proper migration

* squash

---------

Co-authored-by: Yuhong Sun <yuhongsun96@gmail.com>
2024-08-27 04:26:51 +00:00

53 lines
1.7 KiB
Python

from sqlalchemy.orm import Session
from danswer.db.index_attempt import get_last_attempt
from danswer.db.models import ConnectorCredentialPair
from danswer.db.models import IndexingStatus
from danswer.db.search_settings import get_current_search_settings
def check_deletion_attempt_is_allowed(
connector_credential_pair: ConnectorCredentialPair,
db_session: Session,
allow_scheduled: bool = False,
) -> str | None:
"""
To be deletable:
(1) connector should be paused
(2) there should be no in-progress/planned index attempts
Returns an error message if the deletion attempt is not allowed, otherwise None.
"""
base_error_msg = (
f"Connector with ID '{connector_credential_pair.connector_id}' and credential ID "
f"'{connector_credential_pair.credential_id}' is not deletable."
)
if connector_credential_pair.status.is_active():
return base_error_msg + " Connector must be paused."
connector_id = connector_credential_pair.connector_id
credential_id = connector_credential_pair.credential_id
search_settings = get_current_search_settings(db_session)
last_indexing = get_last_attempt(
connector_id=connector_id,
credential_id=credential_id,
search_settings_id=search_settings.id,
db_session=db_session,
)
if not last_indexing:
return None
if last_indexing.status == IndexingStatus.IN_PROGRESS or (
last_indexing.status == IndexingStatus.NOT_STARTED and not allow_scheduled
):
return (
base_error_msg
+ " There is an ongoing / planned indexing attempt. "
+ "The indexing attempt must be completed or cancelled before deletion."
)
return None