def handle_one(self, domain, case_type, chunk_size): self.log('Copying {case_type} cases in {domain}' .format(case_type=case_type, domain=domain)) old_db = CommCareCase.get_db() new_db = IndicatorCase.get_db() assert old_db.uri != new_db.uri # this dbaccessor pulls from old_db case_ids = get_case_ids_in_domain(domain, case_type) self.delete_bad_doc_types(case_ids, chunk_size) case_dict_chunks = chunked(iter_docs(old_db, case_ids, chunk_size), chunk_size) for case_dicts in case_dict_chunks: for case_dict in case_dicts: del case_dict['_rev'] case_dict.pop('_attachments', None) case_dict['doc_type'] = "IndicatorCase" try: results = new_db.bulk_save(case_dicts) except BulkSaveError as error: results = error.results for result in results: if result.get('error') == 'conflict': self.log('- OK: [{id}] is already in the indicator db' .format(id=result.get('id'))) elif 'error' in result: self.log('- ERROR: [{id}] ({result})'.format( id=result.get('id'), result=json.dumps(result) )) else: self.log('- ADDED: [{id}] saved to indicator db'.format( id=result.get('id') ))
def delete_bad_doc_types(self, case_ids, chunk_size): """ No view in this db includes CommCareCases, so check manually """ db = IndicatorCase.get_db() case_dict_chunks = chunked(iter_docs(db, case_ids, chunk_size), chunk_size) to_delete = [] for case_dicts in case_dict_chunks: for case_dict in case_dicts: if case_dict['doc_type'] == 'CommCareCase': to_delete.append(case_dict['_id']) elif case_dict['doc_type'] != 'IndicatorCase': raise Exception("Unexpected case type {} found" .format(case_dict['doc_type'])) assert db.uri != CommCareCase.get_db().uri print "Deleting {} docs from db '{}'".format(len(to_delete), db.dbname) iter_bulk_delete(db, to_delete, chunk_size)
def delete_bad_doc_types(self, case_ids, chunk_size): """ No view in this db includes CommCareCases, so check manually """ db = IndicatorCase.get_db() case_dict_chunks = chunked(iter_docs(db, case_ids, chunk_size), chunk_size) to_delete = [] for case_dicts in case_dict_chunks: for case_dict in case_dicts: if case_dict['doc_type'] == 'CommCareCase': to_delete.append(case_dict['_id']) elif case_dict['doc_type'] != 'IndicatorCase': raise Exception("Unexpected case type {} found" .format(case_dict['doc_type'])) assert db.uri != CommCareCase.get_db().uri print "Deleting {} docs from db '{}'".format(len(to_delete), db.dbname) iter_bulk_delete(db, to_delete, chunk_size)
def handle_one(self, domain, case_type, chunk_size): self.log('Copying {case_type} cases in {domain}' .format(case_type=case_type, domain=domain)) old_db = CommCareCase.get_db() new_db = IndicatorCase.get_db() assert old_db.uri != new_db.uri case_ids = old_db.view( 'case/all_cases', startkey=["all type", domain, case_type], endkey=["all type", domain, case_type, {}], reduce=False, wrapper=lambda x: x['id'] ).all() self.delete_bad_doc_types(case_ids, chunk_size) case_dict_chunks = chunked(iter_docs(old_db, case_ids, chunk_size), chunk_size) for case_dicts in case_dict_chunks: for case_dict in case_dicts: del case_dict['_rev'] case_dict.pop('_attachments', None) case_dict['doc_type'] = "IndicatorCase" try: results = new_db.bulk_save(case_dicts) except BulkSaveError as error: results = error.results for result in results: if result.get('error') == 'conflict': self.log('- OK: [{id}] is already in the indicator db' .format(id=result.get('id'))) elif 'error' in result: self.log('- ERROR: [{id}] ({result})'.format( id=result.get('id'), result=json.dumps(result) )) else: self.log('- ADDED: [{id}] saved to indicator db'.format( id=result.get('id') ))