def remove_indices_from_deleted_cases(domain, case_ids): from corehq.apps.hqcase.utils import submit_case_blocks deleted_ids = set(case_ids) indexes_referencing_deleted_cases = get_all_reverse_indices_info(domain, case_ids) case_updates = [ CaseBlock( case_id=index_info.case_id, index={ index_info.identifier: (index_info.referenced_type, '') # blank string = delete index } ).as_string(format_datetime=json_format_datetime) for index_info in indexes_referencing_deleted_cases if index_info.case_id not in deleted_ids ] submit_case_blocks(case_updates, domain)
def get_cleanliness_flag_from_scratch(domain, owner_id): footprint_info = get_case_footprint_info(domain, owner_id) cases_to_check = footprint_info.all_ids - footprint_info.base_ids if cases_to_check: closed_owned_case_ids = set(get_closed_case_ids(domain, owner_id)) cases_to_check = cases_to_check - closed_owned_case_ids if cases_to_check: # it wasn't in any of the open or closed IDs - it must be dirty reverse_index_infos = get_all_reverse_indices_info(domain, list(cases_to_check)) reverse_index_ids = set([r.case_id for r in reverse_index_infos]) indexed_with_right_owner = reverse_index_ids & (footprint_info.base_ids | closed_owned_case_ids) found_deleted_cases = False while indexed_with_right_owner: hint_id = indexed_with_right_owner.pop() infos_for_this_owner = _get_info_by_case_id(reverse_index_infos, hint_id) for info in infos_for_this_owner: try: case = CommCareCase.get(info.referenced_id) if case.doc_type == "CommCareCase": return CleanlinessFlag(False, hint_id) else: found_deleted_cases = True except ResourceNotFound: # the case doesn't exist - don't use it as a dirty flag found_deleted_cases = True if found_deleted_cases: # if we made it all the way to the end of the loop without returning anything # then the owner was only flagged as dirty due to missing cases, # This implies the owner is still clean. return CleanlinessFlag(True, None) else: # I don't believe code can ever be hit, but if it is we should fail hard # until we can better understand it. raise IllegalCaseId( "Owner {} in domain {} has an invalid index reference chain!!".format(owner_id, domain) ) return CleanlinessFlag(True, None)
def get_all_reverse_indices_info(domain, case_ids): return get_all_reverse_indices_info(domain, case_ids)