
- Set up project structure with FastAPI and SQLite - Implement user authentication using JWT - Create database models for users, conversations, and messages - Implement API endpoints for user management and chat functionality - Set up WebSocket for real-time messaging - Add database migrations with Alembic - Create health check endpoint - Update README with comprehensive documentation generated with BackendIM... (backend.im)
85 lines
3.6 KiB
Python
85 lines
3.6 KiB
Python
"""Initial migration
|
|
|
|
Revision ID: 001
|
|
Revises:
|
|
Create Date: 2025-05-12
|
|
|
|
"""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision = '001'
|
|
down_revision = None
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
# Create users table
|
|
op.create_table(
|
|
'users',
|
|
sa.Column('id', sa.String(), nullable=False),
|
|
sa.Column('username', sa.String(), nullable=False),
|
|
sa.Column('email', sa.String(), nullable=False),
|
|
sa.Column('hashed_password', sa.String(), nullable=False),
|
|
sa.Column('is_active', sa.Boolean(), nullable=True, default=True),
|
|
sa.Column('created_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
|
|
op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False)
|
|
op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True)
|
|
|
|
# Create conversations table
|
|
op.create_table(
|
|
'conversations',
|
|
sa.Column('id', sa.String(), nullable=False),
|
|
sa.Column('name', sa.String(), nullable=True),
|
|
sa.Column('is_group', sa.String(), nullable=True, default=False),
|
|
sa.Column('created_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_conversations_id'), 'conversations', ['id'], unique=False)
|
|
|
|
# Create conversation_participants table
|
|
op.create_table(
|
|
'conversation_participants',
|
|
sa.Column('conversation_id', sa.String(), nullable=False),
|
|
sa.Column('user_id', sa.String(), nullable=False),
|
|
sa.ForeignKeyConstraint(['conversation_id'], ['conversations.id'], ),
|
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
|
sa.PrimaryKeyConstraint('conversation_id', 'user_id')
|
|
)
|
|
|
|
# Create messages table
|
|
op.create_table(
|
|
'messages',
|
|
sa.Column('id', sa.String(), nullable=False),
|
|
sa.Column('content', sa.String(), nullable=False),
|
|
sa.Column('sender_id', sa.String(), nullable=False),
|
|
sa.Column('recipient_id', sa.String(), nullable=True),
|
|
sa.Column('conversation_id', sa.String(), nullable=False),
|
|
sa.Column('is_read', sa.Boolean(), nullable=True, default=False),
|
|
sa.Column('created_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
|
sa.ForeignKeyConstraint(['conversation_id'], ['conversations.id'], ),
|
|
sa.ForeignKeyConstraint(['recipient_id'], ['users.id'], ),
|
|
sa.ForeignKeyConstraint(['sender_id'], ['users.id'], ),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_messages_id'), 'messages', ['id'], unique=False)
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_index(op.f('ix_messages_id'), table_name='messages')
|
|
op.drop_table('messages')
|
|
op.drop_table('conversation_participants')
|
|
op.drop_index(op.f('ix_conversations_id'), table_name='conversations')
|
|
op.drop_table('conversations')
|
|
op.drop_index(op.f('ix_users_username'), table_name='users')
|
|
op.drop_index(op.f('ix_users_id'), table_name='users')
|
|
op.drop_index(op.f('ix_users_email'), table_name='users')
|
|
op.drop_table('users') |