def purge_locations(domain): """ Delete all location data associated with <domain>. This means Locations, SQLLocations, LocationTypes, and anything which has a ForeignKey relationship to SQLLocation (as of 2015-03-02, this includes only StockStates and some custom stuff). """ location_ids = set([r['id'] for r in Location.get_db().view( 'locations/by_type', reduce=False, startkey=[domain], endkey=[domain, {}], ).all()]) iter_bulk_delete(Location.get_db(), location_ids) for loc in SQLLocation.objects.filter(domain=domain).iterator(): if loc.supply_point_id: case = CommCareCase.get(loc.supply_point_id) case.delete() loc.delete() db = Domain.get_db() domain_obj = Domain.get_by_name(domain) # cached lookup is fast but stale domain_json = db.get(domain_obj._id) # get latest raw, unwrapped doc domain_json.pop('obsolete_location_types', None) domain_json.pop('location_types', None) db.save_doc(domain_json)
def delete_all_locations(): ids = [ doc['id'] for doc in SupplyPointCase.get_db().view('supply_point_by_loc/view', reduce=False).all() ] iter_bulk_delete(SupplyPointCase.get_db(), ids) delete_all_docs_by_doc_type(Location.get_db(), ['Location']) SQLLocation.objects.all().delete()
def delete_all_users(): from corehq.apps.users.models import CouchUser from django.contrib.auth.models import User def _clear_cache(doc): user = CouchUser.wrap_correctly(doc, allow_deleted_doc_types=True) user.clear_quickcache_for_user() iter_bulk_delete(CommCareUser.get_db(), get_all_user_ids(), doc_callback=_clear_cache) User.objects.all().delete()
def delete(self): # delete all associated objects db = self.get_db() related_doc_ids = [row['id'] for row in db.view('domain/related_to_domain', startkey=[self.name], endkey=[self.name, {}], include_docs=False, )] iter_bulk_delete(db, related_doc_ids, chunksize=500) super(Domain, self).delete()
def delete_all_locations(): ids = [ doc['id'] for doc in SupplyPointCase.get_db().view('commtrack/supply_point_by_loc', reduce=False).all() ] iter_bulk_delete(SupplyPointCase.get_db(), ids) iter_bulk_delete(Location.get_db(), SQLLocation.objects.location_ids()) SQLLocation.objects.all().delete()
def delete(self): # delete all associated objects db = self.get_db() related_doc_ids = [ row['id'] for row in db.view( 'domain/related_to_domain', startkey=[self.name], endkey=[self.name, {}], include_docs=False, ) ] iter_bulk_delete(db, related_doc_ids, chunksize=500) super(Domain, self).delete()
def delete_all_locations(): to_delete = [(Location, 'locations/by_name'), (SupplyPointCase, 'commtrack/supply_point_by_loc')] for model, view in to_delete: ids = [ doc['id'] for doc in model.get_db().view(view, reduce=False).all() ] try: iter_bulk_delete(model.get_db(), ids) except BulkSaveError: pass SQLLocation.objects.all().delete()
def delete(self): from corehq.apps.domain.signals import commcare_domain_pre_delete results = commcare_domain_pre_delete.send_robust(sender='domain', domain=self) for result in results: if result[1]: raise DomainDeleteException( u"Error occurred during domain pre_delete {}: {}".format(self.name, str(result[1])) ) # delete all associated objects for db, related_doc_ids in get_all_doc_ids_for_domain_grouped_by_db(self.name): iter_bulk_delete(db, related_doc_ids, chunksize=500) self._delete_web_users_from_domain() self._delete_sql_objects() super(Domain, self).delete() Domain.get_by_name.clear(Domain, self.name) # clear the domain cache
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 _pre_delete(self): from corehq.apps.domain.signals import commcare_domain_pre_delete from corehq.apps.domain.deletion import apply_deletion_operations dynamic_deletion_operations = [] results = commcare_domain_pre_delete.send_robust(sender='domain', domain=self) for result in results: response = result[1] if isinstance(response, Exception): raise DomainDeleteException(u"Error occurred during domain pre_delete {}: {}".format(self.name, str(response))) elif response: assert isinstance(response, list) dynamic_deletion_operations.extend(response) # delete all associated objects for db, related_doc_ids in get_all_doc_ids_for_domain_grouped_by_db(self.name): iter_bulk_delete(db, related_doc_ids, chunksize=500) apply_deletion_operations(self.name, dynamic_deletion_operations)
def handle(self, domain, **options): db = ExportInstance.get_db() exports = db.view( 'export_instances_by_domain/view', startkey=[domain], endkey=[domain, {}], include_docs=False, reduce=False, ).all() if not exports: print("No exports to delete here, exiting.") return filter_exports = lambda _type: [row for row in exports if _type in row['key']] form_exports = filter_exports('FormExportInstance') case_exports = filter_exports('CaseExportInstance') confirm = input( "There are {f_count} form exports, and {c_count} case exports. " "Are you sure you want to delete all these exports [y/N]?\n".format( f_count=len(form_exports), c_count=len(case_exports) ) ) to_delete = [] if confirm.lower() == 'y': _type = input( "Enter 'case' to delete all case exports, " "'form to delete all form exports, " "'all' to delete both form and case exports. " "Entery anything else to exit.\n" ) if _type == 'form': to_delete = form_exports print("Deleting form exports") elif _type == 'case': to_delete = case_exports print("Deleting case exports") elif _type == 'all': to_delete = form_exports + case_exports print("Deleting all exports") else: print("Not deleting anything, exiting!") return total_count = iter_bulk_delete(db, [doc['id'] for doc in to_delete]) print("Deleted total of {} exports succesfully!".format(total_count))
def delete_all(self): case_ids = get_case_ids_in_domain_by_owner(self.domain, self.user.user_id) iter_bulk_delete(self.db, case_ids)
def hard_delete_deleted_users(): # Hard deleted the deleted users to truncate the view db_view = CommCareUser.get_db().view('deleted_users_by_username/view', reduce=False) deleted_user_ids = [row['id'] for row in db_view] iter_bulk_delete(CommCareUser.get_db(), deleted_user_ids)
def delete_all(self): case_ids = get_case_ids_in_domain_by_owner( self.domain, self.user.user_id) iter_bulk_delete(self.db, case_ids)
def delete_all(self): case_ids = [r["id"] for r in self.case_query(reduce=False)] iter_bulk_delete(self.db, case_ids)
def delete_all_users(): if not settings.UNIT_TESTING: raise Exception("You're not allowed to delete all users except in tests!") iter_bulk_delete(CommCareUser.get_db(), get_all_user_ids())
def delete_fixture_items_for_data_type(domain, data_type_id): from corehq.apps.fixtures.models import FixtureDataItem iter_bulk_delete(FixtureDataItem.get_db(), [ i["_id"] for i in iter_fixture_items_for_data_type(domain, data_type_id) ])
def tearDown(self): all_auditcare_ids = list(set([result["id"] for result in NavigationEventAudit.get_db().view( "auditcare/urlpath_by_user_date", reduce=False, ).all()])) iter_bulk_delete(NavigationEventAudit.get_db(), all_auditcare_ids)
def handle(self, domain, **options): db = ExportInstance.get_db() exports = db.view( 'export_instances_by_domain/view', startkey=[domain], endkey=[domain, {}], include_docs=False, reduce=False, ).all() if not exports: print("No exports to delete here, exiting.") return if options['days_inactive'] > 0: import datetime inactive_since = datetime.datetime.today() - datetime.timedelta(days=int(options['days_inactive'])) inactive_exports = [] for export in exports: e = ExportInstance.get(export['id']) if e.last_accessed and e.last_accessed <= inactive_since: inactive_exports.append(export) if not inactive_exports: print("No exports have been inactive for more than {days_inactive} days, exiting.".format( days_inactive=options['days_inactive']) ) return confirm = input( "There are {total_exports} exports in {domain}. Are you sure you want to delete " "{total_inactive_exports} that are older than {days_inactive} days [y/N]?".format( total_exports=len(exports), total_inactive_exports=len(inactive_exports), domain=domain, days_inactive=int(options['days_inactive']) ) ) if confirm.lower() == 'y': exports = inactive_exports else: return filter_exports = lambda _type: [row for row in exports if _type in row['key']] form_exports = filter_exports('FormExportInstance') case_exports = filter_exports('CaseExportInstance') confirm = input( "There are {f_count} form exports, and {c_count} case exports. " "Are you sure you want to delete all these exports [y/N]?\n".format( f_count=len(form_exports), c_count=len(case_exports) ) ) to_delete = [] if confirm.lower() == 'y': _type = input( "Enter 'case' to delete all case exports, " "'form to delete all form exports, " "'all' to delete both form and case exports. " "Enter anything else to exit.\n" ) if _type == 'form': to_delete = form_exports print("Deleting form exports") elif _type == 'case': to_delete = case_exports print("Deleting case exports") elif _type == 'all': to_delete = form_exports + case_exports print("Deleting all exports") else: print("Not deleting anything, exiting!") return total_count = iter_bulk_delete(db, [doc['id'] for doc in to_delete]) print("Deleted total of {} exports succesfully!".format(total_count))
def delete_all_users(): iter_bulk_delete(CommCareUser.get_db(), get_all_user_ids())