"""use partition topic_name and number in wal file offsets

Revision ID: d77fb56536a8
Revises: 5cc5497d09b5
Create Date: 2025-07-09 14:47:00.591448

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "d77fb56536a8"
down_revision: Union[str, None] = "5cc5497d09b5"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "wal_file_offsets", sa.Column("topic_name", sa.String(), nullable=False)
    )
    op.add_column(
        "wal_file_offsets", sa.Column("partition_number", sa.Integer(), nullable=False)
    )
    op.create_index(
        "ix_wal_file_offset_topic_partition",
        "wal_file_offsets",
        ["topic_name", "partition_number"],
        unique=False,
    )
    op.drop_constraint(
        op.f("wal_file_offsets_partition_id_fkey"),
        "wal_file_offsets",
        type_="foreignkey",
    )
    op.create_foreign_key(
        None,
        "wal_file_offsets",
        "partitions",
        ["topic_name", "partition_number"],
        ["topic_name", "partition_number"],
        ondelete="CASCADE",
    )
    op.drop_column("wal_file_offsets", "partition_id")
    op.add_column("wal_files", sa.Column("uri", sa.Text(), nullable=False))
    op.add_column("wal_files", sa.Column("etag", sa.Text(), nullable=True))
    op.drop_column("wal_files", "iceberg_commit_id")
    op.drop_column("wal_files", "s3_etag")
    op.drop_column("wal_files", "s3_uri")
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "wal_files", sa.Column("s3_uri", sa.TEXT(), autoincrement=False, nullable=False)
    )
    op.add_column(
        "wal_files", sa.Column("s3_etag", sa.TEXT(), autoincrement=False, nullable=True)
    )
    op.add_column(
        "wal_files",
        sa.Column("iceberg_commit_id", sa.TEXT(), autoincrement=False, nullable=True),
    )
    op.drop_column("wal_files", "etag")
    op.drop_column("wal_files", "uri")
    op.add_column(
        "wal_file_offsets",
        sa.Column("partition_id", sa.INTEGER(), autoincrement=False, nullable=False),
    )
    op.drop_constraint(None, "wal_file_offsets", type_="foreignkey")
    op.create_foreign_key(
        op.f("wal_file_offsets_partition_id_fkey"),
        "wal_file_offsets",
        "partitions",
        ["partition_id"],
        ["id"],
        ondelete="CASCADE",
    )
    op.drop_index("ix_wal_file_offset_topic_partition", table_name="wal_file_offsets")
    op.drop_column("wal_file_offsets", "partition_number")
    op.drop_column("wal_file_offsets", "topic_name")
    # ### end Alembic commands ###
