Skip to content
Merged
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
18 changes: 10 additions & 8 deletions backend/alembic/versions/6e2d1d2b7c4a_add_ai_interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@


def upgrade() -> None:
# Create Enum types
# Note: Using sa.Enum in create_table will automatically create the type if it doesn't exist,
# but for PostgreSQL it's often better to create it explicitly if we want to be safe.
# Safely create enum types if they don't exist
op.execute("DO $$ BEGIN CREATE TYPE ai_interaction_type AS ENUM ('highlight', 'question', 'summary', 'quiz'); EXCEPTION WHEN duplicate_object THEN null; END $$;")
op.execute("DO $$ BEGIN CREATE TYPE ai_action_type AS ENUM ('explain_simple', 'define', 'analogy', 'example', 'expand_acronym'); EXCEPTION WHEN duplicate_object THEN null; END $$;")

# Add new value 'answer_question' to ai_action_type if it doesn't already exist
op.execute("ALTER TYPE ai_action_type ADD VALUE IF NOT EXISTS 'answer_question'")

op.create_table('ai_interactions',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('user_id', sa.UUID(), nullable=False),
sa.Column('document_id', sa.UUID(), nullable=False),
sa.Column('interaction_type', sa.Enum('highlight', 'question', 'summary', 'quiz', name='aiinteractiontype'), nullable=False),
sa.Column('action', sa.Enum('explain_simple', 'define', 'analogy', 'example', 'expand_acronym', 'answer_question', name='aiactiontype'), nullable=False),
sa.Column('interaction_type', sa.Enum('highlight', 'question', 'summary', 'quiz', name='ai_interaction_type'), nullable=False),
sa.Column('action', sa.Enum('explain_simple', 'define', 'analogy', 'example', 'expand_acronym', 'answer_question', name='ai_action_type'), nullable=False),
sa.Column('input_text', sa.Text(), nullable=False),
sa.Column('response_text', sa.Text(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
Expand All @@ -49,6 +52,5 @@ def downgrade() -> None:
op.drop_table('ai_interactions')

# Drop types
# Note: Be careful with dropping types if they are used elsewhere, but here they are specific to this table.
sa.Enum(name='aiinteractiontype').drop(op.get_bind())
sa.Enum(name='aiactiontype').drop(op.get_bind())
sa.Enum(name='ai_interaction_type').drop(op.get_bind())
sa.Enum(name='ai_action_type').drop(op.get_bind())
4 changes: 2 additions & 2 deletions backend/app/models/ai_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ class AIInteraction(Base):
)

interaction_type = Column(
Enum(AIInteractionType),
Enum(AIInteractionType, name="ai_interaction_type", create_type=False),
nullable=False
)

action = Column(
Enum(AIActionType),
Enum(AIActionType, name="ai_action_type", create_type=False),
nullable=False
)

Expand Down
Loading