Fix DB issue 2s

This commit is contained in:
Urtzi Alfaro
2025-09-30 21:58:10 +02:00
parent 147893015e
commit 7cc4b957a5
77 changed files with 4385 additions and 1211 deletions

View File

@@ -1,4 +1,10 @@
# services/sales/Dockerfile
# Sales Dockerfile
# Add this stage at the top of each service Dockerfile
FROM python:3.11-slim AS shared
WORKDIR /shared
COPY shared/ /shared/
# Then your main service stage
FROM python:3.11-slim
WORKDIR /app
@@ -7,34 +13,34 @@ WORKDIR /app
RUN apt-get update && apt-get install -y \
gcc \
g++ \
curl \
&& rm -rf /var/lib/apt/lists/*
# Copy requirements and install Python dependencies
# Copy requirements
COPY services/sales/requirements.txt .
# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy shared modules first
COPY shared/ /app/shared/
# Copy shared libraries from the shared stage
COPY --from=shared /shared /app/shared
# Copy application code
COPY services/sales/app/ /app/app/
# Copy migrations and alembic config
COPY services/sales/migrations/ /app/migrations/
COPY services/sales/alembic.ini /app/alembic.ini
COPY services/sales/ .
# Copy scripts directory
COPY scripts/ /app/scripts/
# Set Python path to include shared modules
ENV PYTHONPATH=/app
# Add shared libraries to Python path
ENV PYTHONPATH="/app:/app/shared:${PYTHONPATH:-}"
# Expose port
EXPOSE 8000
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
CMD python -c "import requests; requests.get('http://localhost:8000/health', timeout=5)" || exit 1
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8000/health || exit 1
# Run the application
CMD ["python", "-m", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
# Run application
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

View File

@@ -16,7 +16,7 @@ from app.api.import_data import router as import_router
class SalesService(StandardFastAPIService):
"""Sales Service with standardized setup"""
expected_migration_version = "001_initial_sales"
expected_migration_version = "00001"
async def on_startup(self, app):
"""Custom startup logic including migration verification"""

View File

@@ -0,0 +1,75 @@
"""Initial schema for sales service
Revision ID: 00001
Revises:
Create Date: 2025-09-30 18:00:00.0000
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = '00001'
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
op.create_table('sales_transactions',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('order_id', sa.UUID(), nullable=True),
sa.Column('customer_id', sa.UUID(), nullable=True),
sa.Column('transaction_type', sa.String(50), nullable=False),
sa.Column('payment_method', sa.String(50), nullable=True),
sa.Column('total_amount', sa.Float(), nullable=False),
sa.Column('tax_amount', sa.Float(), nullable=True),
sa.Column('discount_amount', sa.Float(), nullable=True),
sa.Column('currency', sa.String(3), nullable=True),
sa.Column('status', sa.String(50), nullable=True),
sa.Column('transaction_date', sa.DateTime(), nullable=False),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_sales_transactions_tenant_id'), 'sales_transactions', ['tenant_id'], unique=False)
op.create_index(op.f('ix_sales_transactions_order_id'), 'sales_transactions', ['order_id'], unique=False)
op.create_index(op.f('ix_sales_transactions_customer_id'), 'sales_transactions', ['customer_id'], unique=False)
op.create_index(op.f('ix_sales_transactions_transaction_type'), 'sales_transactions', ['transaction_type'], unique=False)
op.create_index(op.f('ix_sales_transactions_status'), 'sales_transactions', ['status'], unique=False)
op.create_index(op.f('ix_sales_transactions_transaction_date'), 'sales_transactions', ['transaction_date'], unique=False)
op.create_table('sales_reports',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('tenant_id', sa.UUID(), nullable=False),
sa.Column('report_type', sa.String(100), nullable=False),
sa.Column('report_date', sa.Date(), nullable=False),
sa.Column('period_start', sa.Date(), nullable=False),
sa.Column('period_end', sa.Date(), nullable=False),
sa.Column('total_sales', sa.Float(), nullable=False),
sa.Column('total_transactions', sa.Integer(), nullable=False),
sa.Column('average_transaction_value', sa.Float(), nullable=True),
sa.Column('top_products', postgresql.JSON(astext_type=sa.Text()), nullable=True),
sa.Column('metrics', postgresql.JSON(astext_type=sa.Text()), nullable=True),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_sales_reports_tenant_id'), 'sales_reports', ['tenant_id'], unique=False)
op.create_index(op.f('ix_sales_reports_report_type'), 'sales_reports', ['report_type'], unique=False)
op.create_index(op.f('ix_sales_reports_report_date'), 'sales_reports', ['report_date'], unique=False)
def downgrade() -> None:
op.drop_index(op.f('ix_sales_reports_report_date'), table_name='sales_reports')
op.drop_index(op.f('ix_sales_reports_report_type'), table_name='sales_reports')
op.drop_index(op.f('ix_sales_reports_tenant_id'), table_name='sales_reports')
op.drop_table('sales_reports')
op.drop_index(op.f('ix_sales_transactions_transaction_date'), table_name='sales_transactions')
op.drop_index(op.f('ix_sales_transactions_status'), table_name='sales_transactions')
op.drop_index(op.f('ix_sales_transactions_transaction_type'), table_name='sales_transactions')
op.drop_index(op.f('ix_sales_transactions_customer_id'), table_name='sales_transactions')
op.drop_index(op.f('ix_sales_transactions_order_id'), table_name='sales_transactions')
op.drop_index(op.f('ix_sales_transactions_tenant_id'), table_name='sales_transactions')
op.drop_table('sales_transactions')

View File

@@ -1 +0,0 @@
/Users/urtzialfaro/Documents/bakery-ia/shared