diff --git a/backend/alembic/versions/ee73fc803704_cascade_commit.py b/backend/alembic/versions/ee73fc803704_cascade_commit.py new file mode 100644 index 00000000..9137c2ea --- /dev/null +++ b/backend/alembic/versions/ee73fc803704_cascade_commit.py @@ -0,0 +1,44 @@ +"""cascade commit + +Revision ID: ee73fc803704 +Revises: 663b3fea3024 +Create Date: 2023-12-03 18:44:07.577117 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision: str = 'ee73fc803704' +down_revision: Union[str, None] = '663b3fea3024' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint('conversationdocument_document_id_fkey', 'conversationdocument', type_='foreignkey') + op.drop_constraint('conversationdocument_conversation_id_fkey', 'conversationdocument', type_='foreignkey') + op.create_foreign_key(None, 'conversationdocument', 'document', ['document_id'], ['id'], ondelete='CASCADE') + op.create_foreign_key(None, 'conversationdocument', 'conversation', ['conversation_id'], ['id'], ondelete='CASCADE') + op.drop_constraint('message_conversation_id_fkey', 'message', type_='foreignkey') + op.create_foreign_key(None, 'message', 'conversation', ['conversation_id'], ['id'], ondelete='CASCADE') + op.drop_constraint('messagesubprocess_message_id_fkey', 'messagesubprocess', type_='foreignkey') + op.create_foreign_key(None, 'messagesubprocess', 'message', ['message_id'], ['id'], ondelete='CASCADE') + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint(None, 'messagesubprocess', type_='foreignkey') + op.create_foreign_key('messagesubprocess_message_id_fkey', 'messagesubprocess', 'message', ['message_id'], ['id']) + op.drop_constraint(None, 'message', type_='foreignkey') + op.create_foreign_key('message_conversation_id_fkey', 'message', 'conversation', ['conversation_id'], ['id']) + op.drop_constraint(None, 'conversationdocument', type_='foreignkey') + op.drop_constraint(None, 'conversationdocument', type_='foreignkey') + op.create_foreign_key('conversationdocument_conversation_id_fkey', 'conversationdocument', 'conversation', ['conversation_id'], ['id']) + op.create_foreign_key('conversationdocument_document_id_fkey', 'conversationdocument', 'document', ['document_id'], ['id']) + # ### end Alembic commands ### diff --git a/backend/app/models/db.py b/backend/app/models/db.py index 36923876..4934d7ff 100644 --- a/backend/app/models/db.py +++ b/backend/app/models/db.py @@ -46,7 +46,7 @@ class Document(Base): # URL to the actual document (e.g. a PDF) url = Column(String, nullable=False, unique=True) metadata_map = Column(JSONB, nullable=True) - conversations = relationship("ConversationDocument", back_populates="document") + conversations = relationship("ConversationDocument", back_populates="document", cascade="all, delete", passive_deletes=True) class Conversation(Base): @@ -54,9 +54,9 @@ class Conversation(Base): A conversation with messages and linked documents """ - messages = relationship("Message", back_populates="conversation") + messages = relationship("Message", back_populates="conversation", cascade="all, delete", passive_deletes=True,) conversation_documents = relationship( - "ConversationDocument", back_populates="conversation" + "ConversationDocument", back_populates="conversation", cascade="all, delete", passive_deletes=True, ) @@ -66,9 +66,9 @@ class ConversationDocument(Base): """ conversation_id = Column( - UUID(as_uuid=True), ForeignKey("conversation.id"), index=True + UUID(as_uuid=True), ForeignKey("conversation.id", ondelete="CASCADE"), index=True ) - document_id = Column(UUID(as_uuid=True), ForeignKey("document.id"), index=True) + document_id = Column(UUID(as_uuid=True), ForeignKey("document.id", ondelete="CASCADE"), index=True) conversation = relationship("Conversation", back_populates="conversation_documents") document = relationship("Document", back_populates="conversations") @@ -79,13 +79,13 @@ class Message(Base): """ conversation_id = Column( - UUID(as_uuid=True), ForeignKey("conversation.id"), index=True + UUID(as_uuid=True), ForeignKey("conversation.id", ondelete="CASCADE"), index=True ) content = Column(String) role = Column(to_pg_enum(MessageRoleEnum)) status = Column(to_pg_enum(MessageStatusEnum), default=MessageStatusEnum.PENDING) conversation = relationship("Conversation", back_populates="messages") - sub_processes = relationship("MessageSubProcess", back_populates="message") + sub_processes = relationship("MessageSubProcess", back_populates="message", cascade="all, delete", passive_deletes=True) class MessageSubProcess(Base): @@ -93,7 +93,7 @@ class MessageSubProcess(Base): A record of a sub-process that occurred as part of the generation of a message from an AI assistant """ - message_id = Column(UUID(as_uuid=True), ForeignKey("message.id"), index=True) + message_id = Column(UUID(as_uuid=True), ForeignKey("message.id", ondelete="CASCADE"), index=True) source = Column(to_pg_enum(MessageSubProcessSourceEnum)) message = relationship("Message", back_populates="sub_processes") status = Column( @@ -101,4 +101,4 @@ class MessageSubProcess(Base): default=MessageSubProcessStatusEnum.FINISHED, nullable=False, ) - metadata_map = Column(JSONB, nullable=True) + metadata_map = Column(JSONB, nullable=True) \ No newline at end of file