
- Set up project structure and FastAPI application - Create database models for users, products, and inventory - Configure SQLAlchemy and Alembic for database management - Implement JWT authentication - Create API endpoints for user, product, and inventory management - Add admin-only routes and authorization middleware - Add health check endpoint - Update README with documentation - Lint and fix code issues
125 lines
5.8 KiB
Python
125 lines
5.8 KiB
Python
"""Initial schema
|
|
|
|
Revision ID: 00001
|
|
Revises:
|
|
Create Date: 2023-10-01
|
|
|
|
"""
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision = '00001'
|
|
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('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('role', sa.Enum('admin', 'customer', 'staff', name='userrole'), nullable=False),
|
|
sa.Column('is_active', sa.Boolean(), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
|
|
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
|
|
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 categories table
|
|
op.create_table(
|
|
'categories',
|
|
sa.Column('id', sa.String(), nullable=False),
|
|
sa.Column('name', sa.String(), nullable=False),
|
|
sa.Column('description', sa.Text(), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
|
|
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
)
|
|
op.create_index(op.f('ix_categories_id'), 'categories', ['id'], unique=False)
|
|
op.create_index(op.f('ix_categories_name'), 'categories', ['name'], unique=True)
|
|
|
|
# Create products table
|
|
op.create_table(
|
|
'products',
|
|
sa.Column('id', sa.String(), nullable=False),
|
|
sa.Column('name', sa.String(), nullable=False),
|
|
sa.Column('description', sa.Text(), nullable=True),
|
|
sa.Column('price', sa.Float(), nullable=False),
|
|
sa.Column('sku', sa.String(), nullable=False),
|
|
sa.Column('is_active', sa.Boolean(), nullable=True),
|
|
sa.Column('category_id', sa.String(), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
|
|
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
|
|
sa.ForeignKeyConstraint(['category_id'], ['categories.id'], ),
|
|
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)
|
|
op.create_index(op.f('ix_products_sku'), 'products', ['sku'], unique=True)
|
|
|
|
# Create inventory_items table
|
|
op.create_table(
|
|
'inventory_items',
|
|
sa.Column('id', sa.String(), nullable=False),
|
|
sa.Column('quantity', sa.Integer(), nullable=False),
|
|
sa.Column('status', sa.Enum('in_stock', 'low_stock', 'out_of_stock', 'discontinued', name='inventorystatus'), nullable=False),
|
|
sa.Column('location', sa.String(), nullable=True),
|
|
sa.Column('notes', sa.Text(), nullable=True),
|
|
sa.Column('product_id', sa.String(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
|
|
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
|
|
sa.ForeignKeyConstraint(['product_id'], ['products.id'], ),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
)
|
|
op.create_index(op.f('ix_inventory_items_id'), 'inventory_items', ['id'], unique=False)
|
|
|
|
# Create inventory_transactions table
|
|
op.create_table(
|
|
'inventory_transactions',
|
|
sa.Column('id', sa.String(), nullable=False),
|
|
sa.Column('product_id', sa.String(), nullable=False),
|
|
sa.Column('quantity_change', sa.Integer(), nullable=False),
|
|
sa.Column('notes', sa.Text(), nullable=True),
|
|
sa.Column('transaction_by', sa.String(), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
|
|
sa.ForeignKeyConstraint(['product_id'], ['products.id'], ),
|
|
sa.ForeignKeyConstraint(['transaction_by'], ['users.id'], ),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
)
|
|
op.create_index(op.f('ix_inventory_transactions_id'), 'inventory_transactions', ['id'], unique=False)
|
|
|
|
|
|
def downgrade() -> None:
|
|
# Drop tables in reverse order to handle foreign key constraints
|
|
op.drop_index(op.f('ix_inventory_transactions_id'), table_name='inventory_transactions')
|
|
op.drop_table('inventory_transactions')
|
|
|
|
op.drop_index(op.f('ix_inventory_items_id'), table_name='inventory_items')
|
|
op.drop_table('inventory_items')
|
|
|
|
op.drop_index(op.f('ix_products_sku'), table_name='products')
|
|
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')
|
|
|
|
op.drop_index(op.f('ix_categories_name'), table_name='categories')
|
|
op.drop_index(op.f('ix_categories_id'), table_name='categories')
|
|
op.drop_table('categories')
|
|
|
|
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')
|
|
|
|
# Drop enum types after tables
|
|
op.execute('DROP TYPE userrole')
|
|
op.execute('DROP TYPE inventorystatus') |