from patch.include import *
from tqdm import tqdm


@app_context(commit=True)
def fix():
    """
    Для тестирования патча: ( cd /opt/eva-app; python3 -m patch.202403191705_migrate_root_parent )
    Здесь можно работать с моделями через models.CmfTask и т.д.
    Для прогрессбара используйте:
    for task in tqdm(models.CmfTask.list()):
        ...
    """
    print('Мигрируем root_parent в project')
    # 1. Делаем промежуточные коммиты
    # 2. Пытаемся не разбивать на кусочки апдейты по таблице, в теории апдейт должен пройти быстро
    # 3. Попадались root_parent = CmfProject:... по несуществующему проекту. Т.к. там не было FK это работало, сейчас падает
    #   поэтому их чистим отдельно
    # Вместо фикса п.3 и дополнительно для п.2 сделаем bulk_update per project, которые заселектим (иначе не сможем заюзать его)
    # На базе Б 7m33.606s

    for project in models.CmfProject.slist(include_deleted=True, fields=['id', 'name']):
        print(f'Мигрируем {project.id} {project.name}')
        for model in cmfutil.iter_models():
            if hasattr(model, 'project') and hasattr(model, 'root_parent'):
                print(f'Мигрируем {model}')
                model.bulk_update(values={'project_id': project.id},
                                  filter=[['root_parent', '==', project.id],
                                          ['OR', ['project_id', '==', None], ['project_id', '!=', project.id]]],
                                  include_deleted=True)
                commit_all_ds()
            # else:
            #     print(f'Пропускаем {model}')


#     for table_name in [
#         'cmf_active_entity_filter', 'cmf_s_desk_knowlage_category'
#     ]:
#         op.execute(
#             f'update {table_name} set root_parent_id=null where root_parent_id like \'CmfProject:%\' and root_parent_id not in (select id from cmf_project)')
#     for table_name in [
#         'cmf_answer_template', 'cmf_asset', 'cmf_attachment', 'cmf_chat_group', 'cmf_chat_topic', 'cmf_comment',
#         'cmf_company', 'cmf_component', 'cmf_dashboard', 'cmf_deal', 'cmf_document', 'cmf_folder',
#         'cmf_import', 'cmf_invoice', 'cmf_kanban_board', 'cmf_lead', 'cmf_list', 'cmf_mail_handler', 'cmf_payment',
#         'cmf_pipeline', 'cmf_project', 'cmf_purchase_order', 'cmf_purchase_order_items', 'cmf_roadmap', 'cmf_scheme_wf',
#         'cmf_stock_incoming_order', 'cmf_stock_initial', 'cmf_stock_inventory', 'cmf_stock_move',
#         'cmf_stock_outcoming_order', 'cmf_task', 'cmf_task_filter', 'cmf_task_queue', 'cmf_task_report',
#         'cmf_time_tracker_history', 'cmf_u_work_post',
#     ]:
#         op.execute(
#             f'update {table_name} set root_parent_id=null where root_parent_id like \'CmfProject:%\' and root_parent_id not in (select id from cmf_project)')
#     op.execute(
#         f'update cmf_full_search set obj_root_parent_id=null where obj_root_parent_id like \'CmfProject:%\' and obj_root_parent_id not in (select id from cmf_project)')
#
#     op.execute('update cmf_active_entity_filter set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.execute('update cmf_s_desk_knowlage_category set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
# op.execute('update cmf_answer_template set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_answer_template_project_id'), 'cmf_answer_template', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_answer_template', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_asset', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_asset set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_asset_project_id'), 'cmf_asset', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_asset', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_attachment', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_attachment set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_attachment_project_id'), 'cmf_attachment', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_attachment', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_chat_group', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_chat_group set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_chat_group_project_id'), 'cmf_chat_group', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_chat_group', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_chat_topic', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_chat_topic set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_chat_topic_project_id'), 'cmf_chat_topic', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_chat_topic', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_comment', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_comment set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_comment_project_id'), 'cmf_comment', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_comment', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_company', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_company set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_company_project_id'), 'cmf_company', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_company', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_component', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_component set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_component_project_id'), 'cmf_component', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_component', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_dashboard', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_dashboard set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_dashboard_project_id'), 'cmf_dashboard', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_dashboard', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_deal', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_deal set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_deal_project_id'), 'cmf_deal', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_deal', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_document', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_document set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_document_project_id'), 'cmf_document', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_document', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_folder', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_folder set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_folder_project_id'), 'cmf_folder', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_folder', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_full_search', sa.Column('obj_project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_full_search set obj_project_id=obj_root_parent_id where obj_root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_full_search_obj_project_id'), 'cmf_full_search', ['obj_project_id'], unique=False)
#     op.add_column('cmf_import', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_import set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_import_project_id'), 'cmf_import', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_import', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_invoice', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_invoice set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_invoice_project_id'), 'cmf_invoice', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_invoice', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_kanban_board', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_kanban_board set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_kanban_board_project_id'), 'cmf_kanban_board', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_kanban_board', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_lead', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_lead set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_lead_project_id'), 'cmf_lead', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_lead', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_list', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_list set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_list_project_id'), 'cmf_list', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_list', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_mail_handler', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_mail_handler set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_mail_handler_project_id'), 'cmf_mail_handler', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_mail_handler', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_payment', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_payment set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_payment_project_id'), 'cmf_payment', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_payment', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_pipeline', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_pipeline set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_pipeline_project_id'), 'cmf_pipeline', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_pipeline', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_project', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_project set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_project_project_id'), 'cmf_project', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_project', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_purchase_order', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_purchase_order set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_purchase_order_project_id'), 'cmf_purchase_order', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_purchase_order', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_purchase_order_items', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_purchase_order_items set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_purchase_order_items_project_id'), 'cmf_purchase_order_items', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_purchase_order_items', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_roadmap', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_roadmap set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_roadmap_project_id'), 'cmf_roadmap', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_roadmap', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_scheme_wf', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_scheme_wf set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_scheme_wf_project_id'), 'cmf_scheme_wf', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_scheme_wf', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_stock_incoming_order', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_stock_incoming_order set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_stock_incoming_order_project_id'), 'cmf_stock_incoming_order', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_stock_incoming_order', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_stock_initial', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_stock_initial set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_stock_initial_project_id'), 'cmf_stock_initial', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_stock_initial', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_stock_inventory', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_stock_inventory set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_stock_inventory_project_id'), 'cmf_stock_inventory', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_stock_inventory', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_stock_move', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_stock_move set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_stock_move_project_id'), 'cmf_stock_move', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_stock_move', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_stock_outcoming_order', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_stock_outcoming_order set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_stock_outcoming_order_project_id'), 'cmf_stock_outcoming_order', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_stock_outcoming_order', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_task', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_task set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_task_project_id'), 'cmf_task', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_task', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_task_filter', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_task_filter set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_task_filter_project_id'), 'cmf_task_filter', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_task_filter', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_task_queue', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_task_queue set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_task_queue_project_id'), 'cmf_task_queue', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_task_queue', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_task_report', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_task_report set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_task_report_project_id'), 'cmf_task_report', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_task_report', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_time_tracker_history', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_time_tracker_history set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')
#     op.create_index(op.f('ix_cmf_time_tracker_history_project_id'), 'cmf_time_tracker_history', ['project_id'], unique=False)
#     op.create_foreign_key(None, 'cmf_time_tracker_history', 'cmf_project', ['project_id'], ['id'])
#     op.add_column('cmf_u_work_post', sa.Column('project_id', sa.String(length=64), nullable=True))
#     op.execute('update cmf_u_work_post set project_id=root_parent_id where root_parent_id like \'CmfProject:%\'')



if __name__ == "__main__":
    fix()
