"""Add Event.ingested_at

Revision ID: 301eb03ce91c
Revises: 5cc3e60ba6a1
Create Date: 2025-04-03 14:19:39.447003

"""

import sqlalchemy as sa
from alembic import op

# Polar Custom Imports

# revision identifiers, used by Alembic.
revision = "301eb03ce91c"
down_revision = "5cc3e60ba6a1"
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "events", sa.Column("ingested_at", sa.TIMESTAMP(timezone=True), nullable=True)
    )

    op.execute(
        """
        UPDATE events
        set ingested_at = timestamp
        """
    )

    op.alter_column(
        "events",
        "ingested_at",
        existing_type=sa.TIMESTAMP(timezone=True),
        nullable=False,
    )

    op.create_index(
        op.f("ix_events_ingested_at"), "events", ["ingested_at"], unique=False
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_events_ingested_at"), table_name="events")
    op.drop_column("events", "ingested_at")
    # ### end Alembic commands ###
