"""Add opensea state table and add index by label_data ->> name

Revision ID: ecb7817db377
Revises: ea8185bd24c7
Create Date: 2021-08-31 17:44:24.139028

"""
from alembic import op
import sqlalchemy as sa


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


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "opensea_crawler_state",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("query", sa.Text(), nullable=False),
        sa.Column(
            "crawled_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("TIMEZONE('utc', statement_timestamp())"),
            nullable=False,
        ),
        sa.Column("total_count", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_opensea_crawler_state")),
        sa.UniqueConstraint("id", name=op.f("uq_opensea_crawler_state_id")),
    )
    op.execute(
        "ALTER TABLE ethereum_labels DROP CONSTRAINT IF EXISTS uq_ethereum_labels_label"
    )

    op.execute(
        f"CREATE INDEX idx_ethereum_labels_opensea_nft_name ON ethereum_labels((label_data->>'name')) where label='opensea_nft';"
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("opensea_crawler_state")
    op.drop_index("idx_ethereum_labels_opensea_nft_name")
    # ### end Alembic commands ###
