
- Added Python path configuration in migrations/env.py to correctly import app modules - Fixed SQLite database path configuration - Fixed migration revision ID format - Added proper error handling for imports Generated with BackendIM... (backend.im)
220 lines
9.7 KiB
Python
220 lines
9.7 KiB
Python
"""initial migration
|
|
|
|
Revision ID: 00001
|
|
Revises:
|
|
Create Date: 2025-05-12
|
|
|
|
"""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
from sqlalchemy import Column, Integer, String, Float, DateTime, Text, Boolean, ForeignKey, Enum
|
|
import enum
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision = '00001'
|
|
down_revision = None
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
# Enum classes for migration
|
|
class LocationType(str, enum.Enum):
|
|
WAREHOUSE = "warehouse"
|
|
STORE = "store"
|
|
SUPPLIER = "supplier"
|
|
|
|
class TransactionType(str, enum.Enum):
|
|
PURCHASE = "purchase"
|
|
SALE = "sale"
|
|
TRANSFER = "transfer"
|
|
ADJUSTMENT = "adjustment"
|
|
RETURN = "return"
|
|
|
|
def upgrade():
|
|
# Categories table
|
|
op.create_table(
|
|
'categories',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.Column('name', sa.String(length=100), nullable=False),
|
|
sa.Column('description', sa.Text(), nullable=True),
|
|
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)
|
|
|
|
# Products table
|
|
op.create_table(
|
|
'products',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
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('category_id', sa.Integer(), nullable=True),
|
|
sa.Column('price', sa.Float(), nullable=False),
|
|
sa.Column('cost_price', sa.Float(), nullable=False),
|
|
sa.Column('barcode', sa.String(length=100), nullable=True),
|
|
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)
|
|
op.create_index(op.f('ix_products_barcode'), 'products', ['barcode'], unique=True)
|
|
|
|
# Locations table
|
|
op.create_table(
|
|
'locations',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.Column('name', sa.String(length=100), nullable=False),
|
|
sa.Column('type', sa.Enum('warehouse', 'store', 'supplier', name='locationtype'), nullable=False),
|
|
sa.Column('address', sa.String(length=255), nullable=True),
|
|
sa.Column('description', sa.String(length=255), nullable=True),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_locations_id'), 'locations', ['id'], unique=False)
|
|
|
|
# Inventory Transactions table
|
|
op.create_table(
|
|
'inventory_transactions',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.Column('type', sa.Enum('purchase', 'sale', 'transfer', 'adjustment', 'return', name='transactiontype'), nullable=False),
|
|
sa.Column('reference_id', sa.String(length=100), nullable=True),
|
|
sa.Column('notes', sa.String(length=255), nullable=True),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_inventory_transactions_id'), 'inventory_transactions', ['id'], unique=False)
|
|
|
|
# Inventory Items table
|
|
op.create_table(
|
|
'inventory_items',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.Column('product_id', sa.Integer(), nullable=False),
|
|
sa.Column('location_id', sa.Integer(), nullable=False),
|
|
sa.Column('quantity', sa.Float(), nullable=False),
|
|
sa.Column('minimum_stock', sa.Float(), nullable=False),
|
|
sa.Column('maximum_stock', sa.Float(), nullable=True),
|
|
sa.ForeignKeyConstraint(['location_id'], ['locations.id'], ),
|
|
sa.ForeignKeyConstraint(['product_id'], ['products.id'], ),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_inventory_items_id'), 'inventory_items', ['id'], unique=False)
|
|
|
|
# Inventory Transaction Items table
|
|
op.create_table(
|
|
'inventory_transaction_items',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.Column('transaction_id', sa.Integer(), nullable=False),
|
|
sa.Column('product_id', sa.Integer(), nullable=False),
|
|
sa.Column('quantity', sa.Float(), nullable=False),
|
|
sa.Column('from_location_id', sa.Integer(), nullable=True),
|
|
sa.Column('to_location_id', sa.Integer(), nullable=True),
|
|
sa.ForeignKeyConstraint(['from_location_id'], ['locations.id'], ),
|
|
sa.ForeignKeyConstraint(['product_id'], ['products.id'], ),
|
|
sa.ForeignKeyConstraint(['to_location_id'], ['locations.id'], ),
|
|
sa.ForeignKeyConstraint(['transaction_id'], ['inventory_transactions.id'], ),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_inventory_transaction_items_id'), 'inventory_transaction_items', ['id'], unique=False)
|
|
|
|
# Suppliers table
|
|
op.create_table(
|
|
'suppliers',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.Column('name', sa.String(length=255), nullable=False),
|
|
sa.Column('contact_name', sa.String(length=100), nullable=True),
|
|
sa.Column('email', sa.String(length=255), nullable=True),
|
|
sa.Column('phone', sa.String(length=20), nullable=True),
|
|
sa.Column('address', sa.Text(), nullable=True),
|
|
sa.Column('notes', sa.Text(), nullable=True),
|
|
sa.Column('is_active', sa.Boolean(), nullable=True),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_suppliers_id'), 'suppliers', ['id'], unique=False)
|
|
op.create_index(op.f('ix_suppliers_name'), 'suppliers', ['name'], unique=False)
|
|
|
|
# Purchases table
|
|
op.create_table(
|
|
'purchases',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.Column('supplier_id', sa.Integer(), nullable=False),
|
|
sa.Column('reference_number', sa.String(length=100), nullable=True),
|
|
sa.Column('order_date', sa.DateTime(), nullable=True),
|
|
sa.Column('delivery_date', sa.DateTime(), nullable=True),
|
|
sa.Column('total_amount', sa.Float(), nullable=False),
|
|
sa.Column('notes', sa.Text(), nullable=True),
|
|
sa.ForeignKeyConstraint(['supplier_id'], ['suppliers.id'], ),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
sa.UniqueConstraint('reference_number')
|
|
)
|
|
op.create_index(op.f('ix_purchases_id'), 'purchases', ['id'], unique=False)
|
|
|
|
# Purchase Items table
|
|
op.create_table(
|
|
'purchase_items',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.Column('purchase_id', sa.Integer(), nullable=False),
|
|
sa.Column('product_id', sa.Integer(), nullable=False),
|
|
sa.Column('quantity', sa.Float(), nullable=False),
|
|
sa.Column('unit_price', sa.Float(), nullable=False),
|
|
sa.Column('total_price', sa.Float(), nullable=False),
|
|
sa.ForeignKeyConstraint(['product_id'], ['products.id'], ),
|
|
sa.ForeignKeyConstraint(['purchase_id'], ['purchases.id'], ),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
op.create_index(op.f('ix_purchase_items_id'), 'purchase_items', ['id'], unique=False)
|
|
|
|
|
|
def downgrade():
|
|
# Drop tables in reverse order (considering foreign key constraints)
|
|
op.drop_index(op.f('ix_purchase_items_id'), table_name='purchase_items')
|
|
op.drop_table('purchase_items')
|
|
|
|
op.drop_index(op.f('ix_purchases_id'), table_name='purchases')
|
|
op.drop_table('purchases')
|
|
|
|
op.drop_index(op.f('ix_suppliers_name'), table_name='suppliers')
|
|
op.drop_index(op.f('ix_suppliers_id'), table_name='suppliers')
|
|
op.drop_table('suppliers')
|
|
|
|
op.drop_index(op.f('ix_inventory_transaction_items_id'), table_name='inventory_transaction_items')
|
|
op.drop_table('inventory_transaction_items')
|
|
|
|
op.drop_index(op.f('ix_inventory_items_id'), table_name='inventory_items')
|
|
op.drop_table('inventory_items')
|
|
|
|
op.drop_index(op.f('ix_inventory_transactions_id'), table_name='inventory_transactions')
|
|
op.drop_table('inventory_transactions')
|
|
|
|
op.drop_index(op.f('ix_locations_id'), table_name='locations')
|
|
op.drop_table('locations')
|
|
|
|
op.drop_index(op.f('ix_products_barcode'), table_name='products')
|
|
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')
|
|
|
|
# Drop enum types
|
|
op.execute("DROP TYPE locationtype")
|
|
op.execute("DROP TYPE transactiontype") |