Create new services: inventory, recipes, suppliers
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
"""Add inventory product reference and remove redundant product model
|
||||
|
||||
Revision ID: 003
|
||||
Revises: 002
|
||||
Create Date: 2025-01-15 11:00:00.000000
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '003'
|
||||
down_revision = '002'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# Add inventory product reference to sales_data table
|
||||
op.add_column('sales_data', sa.Column('inventory_product_id',
|
||||
postgresql.UUID(as_uuid=True), nullable=True))
|
||||
|
||||
# Add product_type column for caching product type from inventory
|
||||
op.add_column('sales_data', sa.Column('product_type',
|
||||
sa.String(20), nullable=True))
|
||||
|
||||
# Create indexes for new columns
|
||||
op.create_index('idx_sales_inventory_product', 'sales_data',
|
||||
['inventory_product_id', 'tenant_id'])
|
||||
op.create_index('idx_sales_product_type', 'sales_data',
|
||||
['product_type', 'tenant_id', 'date'])
|
||||
|
||||
# Drop the redundant products table if it exists
|
||||
op.execute("DROP TABLE IF EXISTS products CASCADE;")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# Drop new indexes
|
||||
op.drop_index('idx_sales_product_type', table_name='sales_data')
|
||||
op.drop_index('idx_sales_inventory_product', table_name='sales_data')
|
||||
|
||||
# Remove new columns
|
||||
op.drop_column('sales_data', 'product_type')
|
||||
op.drop_column('sales_data', 'inventory_product_id')
|
||||
|
||||
# Recreate products table (basic version)
|
||||
op.create_table(
|
||||
'products',
|
||||
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('tenant_id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('name', sa.String(255), nullable=False),
|
||||
sa.Column('sku', sa.String(100), nullable=True),
|
||||
sa.Column('category', sa.String(100), nullable=True),
|
||||
sa.Column('description', sa.Text(), nullable=True),
|
||||
sa.Column('is_active', sa.Boolean(), default=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
|
||||
# Recreate basic indexes
|
||||
op.create_index('idx_products_tenant_name', 'products', ['tenant_id', 'name'], unique=True)
|
||||
op.create_index('idx_products_tenant_sku', 'products', ['tenant_id', 'sku'])
|
||||
@@ -0,0 +1,61 @@
|
||||
"""Remove cached product fields - use only inventory_product_id
|
||||
|
||||
Revision ID: 004
|
||||
Revises: 003
|
||||
Create Date: 2025-01-15 12:00:00.000000
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '004'
|
||||
down_revision = '003'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# Make inventory_product_id required (NOT NULL)
|
||||
op.alter_column('sales_data', 'inventory_product_id', nullable=False)
|
||||
|
||||
# Remove cached product fields - inventory service is single source of truth
|
||||
op.drop_column('sales_data', 'product_name')
|
||||
op.drop_column('sales_data', 'product_category')
|
||||
op.drop_column('sales_data', 'product_sku')
|
||||
op.drop_column('sales_data', 'product_type')
|
||||
|
||||
# Drop old indexes that referenced removed fields
|
||||
op.execute("DROP INDEX IF EXISTS idx_sales_tenant_product")
|
||||
op.execute("DROP INDEX IF EXISTS idx_sales_tenant_category")
|
||||
op.execute("DROP INDEX IF EXISTS idx_sales_product_date")
|
||||
op.execute("DROP INDEX IF EXISTS idx_sales_sku_date")
|
||||
op.execute("DROP INDEX IF EXISTS idx_sales_product_type")
|
||||
|
||||
# Create optimized indexes for inventory-only approach
|
||||
op.create_index('idx_sales_inventory_product_date', 'sales_data',
|
||||
['inventory_product_id', 'date', 'tenant_id'])
|
||||
op.create_index('idx_sales_tenant_inventory_product', 'sales_data',
|
||||
['tenant_id', 'inventory_product_id'])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# Drop new indexes
|
||||
op.drop_index('idx_sales_tenant_inventory_product', table_name='sales_data')
|
||||
op.drop_index('idx_sales_inventory_product_date', table_name='sales_data')
|
||||
|
||||
# Add back cached product fields for downgrade compatibility
|
||||
op.add_column('sales_data', sa.Column('product_name', sa.String(255), nullable=True))
|
||||
op.add_column('sales_data', sa.Column('product_category', sa.String(100), nullable=True))
|
||||
op.add_column('sales_data', sa.Column('product_sku', sa.String(100), nullable=True))
|
||||
op.add_column('sales_data', sa.Column('product_type', sa.String(20), nullable=True))
|
||||
|
||||
# Make inventory_product_id optional again
|
||||
op.alter_column('sales_data', 'inventory_product_id', nullable=True)
|
||||
|
||||
# Recreate old indexes
|
||||
op.create_index('idx_sales_tenant_product', 'sales_data', ['tenant_id', 'product_name'])
|
||||
op.create_index('idx_sales_tenant_category', 'sales_data', ['tenant_id', 'product_category'])
|
||||
op.create_index('idx_sales_product_date', 'sales_data', ['product_name', 'date', 'tenant_id'])
|
||||
op.create_index('idx_sales_sku_date', 'sales_data', ['product_sku', 'date', 'tenant_id'])
|
||||
Reference in New Issue
Block a user