"""empty message

Revision ID: a729a66b91f1
Revises: 15d726bb1cf2
Create Date: 2022-12-06 16:14:18.806785

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'a729a66b91f1'
down_revision = '15d726bb1cf2'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    # т.к. не было вовремя миграции с drop index *_ext_id, часть БД уже без него.
    # op.drop_index('ix_cmf_access_list_ext_id', table_name='cmf_access_list')
    op.execute('DROP INDEX IF EXISTS ix_cmf_access_list_ext_id')
    # op.drop_index('ix_cmf_access_rule_ext_id', table_name='cmf_access_rule')
    op.execute('DROP INDEX IF EXISTS ix_cmf_access_rule_ext_id')
    op.alter_column('cmf_access_rule_subjects', 'id',
               existing_type=sa.VARCHAR(length=64),
               type_=sa.String(length=128),
               existing_nullable=False)
    op.alter_column('cmf_auth', 'login',
               existing_type=sa.VARCHAR(length=64),
               type_=sa.String(length=128),
               existing_nullable=True)
    op.alter_column('cmf_auth', 'email',
               existing_type=sa.VARCHAR(length=64),
               type_=sa.String(length=128),
               existing_nullable=True)
    # op.drop_index('ix_cmf_auth_ldap_plugin_ext_id', table_name='cmf_auth_ldap_plugin')
    op.execute('DROP INDEX IF EXISTS ix_cmf_auth_ldap_plugin_ext_id')
    op.alter_column('cmf_comment', 'cache_cmf_author_login',
               existing_type=sa.VARCHAR(length=64),
               type_=sa.String(length=128),
               existing_nullable=True)
    # op.drop_index('ix_cmf_comment_ext_id', table_name='cmf_comment')
    op.execute('DROP INDEX IF EXISTS ix_cmf_comment_ext_id')
    # op.drop_index('ix_cmf_document_ext_id', table_name='cmf_document')
    op.execute('DROP INDEX IF EXISTS ix_cmf_document_ext_id')
    # op.drop_index('ix_cmf_document_history_ext_id', table_name='cmf_document_history')
    op.execute('DROP INDEX IF EXISTS ix_cmf_document_history_ext_id')
    op.alter_column('cmf_document_history_cmf_sign', 'id',
               existing_type=sa.VARCHAR(length=64),
               type_=sa.String(length=128),
               existing_nullable=False)
    # op.drop_index('ix_cmf_filter_ext_id', table_name='cmf_filter')
    op.execute('DROP INDEX IF EXISTS ix_cmf_filter_ext_id')
    # op.drop_index('ix_cmf_global_settings_ext_id', table_name='cmf_global_settings')
    op.execute('DROP INDEX IF EXISTS ix_cmf_global_settings_ext_id')
    # op.drop_index('ix_cmf_import_ext_id', table_name='cmf_import')
    op.execute('DROP INDEX IF EXISTS ix_cmf_import_ext_id')
    # op.drop_index('ix_cmf_link_ext_id', table_name='cmf_link')
    op.execute('DROP INDEX IF EXISTS ix_cmf_link_ext_id')
    # op.drop_index('ix_cmf_notify_ext_id', table_name='cmf_notify')
    op.execute('DROP INDEX IF EXISTS ix_cmf_notify_ext_id')
    op.add_column('cmf_person', sa.Column('api_token_hash', sa.String(length=64), nullable=True))
    op.add_column('cmf_person', sa.Column('webhook_token_hash', sa.String(length=64), nullable=True))
    op.alter_column('cmf_person', 'login',
               existing_type=sa.VARCHAR(length=64),
               type_=sa.String(length=128),
               existing_nullable=True)
    op.alter_column('cmf_person', 'deleted_login',
               existing_type=sa.VARCHAR(length=64),
               type_=sa.String(length=128),
               existing_nullable=True)
    # op.drop_index('ix_cmf_person_ext_id', table_name='cmf_person')
    op.execute('DROP INDEX IF EXISTS ix_cmf_person_ext_id')
    op.create_index(op.f('ix_cmf_person_api_token_hash'), 'cmf_person', ['api_token_hash'], unique=False)
    # op.create_index(op.f('ix_cmf_person_does_not_work'), 'cmf_person', ['does_not_work'], unique=False)
    op.execute('CREATE INDEX IF NOT EXISTS ix_cmf_person_does_not_work ON cmf_person (does_not_work)')
    # op.create_index(op.f('ix_cmf_person_user_local'), 'cmf_person', ['user_local'], unique=False)
    op.execute('CREATE INDEX IF NOT EXISTS ix_cmf_person_user_local ON cmf_person (user_local)')
    op.create_index(op.f('ix_cmf_person_webhook_token_hash'), 'cmf_person', ['webhook_token_hash'], unique=False)
    # op.drop_index('ix_cmf_person_group_ext_id', table_name='cmf_person_group')
    op.execute('DROP INDEX IF EXISTS ix_cmf_person_group_ext_id')
    op.alter_column('cmf_person_group_rg_members', 'id',
               existing_type=sa.VARCHAR(length=64),
               type_=sa.String(length=128),
               existing_nullable=False)
    # op.drop_index('ix_cmf_person_var_ext_id', table_name='cmf_person_var')
    op.execute('DROP INDEX IF EXISTS ix_cmf_person_var_ext_id')
    op.alter_column('cmf_person_var_favorites', 'id',
               existing_type=sa.VARCHAR(length=64),
               type_=sa.String(length=128),
               existing_nullable=False)
    # op.drop_index('ix_cmf_plugin_ext_id', table_name='cmf_plugin')
    op.execute('DROP INDEX IF EXISTS ix_cmf_plugin_ext_id')
    # op.add_column('cmf_task', sa.Column('cache_child_tasks_count', sa.Integer(), nullable=False))
    op.execute('ALTER TABLE cmf_task ADD COLUMN IF NOT EXISTS cache_child_tasks_count INTEGER NOT NULL')
    # op.add_column('cmf_task', sa.Column('cache_branch_gantt_path', sa.TEXT(), nullable=True))
    op.execute('ALTER TABLE cmf_task ADD COLUMN IF NOT EXISTS cache_branch_gantt_path TEXT')
    # op.add_column('cmf_task', sa.Column('has_child_tasks', sa.Boolean(), nullable=True))
    op.execute('ALTER TABLE cmf_task ADD COLUMN IF NOT EXISTS has_child_tasks BOOLEAN')
    # op.add_column('cmf_task', sa.Column('epic_id', sa.String(length=64), nullable=True))
    op.execute('ALTER TABLE cmf_task ADD COLUMN IF NOT EXISTS epic_id VARCHAR(64)')
    # op.add_column('cmf_task', sa.Column('subproject_id', sa.String(length=64), nullable=True))
    op.execute('ALTER TABLE cmf_task ADD COLUMN IF NOT EXISTS subproject_id VARCHAR(64)')
    # op.drop_index('ix_cmf_task_ext_id', table_name='cmf_task')
    op.execute('DROP INDEX IF EXISTS ix_cmf_task_ext_id')
    # op.create_index(op.f('ix_cmf_task_epic_id'), 'cmf_task', ['epic_id'], unique=False)
    op.execute('CREATE INDEX IF NOT EXISTS ix_cmf_task_epic_id ON cmf_task (epic_id)')
    # op.create_index(op.f('ix_cmf_task_subproject_id'), 'cmf_task', ['subproject_id'], unique=False)
    op.execute('CREATE INDEX IF NOT EXISTS ix_cmf_task_subproject_id ON cmf_task (subproject_id)')
    op.create_foreign_key(None, 'cmf_task', 'cmf_task', ['subproject_id'], ['id'])
    op.create_foreign_key(None, 'cmf_task', 'cmf_task', ['epic_id'], ['id'])
    op.alter_column('cmf_task_cmf_task', 'id',
               existing_type=sa.VARCHAR(length=64),
               type_=sa.String(length=128),
               existing_nullable=False)
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('cmf_task_cmf_task', 'id',
               existing_type=sa.String(length=128),
               type_=sa.VARCHAR(length=64),
               existing_nullable=False)
    op.drop_constraint(None, 'cmf_task', type_='foreignkey')
    op.drop_constraint(None, 'cmf_task', type_='foreignkey')
    op.drop_index(op.f('ix_cmf_task_subproject_id'), table_name='cmf_task')
    op.drop_index(op.f('ix_cmf_task_epic_id'), table_name='cmf_task')
    op.create_index('ix_cmf_task_ext_id', 'cmf_task', ['ext_id'], unique=False)
    op.drop_column('cmf_task', 'subproject_id')
    op.drop_column('cmf_task', 'epic_id')
    op.drop_column('cmf_task', 'has_child_tasks')
    op.drop_column('cmf_task', 'cache_branch_gantt_path')
    op.drop_column('cmf_task', 'cache_child_tasks_count')
    op.create_index('ix_cmf_plugin_ext_id', 'cmf_plugin', ['ext_id'], unique=False)
    op.alter_column('cmf_person_var_favorites', 'id',
               existing_type=sa.String(length=128),
               type_=sa.VARCHAR(length=64),
               existing_nullable=False)
    op.create_index('ix_cmf_person_var_ext_id', 'cmf_person_var', ['ext_id'], unique=False)
    op.alter_column('cmf_person_group_rg_members', 'id',
               existing_type=sa.String(length=128),
               type_=sa.VARCHAR(length=64),
               existing_nullable=False)
    op.create_index('ix_cmf_person_group_ext_id', 'cmf_person_group', ['ext_id'], unique=False)
    op.drop_index(op.f('ix_cmf_person_webhook_token_hash'), table_name='cmf_person')
    op.drop_index(op.f('ix_cmf_person_user_local'), table_name='cmf_person')
    op.drop_index(op.f('ix_cmf_person_does_not_work'), table_name='cmf_person')
    op.drop_index(op.f('ix_cmf_person_api_token_hash'), table_name='cmf_person')
    op.create_index('ix_cmf_person_ext_id', 'cmf_person', ['ext_id'], unique=False)
    op.alter_column('cmf_person', 'deleted_login',
               existing_type=sa.String(length=128),
               type_=sa.VARCHAR(length=64),
               existing_nullable=True)
    op.alter_column('cmf_person', 'login',
               existing_type=sa.String(length=128),
               type_=sa.VARCHAR(length=64),
               existing_nullable=True)
    op.drop_column('cmf_person', 'webhook_token_hash')
    op.drop_column('cmf_person', 'api_token_hash')
    op.create_index('ix_cmf_notify_ext_id', 'cmf_notify', ['ext_id'], unique=False)
    op.create_index('ix_cmf_link_ext_id', 'cmf_link', ['ext_id'], unique=False)
    op.create_index('ix_cmf_import_ext_id', 'cmf_import', ['ext_id'], unique=False)
    op.create_index('ix_cmf_global_settings_ext_id', 'cmf_global_settings', ['ext_id'], unique=False)
    op.create_index('ix_cmf_filter_ext_id', 'cmf_filter', ['ext_id'], unique=False)
    op.alter_column('cmf_document_history_cmf_sign', 'id',
               existing_type=sa.String(length=128),
               type_=sa.VARCHAR(length=64),
               existing_nullable=False)
    op.create_index('ix_cmf_document_history_ext_id', 'cmf_document_history', ['ext_id'], unique=False)
    op.create_index('ix_cmf_document_ext_id', 'cmf_document', ['ext_id'], unique=False)
    op.create_index('ix_cmf_comment_ext_id', 'cmf_comment', ['ext_id'], unique=False)
    op.alter_column('cmf_comment', 'cache_cmf_author_login',
               existing_type=sa.String(length=128),
               type_=sa.VARCHAR(length=64),
               existing_nullable=True)
    op.create_index('ix_cmf_auth_ldap_plugin_ext_id', 'cmf_auth_ldap_plugin', ['ext_id'], unique=False)
    op.alter_column('cmf_auth', 'email',
               existing_type=sa.String(length=128),
               type_=sa.VARCHAR(length=64),
               existing_nullable=True)
    op.alter_column('cmf_auth', 'login',
               existing_type=sa.String(length=128),
               type_=sa.VARCHAR(length=64),
               existing_nullable=True)
    op.alter_column('cmf_access_rule_subjects', 'id',
               existing_type=sa.String(length=128),
               type_=sa.VARCHAR(length=64),
               existing_nullable=False)
    op.create_index('ix_cmf_access_rule_ext_id', 'cmf_access_rule', ['ext_id'], unique=False)
    op.create_index('ix_cmf_access_list_ext_id', 'cmf_access_list', ['ext_id'], unique=False)
    # ### end Alembic commands ###
