|
| 1 | +"""Initial schema with all tables |
| 2 | +
|
| 3 | +Revision ID: 001_initial |
| 4 | +Revises: |
| 5 | +Create Date: 2024-12-13 |
| 6 | +
|
| 7 | +""" |
| 8 | +from alembic import op |
| 9 | +import sqlalchemy as sa |
| 10 | +from sqlalchemy.dialects import postgresql |
| 11 | + |
| 12 | +# revision identifiers, used by Alembic. |
| 13 | +revision = '001_initial' |
| 14 | +down_revision = None |
| 15 | +branch_labels = None |
| 16 | +depends_on = None |
| 17 | + |
| 18 | + |
| 19 | +def upgrade() -> None: |
| 20 | + # Create conversations table |
| 21 | + op.create_table('conversations', |
| 22 | + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), |
| 23 | + sa.Column('title', sa.String(255), nullable=True), |
| 24 | + sa.Column('session_id', sa.String(255), nullable=True), |
| 25 | + sa.Column('created_at', sa.DateTime(), nullable=True), |
| 26 | + sa.Column('updated_at', sa.DateTime(), nullable=True), |
| 27 | + sa.PrimaryKeyConstraint('id') |
| 28 | + ) |
| 29 | + op.create_index('ix_conversations_session_id', 'conversations', ['session_id'], unique=False) |
| 30 | + |
| 31 | + # Create messages table |
| 32 | + op.create_table('messages', |
| 33 | + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), |
| 34 | + sa.Column('conversation_id', postgresql.UUID(as_uuid=True), nullable=False), |
| 35 | + sa.Column('role', sa.String(20), nullable=False), |
| 36 | + sa.Column('content', sa.Text(), nullable=False), |
| 37 | + sa.Column('tokens_used', sa.Integer(), nullable=True), |
| 38 | + sa.Column('latency_ms', sa.Integer(), nullable=True), |
| 39 | + sa.Column('created_at', sa.DateTime(), nullable=True), |
| 40 | + sa.ForeignKeyConstraint(['conversation_id'], ['conversations.id'], ), |
| 41 | + sa.PrimaryKeyConstraint('id') |
| 42 | + ) |
| 43 | + |
| 44 | + # Create documents table (for RAG chunks) |
| 45 | + op.create_table('documents', |
| 46 | + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), |
| 47 | + sa.Column('title', sa.String(255), nullable=True), |
| 48 | + sa.Column('source', sa.String(255), nullable=True), |
| 49 | + sa.Column('created_at', sa.DateTime(), nullable=True), |
| 50 | + sa.Column('content', sa.Text(), nullable=True), |
| 51 | + sa.Column('tsv', postgresql.TSVECTOR(), nullable=True), |
| 52 | + sa.PrimaryKeyConstraint('id') |
| 53 | + ) |
| 54 | + op.create_index('idx_docs_tsv', 'documents', ['tsv'], unique=False, postgresql_using='gin') |
| 55 | + |
| 56 | + # Create uploaded_files table |
| 57 | + op.create_table('uploaded_files', |
| 58 | + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), |
| 59 | + sa.Column('filename', sa.String(255), nullable=False), |
| 60 | + sa.Column('file_type', sa.String(50), nullable=False), |
| 61 | + sa.Column('file_size', sa.Integer(), nullable=True), |
| 62 | + sa.Column('chunk_count', sa.Integer(), nullable=True), |
| 63 | + sa.Column('status', sa.String(20), nullable=True), |
| 64 | + sa.Column('error_message', sa.Text(), nullable=True), |
| 65 | + sa.Column('created_at', sa.DateTime(), nullable=True), |
| 66 | + sa.Column('processed_at', sa.DateTime(), nullable=True), |
| 67 | + sa.PrimaryKeyConstraint('id') |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +def downgrade() -> None: |
| 72 | + op.drop_table('uploaded_files') |
| 73 | + op.drop_index('idx_docs_tsv', table_name='documents', postgresql_using='gin') |
| 74 | + op.drop_table('documents') |
| 75 | + op.drop_table('messages') |
| 76 | + op.drop_index('ix_conversations_session_id', table_name='conversations') |
| 77 | + op.drop_table('conversations') |
0 commit comments