from patch.include import *
from tqdm import tqdm


@app_context(commit=True)
def fix():
    print('В сборке могли быть не все патчи, исправляем данные задним числом')
    last_release_patch_list = [
        '202109291551_rename_users_admins',
        '202109291552_user_local_check',
        '202110071152_priority2int',
        '202110081636_clear_all_recents',
        '202110111715_fix_comments_views',
        '202110111852_recalc_cards',
        '202110281709_recalc_list_status',
        '202111091508_chat_messages',
        '202111151454_fix_field_next_alarm',
        '202111221011_project_tree_set_parent',
        '202111251249_clean_home_projects',
        '202111291105_cmfcompany_is_internal',
        '202201241238_fix_update_bad_triggers',
        '202202211342_rebase_users_avatars',
        '202202211730_set_is_template',
        '202203231236_status_deduplicate',
        '202204041351_approve_for',
        '202204041842_task_workflow',
        '202204060913_delete_tasks_shadow_links',
        '202204180854_local_mailbox',
        '202204181014_local_sms_gate',
        '202205041515_fix_anonymous_code2',
        '202205220115_tree_node_is_branch',
        '202205230433_tree_parent_set',
        '202205251800_rename_users',
        '202205271731_refactoring_workglow_1-2',
        '202206150100_set_workflow',
        '202206151027_wiki_fix',
        '202206171835_set_menu_item',
        '202206200501_support_group',
        '202206211234_scaffold',
        '202207040951_anonymous_system',
        '202207121030_init_global_acl',
        '202207131730_acl_calc_inherit_parent',
        '202207141504_change_status_in_progress_start',
        '202208041035_backlog_to_folder',
        '202208041338_new_status_and_tag_colors',
        '202208041410_guest_and_sharelink_groups',
        '202208041830_license',
        '202208072049_logic_type',
        '202208101200_is_public_migration',
        '202208121820_fix_next_alarm_agile',
        '202208162031_set_main_list',
        '202208191057_acl_fix',
        '202208221046_init_text_draft_field',
        '202208230940_acl_fix2',
        '202208301750_recalc_cache_acl_fields',
        '202208310942_init_global_var',
        '202209130907_ldap',
        '202209221100_recalc_person_var_owner',
        '202209261600_list_type_to_logic_type',
        '202209291900_fix_wikiproject_parent',
        '202209301600_fix_templates',
        '202210121630_fix_avatar',
        '202210190912_add_filters_report',
        '202210211640_create_relation_type',
        '202210222111_fix_scaffold_logic_type',
        '202210251849_create_timetrackers',
        '202211061329_convert_old_epic',
        '202211061636_ui_view_form',
        '202211080232_has_child_tasks',
        '202211111306_has_child_tasks_gantt_path',
        '202211142216_scaffold_gantt_op_plan',
        '202211221455_connect_epics_subproj_to_gantt',
        '202211231332_fix_gantt_proj_tree_parent',
        '202212021332_relation_cache_recalculate',
        '202212021432_mv_to_trash',
        '202212061511_project_owner_assistans',
        '202212071432_fix_access_rules',
        '202212071948_smtp_fix',
        '202212131709_fix_gantt_proj_system_flag',
        '202212141538_set_gantt_task_parent',
        '202212210858_rename_relation_type',
        '202212261029_create_status_code',
        '202301110926_scaffold_workflow',
        '202301121700_orderno_is_template_set_default',
        '202301181029_create_status_code',
        '202301252255_scaffold_project_permissions',
        '202301301435_',
        '202302021200_calc_root_parent',
        '202302051705_update_relation',
        '202302070946_fill_ui_form',
        '202302080909_trashcan_fix_for_old_projects',
        '202302100925_system_trashcan',
        '202302101403_scaffold_project_permissions',
        '202302140928_logic_type_code_fix',
        '202302150804_show_icons_tree',
        '202303020812_fix_plugin',
        '202303031415_company_tasks',
        '202303071300_disable_timetracker_player',
        '202303091358_add_filter_is_none',
        '202303150819_group_ext_id',
        '202303201156_update_ui_form',
        '202303211649_update_relation_type',
        '202303241534_calc_cache_path_for_role',
        '202303271254_add_filter_is_none_v2',
        '202303291000_sdkesk_client_group',
        '202303291030_sdkesk_project_perm_scheme',
        '202303291130_sdkesk_logic_types',
        '202303291230_sdesk_create_sdesk_workflow',
        '202303291300_create_ui_forms',
        '202303291330_scaffold_calendar',
        '202303291430_ext_id_with_source_hash',
        '202304031200_person_var_show_in_main_menu_set',
        '202304061828_update_ui_froms',
        '202304102003_project_task_code_prefix',
        '202304130702_disable_account',
        '202304141011_remove_child_timetracker_entities',
        '202304141851_set_goals_calendar',
        '202304181040_auth_options',
        '202304181139_add_sys_type_reports_and_filters',
        '202304211605_trashcan_fix_for_old_projects',
        '202305040905_create_missing_active_sprints',
        '202305090950_crypt_passwords',
        '202305110919_delete_tree_lable',
        '202305220905_archive_fix_for_old_projects',
        '202305221233_delete_docs_history_created_by_system',
        '202305221234_replace_xml_to_attach_id_in_drawio',
        '202305301609_update_ui_view_form_for_pfeed',
        '202306061546_crypt',
        '202306061646_gantt_baseline_refactor',
        '202306081347_crypt_mailbox',
        '202306131203_uiform',
        '202306151503_eval_code_return_fix',
        '202306201143_project_perm_anon_comment',
        '202306221733_project_components',
        '202306231555_project_admins',
        '202306291412_backlog_ui_viewform',
        '202306301130_gantt_baseline_refactor_clone',
        '2023063012103_epic',
        '202307061933_root_parent_fix',
        '202307071026_allow_user_invite',
        '202307121241_project_perm_name',
        '202307131126_var_users',
        '202307181253_docflow',
        '202307211108_event_notify_scheme',
        '202307251236_uiform',
        '202308011717_notify_scheme_scaffold',
        '202308032217_notify_scheme_scaffold2',
        '202308041637_hidden_custom_choice',
        '202308041817_notify_scheme_scaffold3',
        '202308071318_project_view_type',
        '202308071617_notify_scheme_scaffold4',
        '202308081833_archive',
        '202308111717_notify_scheme_scaffold5',
        '202308141737_hidden_custom_choice2',
        '202308171746_calendar',
        '202308231937_hidden_custom_choice3',
        '202308291627_calendar_24_7',
        '202309041058_default_goal_calendar',
        '202309070934_fix_attach_ver',
        '202309071234_scaffold_logic_type',
        '202309071526_sdeskapprfw',
        '202309101037_msofiletypebulk',
        '202309141200_reindex_models',
        '202309211419_custom_fields_fix',
        '202309220831_calendar',
        '202309221609_previous_3_years_calendar',
        '202309281121_recalc_calendar',
        '202309281330_custom_fields_fix2',
        '202309281345_system_true_for_old_sprint_reports',
        '202310041721_newnotifyscheme',
        '202310051434_fix_fix_versions',
        '202310061532_ldap_default',
        '202310091518_fix_sprints',
        '202310131225_calendar_timezone',
        '202310131243_set_default_calendar',
        '202310180827_reindex_projects',
        '202310231106_sort_calendar_day_intervals',
        '202310231236_fix_default_workweek_in_system_calendar',
        '202310241136_sdnotify_assign_scheme',
        '202310241413_fix_label_has_child_tasks',
        '202311071143_sd_uiform_logictype',
        '202311071159_sd_wf_rule_fix',
        '202311080957_components',
        '202311081335_fix_emailed_request',
        '202311091142_hotfix_reindex_models',
        '202311141250_components',
        '202311221233_task_parent_logic_type_recalc',
        '202312051229_set_current_sla_cycle',
        '202312061256_custom_field_m2m_fix',
        '202312131159_scaffold_system_task_filters',
        '202312151503_create_gantt_for_cloned_projects',
        '202312200902_scaffold_system_task_filters',
        '202312221507_sd_wf_rule_fix2',
        '202401091607_gantt_fix',
        '202401101148_readonly_for_varspectators',
        '202401101332_default_person_calendar',
        '202401121432_rename_old_sd_wf_add_new',
        '202401121453_convert_bql_to_ubql2',
        '202401191218_sdhtmlscheme',
        '202401221045_add_attachment_notify_to_schema',
        '202401221428_load_synonyms',
        '202401222156_sdcustom_sdname',
        '202401241529_roadmaps',
        '202401241621_convert_bql_to_ubql2_fix',
        '202401311645_sso_plugin',
        '202402060841_fix_gantt_task_project',
        '202402071159_delete_stats_with_invalid_project_id',
        '202402091343_scaffold_sd_private_project_perm_scheme',
        '202402121744_populating_cmfgitrepo_type',
        '202402121955_init_client_spravochniki',
        '202402140814_fix_system_security_levels',
        '202403041038_system_group',
        '202403041314_calc_goal_bql',
        '202403051344_fitlers_from_gantt_to_gantt_2',
        '202403141419_scaffold_workflow',
        '202403200829_client_job',
    ]
    last_release_patch_list.sort()
    first_patch = models.CmfPatch.get(order_by=['name'], fields=['*'])
    for patch in last_release_patch_list:
        # first patch absent during build
        if first_patch:
            print(f'# Check {first_patch.name.value} <= {patch}')
            if first_patch.name.value <= patch:
                print(f'Более новые патчи уже хранятся корректно')
                break
        print(f'\tПомечаем патч {patch} как выполненный')
        if not models.CmfPatch.sget(name=patch):
            models.CmfPatch(name=patch).save()


if __name__ == "__main__":
    fix()
