def handle(self, *args, **options): raise CommandError( 'copy_group_data is currently broken. ' 'Ask Danny or Ethan to fix it along the lines of ' 'https://github.com/dimagi/commcare-hq/pull/9180/files#diff-9d976dc051a36a028c6604581dfbce5dR95' ) if len(args) != 2: raise CommandError('Usage is copy_group_data %s' % self.args) sourcedb = Database(args[0]) group_id = args[1] exclude_user_owned = options["exclude_user_owned"] print 'getting group' group = Group.wrap(sourcedb.get(group_id)) group.save(force_update=True) print 'getting domain' domain = Domain.wrap( sourcedb.view('domain/domains', key=group.domain, include_docs=True, reduce=False, limit=1).one()['doc']) dt = DocumentTransform(domain._obj, sourcedb) save(dt, Domain.get_db()) owners = [group_id] if not exclude_user_owned: owners.extend(group.users) print 'getting case ids' with OverrideDB(CommCareCase, sourcedb): case_ids = get_case_ids_in_domain_by_owner(domain.name, owner_id__in=owners) xform_ids = set() print 'copying %s cases' % len(case_ids) for i, subset in enumerate(chunked(case_ids, CHUNK_SIZE)): print i * CHUNK_SIZE cases = [ CommCareCase.wrap(case['doc']) for case in sourcedb.all_docs( keys=list(subset), include_docs=True, ) ] for case in cases:
def copy_domain(self, sourcedb, domain): print "Copying domain doc" result = sourcedb.view("domain/domains", key=domain, reduce=False, include_docs=True).first() if result and 'doc' in result: domain_doc = Domain.wrap(result['doc']) dt = DocumentTransform(domain_doc, sourcedb) save(dt, self.targetdb) else: print "Domain doc not found for domain %s." % domain
def handle(self, *args, **options): if len(args) < 2 or len(args) > 3: raise CommandError('Usage is copy_doc %s' % self.args) sourcedb = Database(args[0]) app_id = args[1] domain = args[2] if len(args) == 3 else None app_json = sourcedb.get(app_id) if domain: app_json['domain'] = domain dt = DocumentTransform(app_json, sourcedb) save(dt, get_db())
def run(self): for doc, count in iter(self.queue.get, None): try: if self.exclude_types and doc["doc_type"] in self.exclude_types: print " SKIPPED (excluded type: %s). Synced %s/%s docs (%s: %s)" % \ (doc["doc_type"], count, self.total, doc["doc_type"], doc["_id"]) else: if not self.simulate: dt = DocumentTransform(doc, self.sourcedb) save(dt, self.targetdb) print " Synced %s/%s docs (%s: %s)" % (count, self.total, doc["doc_type"], doc["_id"]) except Exception, e: print " Document %s failed! Error is: %s" % (doc["_id"], e)
def copy_domain(self, source_couch, domain): print "Copying domain doc" sourcedb = source_couch.get_db_for_class(Domain) result = sourcedb.view("domain/domains", key=domain, reduce=False, include_docs=True).first() if result and 'doc' in result: domain_doc = Domain.wrap(result['doc']) dt = DocumentTransform(domain_doc._obj, sourcedb) save(dt, self.targetdb.get_db_for_doc_type(domain_doc['doc_type'])) else: print "Domain doc not found for domain %s." % domain
def copy_doc(doc, count, sourcedb, target_couch, exclude_types, total, simulate, exclude_attachments): if exclude_types and doc["doc_type"] in exclude_types: print(" SKIPPED (excluded type: %s). Synced %s/%s docs (%s: %s)" % \ (doc["doc_type"], count, total, doc["doc_type"], doc["_id"])) else: if not simulate: dt = DocumentTransform(doc, sourcedb, exclude_attachments) for i in reversed(list(range(5))): try: save(dt, target_couch) break except (ResourceConflict, ParserError, TypeError): if i == 0: raise print(" Synced %s/%s docs (%s: %s)" % (count, total, doc["doc_type"], doc["_id"]))
def handle(self, sourcedb, doc_ids_or_file, domain, **options): sourcedb = Database(sourcedb) if os.path.isfile(doc_ids_or_file): with open(doc_ids_or_file) as f: doc_ids = f.read().splitlines() else: doc_ids = doc_ids_or_file.split(',') print("Starting copy of {} docs".format(len(doc_ids))) for doc_id in doc_ids: print('Copying doc: {}'.format(doc_id)) doc_json = sourcedb.get(doc_id) if domain: doc_json['domain'] = domain dt = DocumentTransform(doc_json, sourcedb) save(dt, get_db())
def handle(self, *args, **options): if len(args) != 2: raise CommandError('Usage is copy_group_data %s' % self.args) sourcedb = Database(args[0]) group_id = args[1] exclude_user_owned = options["exclude_user_owned"] print 'getting group' group = Group.wrap(sourcedb.get(group_id)) group.save(force_update=True) print 'getting domain' domain = Domain.wrap( sourcedb.view('domain/domains', key=group.domain, include_docs=True, reduce=False, limit=1).one()['doc'] ) dt = DocumentTransform(domain._obj, sourcedb) save(dt, Domain.get_db()) owners = [group_id] if not exclude_user_owned: owners.extend(group.users) print 'getting case ids' with OverrideDB(CommCareCase, sourcedb): case_ids = get_case_ids_in_domain_by_owner( domain.name, owner_id__in=owners) xform_ids = set() print 'copying %s cases' % len(case_ids) for i, subset in enumerate(chunked(case_ids, CHUNK_SIZE)): print i * CHUNK_SIZE cases = [CommCareCase.wrap(case['doc']) for case in sourcedb.all_docs( keys=list(subset), include_docs=True, )] for case in cases:
def handle(self, *args, **options): if len(args) < 2 or len(args) > 3: raise CommandError('Usage is copy_doc %s' % self.args) sourcedb = Database(args[0]) doc_ids_or_file = args[1] domain = args[2] if len(args) == 3 else None if os.path.isfile(doc_ids_or_file): with open(doc_ids_or_file) as f: doc_ids = f.read().splitlines() else: doc_ids = doc_ids_or_file.split(',') print("Starting copy of {} docs".format(len(doc_ids))) for doc_id in doc_ids: print('Copying doc: {}'.format(doc_id)) doc_json = sourcedb.get(doc_id) if domain: doc_json['domain'] = domain dt = DocumentTransform(doc_json, sourcedb) save(dt, get_db())