
Create a complete e-commerce application with the following features: - User authentication and authorization - Product and category management - Shopping cart functionality - Order processing - Database migrations with Alembic - Comprehensive documentation
156 lines
6.2 KiB
Python
156 lines
6.2 KiB
Python
"""Initial tables
|
|
|
|
Revision ID: 1a2b3c4d5e6f
|
|
Revises:
|
|
Create Date: 2023-11-16 00:00:00.000000
|
|
|
|
"""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision = '1a2b3c4d5e6f'
|
|
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=True),
|
|
sa.Column('hashed_password', sa.String(), nullable=True),
|
|
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)
|
|
|
|
# Create category table
|
|
op.create_table(
|
|
'category',
|
|
sa.Column('id', sa.String(), nullable=False),
|
|
sa.Column('name', sa.String(), nullable=True),
|
|
sa.Column('description', sa.Text(), 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_category_id'), 'category', ['id'], unique=False)
|
|
op.create_index(op.f('ix_category_name'), 'category', ['name'], unique=False)
|
|
|
|
# Create product table
|
|
op.create_table(
|
|
'product',
|
|
sa.Column('id', sa.String(), nullable=False),
|
|
sa.Column('name', sa.String(), nullable=True),
|
|
sa.Column('description', sa.Text(), nullable=True),
|
|
sa.Column('price', sa.Float(), nullable=False),
|
|
sa.Column('stock', sa.Integer(), nullable=False),
|
|
sa.Column('is_active', sa.Boolean(), nullable=True),
|
|
sa.Column('category_id', sa.String(), nullable=True),
|
|
sa.Column('image_url', sa.String(), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.ForeignKeyConstraint(['category_id'], ['category.id'], ),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_product_id'), 'product', ['id'], unique=False)
|
|
op.create_index(op.f('ix_product_name'), 'product', ['name'], unique=False)
|
|
|
|
# Create cart table
|
|
op.create_table(
|
|
'cart',
|
|
sa.Column('id', sa.String(), nullable=False),
|
|
sa.Column('user_id', sa.String(), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
sa.UniqueConstraint('user_id')
|
|
)
|
|
op.create_index(op.f('ix_cart_id'), 'cart', ['id'], unique=False)
|
|
|
|
# Create order table
|
|
op.create_table(
|
|
'order',
|
|
sa.Column('id', sa.String(), nullable=False),
|
|
sa.Column('user_id', sa.String(), nullable=True),
|
|
sa.Column('status', sa.String(), nullable=True),
|
|
sa.Column('total_amount', sa.Float(), nullable=False),
|
|
sa.Column('shipping_address', sa.Text(), nullable=True),
|
|
sa.Column('payment_id', sa.String(), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_order_id'), 'order', ['id'], unique=False)
|
|
|
|
# Create order_item table
|
|
op.create_table(
|
|
'order_item',
|
|
sa.Column('id', sa.String(), nullable=False),
|
|
sa.Column('order_id', sa.String(), nullable=True),
|
|
sa.Column('product_id', sa.String(), nullable=True),
|
|
sa.Column('quantity', sa.Integer(), nullable=False),
|
|
sa.Column('price', sa.Float(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.ForeignKeyConstraint(['order_id'], ['order.id'], ),
|
|
sa.ForeignKeyConstraint(['product_id'], ['product.id'], ),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_order_item_id'), 'order_item', ['id'], unique=False)
|
|
|
|
# Create cart_item table
|
|
op.create_table(
|
|
'cart_item',
|
|
sa.Column('id', sa.String(), nullable=False),
|
|
sa.Column('cart_id', sa.String(), nullable=True),
|
|
sa.Column('product_id', sa.String(), nullable=True),
|
|
sa.Column('quantity', sa.Integer(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.ForeignKeyConstraint(['cart_id'], ['cart.id'], ),
|
|
sa.ForeignKeyConstraint(['product_id'], ['product.id'], ),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
sa.UniqueConstraint('cart_id', 'product_id', name='_cart_product_uc')
|
|
)
|
|
op.create_index(op.f('ix_cart_item_id'), 'cart_item', ['id'], unique=False)
|
|
|
|
|
|
def downgrade() -> None:
|
|
# Drop all tables in reverse order
|
|
op.drop_index(op.f('ix_cart_item_id'), table_name='cart_item')
|
|
op.drop_table('cart_item')
|
|
|
|
op.drop_index(op.f('ix_order_item_id'), table_name='order_item')
|
|
op.drop_table('order_item')
|
|
|
|
op.drop_index(op.f('ix_order_id'), table_name='order')
|
|
op.drop_table('order')
|
|
|
|
op.drop_index(op.f('ix_cart_id'), table_name='cart')
|
|
op.drop_table('cart')
|
|
|
|
op.drop_index(op.f('ix_product_name'), table_name='product')
|
|
op.drop_index(op.f('ix_product_id'), table_name='product')
|
|
op.drop_table('product')
|
|
|
|
op.drop_index(op.f('ix_category_name'), table_name='category')
|
|
op.drop_index(op.f('ix_category_id'), table_name='category')
|
|
op.drop_table('category')
|
|
|
|
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') |