"""add scheduler related tables

Revision ID: 20d2e65b1fd2
Revises: fefa06dd93cc
Create Date: 2021-04-09 11:21:19.103241

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '20d2e65b1fd2'
down_revision = 'fefa06dd93cc'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('optimistic_lock_v2',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False, comment='id'),
    sa.Column('name', sa.String(length=255), nullable=False, comment='lock name'),
    sa.Column('version', sa.BIGINT(), nullable=False, comment='lock version'),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True, comment='created at'),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True, comment='updated at'),
    sa.Column('deleted_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True, comment='deleted at'),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('name', name='uniq_name'),
    comment='optimistic lock',
    mysql_charset='utf8mb4',
    mysql_engine='innodb'
    )
    op.create_table('scheduler_item_v2',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False, comment='id'),
    sa.Column('name', sa.String(length=255), nullable=False, comment='item name'),
    sa.Column('pipeline', sa.Text(), nullable=False, comment='pipeline'),
    sa.Column('status', sa.Integer(), nullable=False, comment='item status'),
    sa.Column('interval', sa.Integer(), nullable=False, comment='item run interval in second'),
    sa.Column('last_run_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True, comment='last runner time'),
    sa.Column('retry_cnt', sa.Integer(), nullable=False, comment='retry count when item is failed'),
    sa.Column('extra', sa.Text(), nullable=True, comment='extra info'),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True, comment='created at'),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True, comment='updated at'),
    sa.Column('deleted_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True, comment='deleted at'),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('name', name='uniq_name'),
    comment='scheduler items',
    mysql_charset='utf8mb4',
    mysql_engine='innodb'
    )
    op.create_table('scheduler_runner_v2',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False, comment='id'),
    sa.Column('item_id', sa.Integer(), nullable=False, comment='item id'),
    sa.Column('status', sa.Integer(), nullable=False, comment='runner status'),
    sa.Column('start_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True, comment='runner start time'),
    sa.Column('end_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True, comment='runner end time'),
    sa.Column('pipeline', sa.Text(), nullable=False, comment='pipeline from scheduler item'),
    sa.Column('output', sa.Text(), nullable=False, comment='output'),
    sa.Column('context', sa.Text(), nullable=False, comment='context'),
    sa.Column('extra', sa.Text(), nullable=True, comment='extra info'),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True, comment='created at'),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True, comment='updated at'),
    sa.Column('deleted_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True, comment='deleted at'),
    sa.PrimaryKeyConstraint('id'),
    comment='scheduler runners',
    mysql_charset='utf8mb4',
    mysql_engine='innodb'
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('scheduler_runner_v2')
    op.drop_table('scheduler_item_v2')
    op.drop_table('optimistic_lock_v2')
    # ### end Alembic commands ###
