"""Create daily_cache_logs table.

Revision ID: a7b74667f7d6
Revises: 
Create Date: 2017-10-27 11:21:21.058000

"""
from alembic import op
import logging
import sqlalchemy as sa

logger = logging.getLogger(__name__)

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

first_agent_version = '17.11.2'


def upgrade():
    connection = op.get_bind()
    inspector = sa.inspect(connection)
    tables = set(inspector.get_table_names())
    if 'daily_cache_logs' in tables:
        logger.info("Table 'daily_cache_logs' already exists. Skipping its migration...")
    else:
        op.create_table(
            'daily_cache_logs',
            sa.Column('id', sa.Integer(), nullable=False, autoincrement=True),
            sa.Column('datatype', sa.String(length=11), nullable=False),
            sa.Column('store_id', sa.String(length=64), nullable=True),
            sa.Column('date', sa.Date(), nullable=False),
            sa.Column('last_started', sa.DateTime(), nullable=True),
            sa.Column('last_completed', sa.DateTime(), nullable=True),
            sa.Column('error', sa.Boolean(), nullable=False),
            sa.PrimaryKeyConstraint(
                'id',
                has_autoincrement=True,
            ),
            sa.Index(
                'store_id_datatype_date_idx',
                'store_id',
                'datatype',
                'date',
            ),
        )


def downgrade():
    op.drop_table('daily_cache_logs')

