Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete API bugfix #82

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions backend/alembic/versions/ee73fc803704_cascade_commit.py
Original file line number Diff line number Diff line change
@@ -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 ###
18 changes: 9 additions & 9 deletions backend/app/models/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ 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):
"""
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,
)


Expand All @@ -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")

Expand All @@ -79,26 +79,26 @@ 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):
"""
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(
to_pg_enum(MessageSubProcessStatusEnum),
default=MessageSubProcessStatusEnum.FINISHED,
nullable=False,
)
metadata_map = Column(JSONB, nullable=True)
metadata_map = Column(JSONB, nullable=True)