Automated Action 4aac37bc90 Implement inventory management system with FastAPI and SQLite
- Setup project structure with FastAPI application
- Create database models with SQLAlchemy
- Configure Alembic for database migrations
- Implement CRUD operations for products, categories, suppliers
- Add inventory transaction functionality
- Implement user authentication with JWT
- Add health check endpoint
- Create comprehensive documentation
2025-06-05 11:43:07 +00:00

150 lines
6.5 KiB
Python

"""initial migration
Revision ID: 01initial
Revises:
Create Date: 2023-11-12 12:00:00.000000
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '01initial'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# Create the user table
op.create_table(
'user',
sa.Column('id', sa.Integer(), 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'),
sa.UniqueConstraint('email'),
sa.UniqueConstraint('username')
)
op.create_index(op.f('ix_user_email'), 'user', ['email'], unique=True)
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 the category table
op.create_table(
'category',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
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 the supplier table
op.create_table(
'supplier',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('contact_name', sa.String(), nullable=True),
sa.Column('email', sa.String(), nullable=True),
sa.Column('phone', sa.String(), nullable=True),
sa.Column('address', 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_supplier_id'), 'supplier', ['id'], unique=False)
op.create_index(op.f('ix_supplier_name'), 'supplier', ['name'], unique=False)
# Create the product table
op.create_table(
'product',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('sku', sa.String(), nullable=False),
sa.Column('barcode', sa.String(), nullable=True),
sa.Column('quantity', sa.Integer(), nullable=True),
sa.Column('unit_price', sa.Float(), nullable=False),
sa.Column('cost_price', sa.Float(), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=True),
sa.Column('min_stock_level', sa.Integer(), nullable=True),
sa.Column('max_stock_level', sa.Integer(), nullable=True),
sa.Column('category_id', sa.Integer(), nullable=True),
sa.Column('supplier_id', sa.Integer(), 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.ForeignKeyConstraint(['supplier_id'], ['supplier.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('barcode'),
sa.UniqueConstraint('sku')
)
op.create_index(op.f('ix_product_barcode'), 'product', ['barcode'], unique=True)
op.create_index(op.f('ix_product_id'), 'product', ['id'], unique=False)
op.create_index(op.f('ix_product_name'), 'product', ['name'], unique=False)
op.create_index(op.f('ix_product_sku'), 'product', ['sku'], unique=True)
# Define TransactionType enum for SQLAlchemy
transaction_type = sa.Enum('purchase', 'sale', 'adjustment', 'return', 'transfer', name='transactiontype')
transaction_type.create(op.get_bind())
# Create the inventory_transaction table
op.create_table(
'inventory_transaction',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('transaction_type', transaction_type, nullable=False),
sa.Column('quantity', sa.Integer(), nullable=False),
sa.Column('unit_price', sa.Float(), nullable=True),
sa.Column('transaction_date', sa.DateTime(), nullable=True),
sa.Column('notes', sa.Text(), nullable=True),
sa.Column('reference_number', sa.String(), nullable=True),
sa.Column('product_id', sa.Integer(), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['product_id'], ['product.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_inventory_transaction_id'), 'inventory_transaction', ['id'], unique=False)
def downgrade() -> None:
# Drop tables in reverse order
op.drop_index(op.f('ix_inventory_transaction_id'), table_name='inventory_transaction')
op.drop_table('inventory_transaction')
# Drop TransactionType enum
sa.Enum(name='transactiontype').drop(op.get_bind())
op.drop_index(op.f('ix_product_sku'), table_name='product')
op.drop_index(op.f('ix_product_name'), table_name='product')
op.drop_index(op.f('ix_product_id'), table_name='product')
op.drop_index(op.f('ix_product_barcode'), table_name='product')
op.drop_table('product')
op.drop_index(op.f('ix_supplier_name'), table_name='supplier')
op.drop_index(op.f('ix_supplier_id'), table_name='supplier')
op.drop_table('supplier')
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_username'), table_name='user')
op.drop_index(op.f('ix_user_id'), table_name='user')
op.drop_index(op.f('ix_user_email'), table_name='user')
op.drop_table('user')