diff --git a/backend/alembic/versions/5b29123cd710_nullable_search_settings_for_historic_.py b/backend/alembic/versions/5b29123cd710_nullable_search_settings_for_historic_.py new file mode 100644 index 000000000..58164cd4c --- /dev/null +++ b/backend/alembic/versions/5b29123cd710_nullable_search_settings_for_historic_.py @@ -0,0 +1,70 @@ +"""nullable search settings for historic index attempts + +Revision ID: 5b29123cd710 +Revises: 949b4a92a401 +Create Date: 2024-10-30 19:37:59.630704 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "5b29123cd710" +down_revision = "949b4a92a401" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # Drop the existing foreign key constraint + op.drop_constraint( + "fk_index_attempt_search_settings", "index_attempt", type_="foreignkey" + ) + + # Modify the column to be nullable + op.alter_column( + "index_attempt", "search_settings_id", existing_type=sa.INTEGER(), nullable=True + ) + + # Add back the foreign key with ON DELETE SET NULL + op.create_foreign_key( + "fk_index_attempt_search_settings", + "index_attempt", + "search_settings", + ["search_settings_id"], + ["id"], + ondelete="SET NULL", + ) + + +def downgrade() -> None: + # Warning: This will delete all index attempts that don't have search settings + op.execute( + """ + DELETE FROM index_attempt + WHERE search_settings_id IS NULL + """ + ) + + # Drop foreign key constraint + op.drop_constraint( + "fk_index_attempt_search_settings", "index_attempt", type_="foreignkey" + ) + + # Modify the column to be not nullable + op.alter_column( + "index_attempt", + "search_settings_id", + existing_type=sa.INTEGER(), + nullable=False, + ) + + # Add back the foreign key without ON DELETE SET NULL + op.create_foreign_key( + "fk_index_attempt_search_settings", + "index_attempt", + "search_settings", + ["search_settings_id"], + ["id"], + ) diff --git a/backend/danswer/background/indexing/run_indexing.py b/backend/danswer/background/indexing/run_indexing.py index d95a6a70d..252309e3f 100644 --- a/backend/danswer/background/indexing/run_indexing.py +++ b/backend/danswer/background/indexing/run_indexing.py @@ -118,7 +118,13 @@ def _run_indexing( """ start_time = time.time() + if index_attempt.search_settings is None: + raise ValueError( + "Search settings must be set for indexing. This should not be possible." + ) + search_settings = index_attempt.search_settings + index_name = search_settings.index_name # Only update cc-pair status for primary index jobs diff --git a/backend/danswer/db/models.py b/backend/danswer/db/models.py index bee693534..3f63d1d0d 100644 --- a/backend/danswer/db/models.py +++ b/backend/danswer/db/models.py @@ -734,9 +734,10 @@ class IndexAttempt(Base): full_exception_trace: Mapped[str | None] = mapped_column(Text, default=None) # Nullable because in the past, we didn't allow swapping out embedding models live search_settings_id: Mapped[int] = mapped_column( - ForeignKey("search_settings.id"), - nullable=False, + ForeignKey("search_settings.id", ondelete="SET NULL"), + nullable=True, ) + time_created: Mapped[datetime.datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), @@ -756,7 +757,7 @@ class IndexAttempt(Base): "ConnectorCredentialPair", back_populates="index_attempts" ) - search_settings: Mapped[SearchSettings] = relationship( + search_settings: Mapped[SearchSettings | None] = relationship( "SearchSettings", back_populates="index_attempts" )