"""Add sepolia

Revision ID: a1ead76c0470
Revises: f2c6aa92e5d2
Create Date: 2024-07-22 14:07:22.992523

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "sepolia_blocks",
        sa.Column("block_number", sa.BigInteger(), nullable=False),
        sa.Column("block_hash", sa.VARCHAR(length=256), nullable=False),
        sa.Column("block_timestamp", sa.BigInteger(), nullable=False),
        sa.Column("parent_hash", sa.VARCHAR(length=256), nullable=False),
        sa.Column("row_id", sa.BigInteger(), nullable=False),
        sa.Column("path", sa.Text(), nullable=False),
        sa.Column(
            "indexed_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("TIMEZONE('utc', statement_timestamp())"),
            nullable=False,
        ),
        sa.PrimaryKeyConstraint("block_number", name=op.f("pk_sepolia_blocks")),
    )
    op.create_index(
        op.f("ix_sepolia_blocks_block_number"),
        "sepolia_blocks",
        ["block_number"],
        unique=False,
    )
    op.create_index(
        op.f("ix_sepolia_blocks_block_timestamp"),
        "sepolia_blocks",
        ["block_timestamp"],
        unique=False,
    )
    op.create_table(
        "sepolia_reorgs",
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("block_number", sa.BigInteger(), nullable=False),
        sa.Column("block_hash", sa.VARCHAR(length=256), nullable=False),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_sepolia_reorgs")),
    )
    op.create_index(
        op.f("ix_sepolia_reorgs_block_hash"),
        "sepolia_reorgs",
        ["block_hash"],
        unique=False,
    )
    op.create_index(
        op.f("ix_sepolia_reorgs_block_number"),
        "sepolia_reorgs",
        ["block_number"],
        unique=False,
    )
    op.create_table(
        "sepolia_transactions",
        sa.Column("block_number", sa.BigInteger(), nullable=False),
        sa.Column("hash", sa.VARCHAR(length=256), nullable=False),
        sa.Column("from_address", sa.LargeBinary(length=20), nullable=False),
        sa.Column("to_address", sa.LargeBinary(length=20), nullable=False),
        sa.Column("selector", sa.VARCHAR(length=256), nullable=True),
        sa.Column("type", sa.Integer(), nullable=True),
        sa.Column("row_id", sa.BigInteger(), nullable=False),
        sa.Column("block_hash", sa.VARCHAR(length=256), nullable=False),
        sa.Column("index", sa.BigInteger(), nullable=False),
        sa.Column("path", sa.Text(), nullable=False),
        sa.Column(
            "indexed_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("TIMEZONE('utc', statement_timestamp())"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["block_number"],
            ["sepolia_blocks.block_number"],
            name=op.f("fk_sepolia_transactions_block_number_sepolia_blocks"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("hash", name=op.f("pk_sepolia_transactions")),
    )
    op.create_index(
        op.f("ix_sepolia_transactions_block_hash"),
        "sepolia_transactions",
        ["block_hash"],
        unique=False,
    )
    op.create_index(
        op.f("ix_sepolia_transactions_block_number"),
        "sepolia_transactions",
        ["block_number"],
        unique=False,
    )
    op.create_index(
        op.f("ix_sepolia_transactions_from_address"),
        "sepolia_transactions",
        ["from_address"],
        unique=False,
    )
    op.create_index(
        op.f("ix_sepolia_transactions_hash"),
        "sepolia_transactions",
        ["hash"],
        unique=True,
    )
    op.create_index(
        op.f("ix_sepolia_transactions_index"),
        "sepolia_transactions",
        ["index"],
        unique=False,
    )
    op.create_index(
        op.f("ix_sepolia_transactions_selector"),
        "sepolia_transactions",
        ["selector"],
        unique=False,
    )
    op.create_index(
        op.f("ix_sepolia_transactions_to_address"),
        "sepolia_transactions",
        ["to_address"],
        unique=False,
    )
    op.create_index(
        op.f("ix_sepolia_transactions_type"),
        "sepolia_transactions",
        ["type"],
        unique=False,
    )
    op.create_table(
        "sepolia_logs",
        sa.Column("transaction_hash", sa.VARCHAR(length=256), nullable=False),
        sa.Column("block_hash", sa.VARCHAR(length=256), nullable=False),
        sa.Column("address", sa.LargeBinary(length=20), nullable=False),
        sa.Column("row_id", sa.BigInteger(), nullable=False),
        sa.Column("selector", sa.VARCHAR(length=256), nullable=True),
        sa.Column("topic1", sa.VARCHAR(length=256), nullable=True),
        sa.Column("topic2", sa.VARCHAR(length=256), nullable=True),
        sa.Column("topic3", sa.VARCHAR(length=256), nullable=True),
        sa.Column("log_index", sa.BigInteger(), nullable=False),
        sa.Column("path", sa.Text(), nullable=False),
        sa.Column(
            "indexed_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("TIMEZONE('utc', statement_timestamp())"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["transaction_hash"],
            ["sepolia_transactions.hash"],
            name=op.f("fk_sepolia_logs_transaction_hash_sepolia_transactions"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint(
            "transaction_hash", "log_index", name="pk_sepolia_log_index"
        ),
        sa.UniqueConstraint(
            "transaction_hash",
            "log_index",
            name="uq_sepolia_log_index_transaction_hash_log_index",
        ),
    )
    op.create_index(
        "idx_sepolia_logs_address_selector",
        "sepolia_logs",
        ["address", "selector"],
        unique=False,
    )
    op.create_index(
        "idx_sepolia_logs_block_hash_log_index",
        "sepolia_logs",
        ["block_hash", "log_index"],
        unique=True,
    )
    op.create_index(
        op.f("ix_sepolia_logs_address"),
        "sepolia_logs",
        ["address"],
        unique=False,
    )
    op.create_index(
        op.f("ix_sepolia_logs_block_hash"),
        "sepolia_logs",
        ["block_hash"],
        unique=False,
    )
    op.create_index(
        op.f("ix_sepolia_logs_transaction_hash"),
        "sepolia_logs",
        ["transaction_hash"],
        unique=False,
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(
        op.f("ix_sepolia_logs_transaction_hash"),
        table_name="sepolia_logs",
    )
    op.drop_index(op.f("ix_sepolia_logs_block_hash"), table_name="sepolia_logs")
    op.drop_index(op.f("ix_sepolia_logs_address"), table_name="sepolia_logs")
    op.drop_index(
        "idx_sepolia_logs_block_hash_log_index",
        table_name="sepolia_logs",
    )
    op.drop_index("idx_sepolia_logs_address_selector", table_name="sepolia_logs")
    op.drop_table("sepolia_logs")
    op.drop_index(
        op.f("ix_sepolia_transactions_type"),
        table_name="sepolia_transactions",
    )
    op.drop_index(
        op.f("ix_sepolia_transactions_to_address"),
        table_name="sepolia_transactions",
    )
    op.drop_index(
        op.f("ix_sepolia_transactions_selector"),
        table_name="sepolia_transactions",
    )
    op.drop_index(
        op.f("ix_sepolia_transactions_index"),
        table_name="sepolia_transactions",
    )
    op.drop_index(
        op.f("ix_sepolia_transactions_hash"),
        table_name="sepolia_transactions",
    )
    op.drop_index(
        op.f("ix_sepolia_transactions_from_address"),
        table_name="sepolia_transactions",
    )
    op.drop_index(
        op.f("ix_sepolia_transactions_block_number"),
        table_name="sepolia_transactions",
    )
    op.drop_index(
        op.f("ix_sepolia_transactions_block_hash"),
        table_name="sepolia_transactions",
    )
    op.drop_table("sepolia_transactions")
    op.drop_index(
        op.f("ix_sepolia_reorgs_block_number"),
        table_name="sepolia_reorgs",
    )
    op.drop_index(
        op.f("ix_sepolia_reorgs_block_hash"),
        table_name="sepolia_reorgs",
    )
    op.drop_table("sepolia_reorgs")
    op.drop_index(
        op.f("ix_sepolia_blocks_block_timestamp"),
        table_name="sepolia_blocks",
    )
    op.drop_index(
        op.f("ix_sepolia_blocks_block_number"),
        table_name="sepolia_blocks",
    )
    op.drop_table("sepolia_blocks")
    # ### end Alembic commands ###
