
- Set up project structure with FastAPI and SQLite - Create models for products and cart items - Implement schemas for API request/response validation - Add database migrations with Alembic - Create service layer for business logic - Implement API endpoints for cart operations - Add health endpoint for monitoring - Update documentation - Fix linting issues
78 lines
3.3 KiB
Python
78 lines
3.3 KiB
Python
"""initial migration
|
|
|
|
Revision ID: 00001
|
|
Revises:
|
|
Create Date: 2023-11-15
|
|
|
|
"""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision = '00001'
|
|
down_revision = None
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
# Create products table
|
|
op.create_table(
|
|
'products',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
|
sa.Column('name', sa.String(length=255), nullable=False),
|
|
sa.Column('description', sa.Text(), nullable=True),
|
|
sa.Column('price', sa.Float(), nullable=False),
|
|
sa.Column('stock', sa.Integer(), nullable=False),
|
|
sa.Column('image_url', sa.String(length=512), nullable=True),
|
|
sa.Column('is_active', sa.Boolean(), nullable=False),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_products_id'), 'products', ['id'], unique=False)
|
|
op.create_index(op.f('ix_products_name'), 'products', ['name'], unique=False)
|
|
|
|
# Create carts table
|
|
op.create_table(
|
|
'carts',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
|
sa.Column('user_id', sa.String(length=255), nullable=False),
|
|
sa.Column('is_active', sa.Integer(), nullable=False),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_carts_id'), 'carts', ['id'], unique=False)
|
|
op.create_index(op.f('ix_carts_user_id'), 'carts', ['user_id'], unique=False)
|
|
|
|
# Create cart_items table
|
|
op.create_table(
|
|
'cart_items',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
|
sa.Column('cart_id', sa.Integer(), nullable=False),
|
|
sa.Column('product_id', sa.Integer(), nullable=False),
|
|
sa.Column('quantity', sa.Integer(), nullable=False),
|
|
sa.Column('unit_price', sa.Float(), nullable=False),
|
|
sa.ForeignKeyConstraint(['cart_id'], ['carts.id'], ondelete='CASCADE'),
|
|
sa.ForeignKeyConstraint(['product_id'], ['products.id'], ondelete='CASCADE'),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
sa.UniqueConstraint('cart_id', 'product_id', name='uix_cart_product')
|
|
)
|
|
op.create_index(op.f('ix_cart_items_id'), 'cart_items', ['id'], unique=False)
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_index(op.f('ix_cart_items_id'), table_name='cart_items')
|
|
op.drop_table('cart_items')
|
|
|
|
op.drop_index(op.f('ix_carts_user_id'), table_name='carts')
|
|
op.drop_index(op.f('ix_carts_id'), table_name='carts')
|
|
op.drop_table('carts')
|
|
|
|
op.drop_index(op.f('ix_products_name'), table_name='products')
|
|
op.drop_index(op.f('ix_products_id'), table_name='products')
|
|
op.drop_table('products') |