
- Create project structure with app organization - Set up FastAPI application with CORS and health endpoint - Implement database models with SQLAlchemy (User, Post, Comment) - Set up Alembic for database migrations - Implement authentication with JWT tokens - Create CRUD operations for all models - Implement REST API endpoints for users, posts, and comments - Add comprehensive documentation in README.md
86 lines
3.2 KiB
Python
86 lines
3.2 KiB
Python
"""initial tables
|
|
|
|
Revision ID: 001
|
|
Revises:
|
|
Create Date: 2023-05-08 10:00:00.000000
|
|
|
|
"""
|
|
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 user table
|
|
op.create_table(
|
|
'user',
|
|
sa.Column('id', sa.String(), nullable=False),
|
|
sa.Column('email', sa.String(), nullable=False),
|
|
sa.Column('username', sa.String(), nullable=False),
|
|
sa.Column('hashed_password', sa.String(), nullable=False),
|
|
sa.Column('full_name', sa.String(), nullable=True),
|
|
sa.Column('is_active', sa.Boolean(), nullable=True),
|
|
sa.Column('is_superuser', sa.Boolean(), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
)
|
|
op.create_index(op.f('ix_user_email'), 'user', ['email'], unique=True)
|
|
op.create_index(op.f('ix_user_full_name'), 'user', ['full_name'], unique=False)
|
|
op.create_index(op.f('ix_user_id'), 'user', ['id'], unique=False)
|
|
op.create_index(op.f('ix_user_username'), 'user', ['username'], unique=True)
|
|
|
|
# Create post table
|
|
op.create_table(
|
|
'post',
|
|
sa.Column('id', sa.String(), nullable=False),
|
|
sa.Column('title', sa.String(), nullable=False),
|
|
sa.Column('content', sa.Text(), nullable=False),
|
|
sa.Column('is_published', sa.Boolean(), nullable=True),
|
|
sa.Column('author_id', sa.String(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.ForeignKeyConstraint(['author_id'], ['user.id'], ),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_post_id'), 'post', ['id'], unique=False)
|
|
op.create_index(op.f('ix_post_title'), 'post', ['title'], unique=False)
|
|
|
|
# Create comment table
|
|
op.create_table(
|
|
'comment',
|
|
sa.Column('id', sa.String(), nullable=False),
|
|
sa.Column('content', sa.Text(), nullable=False),
|
|
sa.Column('post_id', sa.String(), nullable=False),
|
|
sa.Column('author_id', sa.String(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.ForeignKeyConstraint(['author_id'], ['user.id'], ),
|
|
sa.ForeignKeyConstraint(['post_id'], ['post.id'], ),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_comment_id'), 'comment', ['id'], unique=False)
|
|
|
|
|
|
def downgrade() -> None:
|
|
# Drop comment table
|
|
op.drop_index(op.f('ix_comment_id'), table_name='comment')
|
|
op.drop_table('comment')
|
|
|
|
# Drop post table
|
|
op.drop_index(op.f('ix_post_title'), table_name='post')
|
|
op.drop_index(op.f('ix_post_id'), table_name='post')
|
|
op.drop_table('post')
|
|
|
|
# Drop user table
|
|
op.drop_index(op.f('ix_user_username'), table_name='user')
|
|
op.drop_index(op.f('ix_user_id'), table_name='user')
|
|
op.drop_index(op.f('ix_user_full_name'), table_name='user')
|
|
op.drop_index(op.f('ix_user_email'), table_name='user')
|
|
op.drop_table('user') |