
This commit includes: - Project structure setup with FastAPI and SQLite - Database models and schemas for inventory management - CRUD operations for all entities - API endpoints for product, category, supplier, and inventory management - User authentication with JWT tokens - Initial database migration - Comprehensive README with setup instructions
156 lines
7.7 KiB
Python
156 lines
7.7 KiB
Python
"""Initial setup
|
|
|
|
Revision ID: 01_initial_setup
|
|
Revises:
|
|
Create Date: 2023-11-22
|
|
|
|
"""
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision: str = '01_initial_setup'
|
|
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 user table
|
|
op.create_table(
|
|
'user',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('email', sa.String(), nullable=False),
|
|
sa.Column('full_name', sa.String(), nullable=True),
|
|
sa.Column('hashed_password', sa.String(), nullable=False),
|
|
sa.Column('is_active', sa.Boolean(), nullable=True),
|
|
sa.Column('is_superuser', sa.Boolean(), nullable=True),
|
|
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.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.Integer(), nullable=False),
|
|
sa.Column('name', sa.String(length=255), nullable=False),
|
|
sa.Column('description', sa.Text(), nullable=True),
|
|
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.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=True)
|
|
|
|
# Create supplier table
|
|
op.create_table(
|
|
'supplier',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('name', sa.String(length=255), nullable=False),
|
|
sa.Column('contact_name', sa.String(length=255), nullable=True),
|
|
sa.Column('email', sa.String(length=255), nullable=True),
|
|
sa.Column('phone', sa.String(length=50), nullable=True),
|
|
sa.Column('address', sa.Text(), nullable=True),
|
|
sa.Column('website', sa.String(length=255), nullable=True),
|
|
sa.Column('notes', sa.Text(), nullable=True),
|
|
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.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 product table
|
|
op.create_table(
|
|
'product',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('name', sa.String(length=255), nullable=False),
|
|
sa.Column('description', sa.Text(), nullable=True),
|
|
sa.Column('sku', sa.String(length=50), nullable=False),
|
|
sa.Column('barcode', sa.String(length=100), nullable=True),
|
|
sa.Column('price', sa.Float(), nullable=False, default=0.0),
|
|
sa.Column('cost', sa.Float(), nullable=False, default=0.0),
|
|
sa.Column('min_stock_level', sa.Integer(), nullable=False, default=0),
|
|
sa.Column('is_active', sa.Boolean(), nullable=True),
|
|
sa.Column('category_id', sa.Integer(), nullable=True),
|
|
sa.Column('supplier_id', sa.Integer(), nullable=True),
|
|
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.ForeignKeyConstraint(['category_id'], ['category.id'], ),
|
|
sa.ForeignKeyConstraint(['supplier_id'], ['supplier.id'], ),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
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)
|
|
|
|
# Create inventory table
|
|
op.create_table(
|
|
'inventory',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('product_id', sa.Integer(), nullable=False),
|
|
sa.Column('quantity', sa.Integer(), nullable=False, default=0),
|
|
sa.Column('location', sa.String(length=255), nullable=True),
|
|
sa.Column('last_counted_at', sa.DateTime(timezone=True), nullable=True),
|
|
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.ForeignKeyConstraint(['product_id'], ['product.id'], ),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_inventory_id'), 'inventory', ['id'], unique=False)
|
|
|
|
# Create inventory_transaction table
|
|
op.create_table(
|
|
'inventory_transaction',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('product_id', sa.Integer(), nullable=False),
|
|
sa.Column('quantity', sa.Integer(), nullable=False),
|
|
sa.Column('transaction_type', sa.String(length=50), nullable=False),
|
|
sa.Column('reference', sa.String(length=255), nullable=True),
|
|
sa.Column('unit_price', sa.Float(), nullable=True),
|
|
sa.Column('notes', sa.Text(), nullable=True),
|
|
sa.Column('transaction_date', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
|
sa.Column('user_id', sa.Integer(), nullable=True),
|
|
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.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')
|
|
|
|
op.drop_index(op.f('ix_inventory_id'), table_name='inventory')
|
|
op.drop_table('inventory')
|
|
|
|
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_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') |