def handle(self, *args, **options): if len(args) < 2: raise CommandError('Usage is copy_case, %s' % self.args) source_couch = CouchConfig(args[0]) case_id = args[1] doc_ids = [case_id] domain = args[2] if len(args) > 2 else None if should_use_sql_backend(domain): raise CommandError('This command only works for couch-based domains.') def _migrate_case(case_id): print 'getting case %s' % case_id case = CommCareCase.wrap(source_couch.get_db_for_class(CommCareCase).get(case_id)) original_domain = case.domain if domain is not None: case.domain = domain case.save(force_update=True) return case, original_domain case, orig_domain = _migrate_case(case_id) print 'copying %s parent cases' % len(case.indices) for index in case.indices: _migrate_case(index.referenced_id) doc_ids.append(index.referenced_id) # hack, set the domain back to make sure we get the reverse indices correctly case.domain = orig_domain with OverrideDB(CommCareCase, source_couch.get_db_for_class(CommCareCase)): child_indices = get_reverse_indices(case) print 'copying %s child cases' % len(child_indices) for index in child_indices: _migrate_case(index.referenced_id) doc_ids.append(index.referenced_id) print 'copying %s xforms' % len(case.xform_ids) def form_wrapper(row): doc = row['doc'] doc.pop('_attachments', None) doc.pop('external_blobs', None) return XFormInstance.wrap(doc) xforms = source_couch.get_db_for_class(XFormInstance).all_docs( keys=case.xform_ids, include_docs=True, wrapper=form_wrapper, ).all() for form in xforms: if domain is not None: form.domain = domain form.save(force_update=True) print 'saved %s' % form._id doc_ids.append(form._id) if options['postgres_db']: copy_postgres_data_for_docs(options['postgres_db'], doc_ids)
def handle(self, *args, **options): if len(args) < 2: raise CommandError("Usage is copy_case, %s" % self.args) source_couch = CouchConfig(args[0]) case_id = args[1] doc_ids = [case_id] domain = args[2] if len(args) > 2 else None def _migrate_case(case_id): print "getting case %s" % case_id case = CommCareCase.wrap(source_couch.get_db_for_class(CommCareCase).get(case_id)) original_domain = case.domain if domain is not None: case.domain = domain case.save(force_update=True) return case, original_domain case, orig_domain = _migrate_case(case_id) print "copying %s parent cases" % len(case.indices) for index in case.indices: _migrate_case(index.referenced_id) doc_ids.append(index.referenced_id) # hack, set the domain back to make sure we get the reverse indices correctly case.domain = orig_domain with OverrideDB(CommCareCase, source_couch.get_db_for_class(CommCareCase)): child_indices = get_reverse_indices(case) print "copying %s child cases" % len(child_indices) for index in child_indices: _migrate_case(index.referenced_id) doc_ids.append(index.referenced_id) print "copying %s xforms" % len(case.xform_ids) def form_wrapper(row): doc = row["doc"] doc.pop("_attachments", None) return XFormInstance.wrap(doc) xforms = ( source_couch.get_db_for_class(XFormInstance) .all_docs(keys=case.xform_ids, include_docs=True, wrapper=form_wrapper) .all() ) for form in xforms: if domain is not None: form.domain = domain form.save(force_update=True) print "saved %s" % form._id doc_ids.append(form._id) if options["postgres_db"]: copy_postgres_data_for_docs(options["postgres_db"], doc_ids)