"""cache ptdata company-level contracts summary on companies table

Revision ID: 0009
Revises: 0008
Create Date: 2026-04-17

Stores the aggregate totals returned by ptdata's /v1/companies/{nif} endpoint.
That endpoint is reliable even when /v1/contracts?supplier=X 504s on large
suppliers (ptdata's supplier-index query is the bottleneck). We keep KPIs
accurate even when the paginated list fails to fetch.
"""
from alembic import op


revision = "0009"
down_revision = "0008"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.execute(
        "ALTER TABLE companies "
        "ADD COLUMN contracts_total INT, "
        "ADD COLUMN contracts_total_value NUMERIC(14,2), "
        "ADD COLUMN contracts_fetched_at TIMESTAMPTZ"
    )


def downgrade() -> None:
    op.execute(
        "ALTER TABLE companies "
        "DROP COLUMN contracts_total, "
        "DROP COLUMN contracts_total_value, "
        "DROP COLUMN contracts_fetched_at"
    )
