"""remove_dependencies_field

Revision ID: 0280
Revises: 0279
Create Date: 2019-02-19 22:34:16.800414

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '0280'
down_revision = '0279'
branch_labels = None
depends_on = None


def upgrade():
    # Delete problematic card_attributes rows before updating
    op.execute("""
        DELETE FROM card_attributes
        WHERE type_name='relationships'
          AND (value = 'null'::jsonb
               OR jsonb_array_length(value) = 0
               OR value IS NULL)
    """)

    op.execute("""
        UPDATE card_attributes
        SET value = (
            SELECT jsonb_agg(jsonb_build_array(elem::integer, 'none', 'end_start'))
            FROM jsonb_array_elements_text(value) AS elem
        )
        WHERE type_name='relationships'
    """)

    op.execute("""
        UPDATE card_attributes
        SET type_name = 'relationships'
        WHERE type_name='dependencies'
    """)

    op.execute("""
        UPDATE attributes
        SET type_name = 'relationships'
        WHERE type_name='dependencies'
    """)


def downgrade():
    pass
