"""Add log_index, block_number , timestamp remove address_id and add address just as text field

Revision ID: f1e8cf50a3ff
Revises: d6ed4b1b43d5
Create Date: 2021-10-12 15:35:49.477610

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "f1e8cf50a3ff"
down_revision = "d6ed4b1b43d5"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###

    ###### insert into  table

    op.execute(
        """CREATE TABLE ethereum_labels_v2 AS
                SELECT
                    ethereum_labels.id as id,
                    ethereum_labels.label as label,
                    ethereum_labels.label_data as label_data,
                    ethereum_labels.created_at as created_at,
                    ethereum_labels.transaction_hash as transaction_hash,
                    ethereum_addresses.address as address
                FROM
                    ethereum_labels
                    left join ethereum_addresses ON ethereum_labels.address_id = ethereum_addresses.id;

                /* Set correct columns metadata columns TYPE | Nullable | Default  */
                ALTER TABLE
                    ethereum_labels_v2
                ALTER COLUMN
                    id
                SET
                    NOT NULL,
                ALTER COLUMN
                    label
                SET
                    NOT NULL,
                ALTER COLUMN
                    created_at TYPE timestamp with time zone,
                ALTER COLUMN
                    created_at
                SET
                    DEFAULT timezone('utc' :: text, statement_timestamp()),
                ALTER COLUMN
                    created_at
                SET
                    NOT NULL,
                ALTER COLUMN
                    transaction_hash TYPE character varying(256),
                ALTER COLUMN
                    address TYPE character varying(256);

                /* ADD new columns  */
                ALTER TABLE
                    ethereum_labels_v2
                ADD
                    log_index INTEGER,
                ADD
                    block_number bigint NULl,
                ADD
                    block_timestamp bigint NULL;
                
                """
    )

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute(
        """
        DROP TABLE ethereum_labels_v2;
    """
    )
