"""empty message

Revision ID: 7d55a089b5bc
Revises: 64b682d53e23
Create Date: 2017-06-26 17:22:12.828934

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "7d55a089b5bc"
down_revision = "64b682d53e23"
branch_labels = None
depends_on = None


def upgrade():
    conn = op.get_bind()

    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "projects", sa.Column("task_creation_mode", sa.Integer(), nullable=True)
    )
    op.create_index(
        "idx_geometry", "projects", ["geometry"], unique=False, postgresql_using="gist"
    )
    op.add_column("tasks", sa.Column("extra_properties", sa.Unicode(), nullable=True))
    # ### end Alembic commands ###

    # Content migration: Check the amount of zoom levels in tasks of a project and set
    # task_creation_mode to 1 or 0 accordingly.
    projects = conn.execute("select * from projects")

    for project in projects:
        query = "select distinct zoom from tasks where project_id = " + str(project.id)
        zooms = conn.execute(query).fetchall()

        if len(zooms) == 1 and zooms[0] == (None,):
            query = "update projects set task_creation_mode = 1 where id = " + str(
                project.id
            )
            op.execute(query)


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column("tasks", "extra_properties")
    op.drop_index("idx_geometry", table_name="projects")
    op.drop_column("projects", "task_creation_mode")
    # ### end Alembic commands ###
