def _copy(config): # unfortunately the only couch view we have for this needs to go by domain # will be a bit slow database = Domain.get_db() assert database.uri == config.source_db.uri, 'can only use "copy" with the main HQ DB as the source' domain_names = Domain.get_all_names() for domain in domain_names: for doc_type in config.doc_types: ids_of_this_type = [row['id'] for row in database.view( 'domain/docs', startkey=[domain, doc_type], endkey=[domain, doc_type, {}], reduce=False, include_docs=False, )] if ids_of_this_type: new_revs = dict([ (row['id'], row['value']['rev']) for row in config.dest_db.view('_all_docs', keys=ids_of_this_type, include_docs=False) if 'error' not in row ]) for id_group in chunked(ids_of_this_type, 500): docs = get_docs(database, id_group) for doc in docs: if doc['_id'] in new_revs: doc['_rev'] = new_revs[doc['_id']] config.dest_db.bulk_save(docs) print 'copied {} {}s from {}'.format(len(ids_of_this_type), doc_type, domain) print 'copy docs complete'
def handle(self, doc_types, *args, **options): input = raw_input('\n'.join([ '\n\nReally delete documents of the following types: {}?', 'This operation is not reversible. Enter a number N to delete the first ' 'N found, or type "delete all" to delete everything.', '', ]).format(doc_types)) if input == 'delete all': remaining = None else: try: remaining = int(input) except ValueError: print 'aborting' sys.exit() doc_types = doc_types.split(',') deleted = 0 # unfortunately the only couch view we have for this needs to go by domain # will be a bit slow domain_names = Domain.get_all_names() database = Domain.get_db() for domain in domain_names: for doc_type in doc_types: docs = [row['doc'] for row in database.view( 'domain/docs', startkey=[domain, doc_type], endkey=[domain, doc_type, {}], reduce=False, include_docs=True, )][:remaining] if docs: count = len(docs) print 'deleting {} {}s from {}'.format(count, doc_type, domain) database.delete_docs(docs) deleted += count if remaining is not None: remaining -= count if remaining <= 0: return print 'successfully deleted {} documents'.format(deleted)