"""Support labels on transactions

Revision ID: 72f1ad512b2e
Revises: ecb7817db377
Create Date: 2021-09-02 22:59:46.408595

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "72f1ad512b2e"
down_revision = "ecb7817db377"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "ethereum_labels",
        sa.Column("transaction_hash", sa.VARCHAR(length=256), nullable=True),
    )
    op.alter_column(
        "ethereum_labels", "address_id", existing_type=sa.INTEGER(), nullable=True
    )
    op.create_index(
        op.f("ix_ethereum_labels_transaction_hash"),
        "ethereum_labels",
        ["transaction_hash"],
        unique=False,
    )
    op.create_foreign_key(
        op.f("fk_ethereum_labels_transaction_hash_ethereum_transactions"),
        "ethereum_labels",
        "ethereum_transactions",
        ["transaction_hash"],
        ["hash"],
        ondelete="CASCADE",
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(
        op.f("fk_ethereum_labels_transaction_hash_ethereum_transactions"),
        "ethereum_labels",
        type_="foreignkey",
    )
    op.drop_index(
        op.f("ix_ethereum_labels_transaction_hash"), table_name="ethereum_labels"
    )
    op.execute("DELETE FROM ethereum_labels WHERE address_id IS NULL;")
    op.alter_column(
        "ethereum_labels", "address_id", existing_type=sa.INTEGER(), nullable=False
    )
    op.drop_column("ethereum_labels", "transaction_hash")
    # ### end Alembic commands ###
