def test_series(self): self.assertEqual(CommCareCase.get_db(), self.normal_db) self.assertEqual(CommCareCase.get_db, self.normal_get_db) with OverrideDB(CommCareCase, self.other_db_1): self.assertEqual(CommCareCase.get_db(), self.other_db_1) self.assertNotEqual(CommCareCase.get_db(), self.normal_db) self.assertNotEqual(CommCareCase.get_db(), self.normal_get_db) self.assertEqual(CommCareCase.get_db(), self.normal_db) self.assertEqual(CommCareCase.get_db, self.normal_get_db) with OverrideDB(CommCareCase, self.other_db_2): self.assertEqual(CommCareCase.get_db(), self.other_db_2) self.assertNotEqual(CommCareCase.get_db(), self.normal_db) self.assertNotEqual(CommCareCase.get_db(), self.normal_get_db) self.assertEqual(CommCareCase.get_db(), self.normal_db) self.assertEqual(CommCareCase.get_db, self.normal_get_db)
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 test_threading(self): result_queue = Queue() with OverrideDB(CommCareCase, self.other_db_1): obj = _override_db.class_to_db def run(): with OverrideDB(CommCareCase, self.other_db_2): result_queue.put(_override_db.class_to_db) t = threading.Thread(target=run) t.start() t.join() result = result_queue.get_nowait() self.assertNotEqual(id(obj), id(result))
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 run(): with OverrideDB(CommCareCase, self.other_db_2): result_queue.put(_override_db.class_to_db)
class Command(LabelCommand): help = "Copy a case and all related forms" args = '<sourcedb> <case_id> <domain>' option_list = LabelCommand.option_list + (make_option( '--postgres-db', action='store', dest='postgres_db', default='', help= "Name of postgres database to pull additional data from. This should map to a " "key in settings.DATABASES. If not specified no additional postgres data will be " "copied. This is currently used to pull CommCare Supply models."), ) def handle(self, *args, **options): if len(args) < 2: raise CommandError('Usage is copy_case, %s' % self.args) sourcedb = Database(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(sourcedb.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, sourcedb): 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 = sourcedb.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)
class Command(BaseCommand): help = "Copy a case and all related forms" def add_arguments(self, parser): parser.add_argument('sourcedb', ) parser.add_argument('case_id', ) parser.add_argument( 'domain', nargs='?', ) parser.add_argument( '--postgres-db', action='store', dest='postgres_db', default='', help= "Name of postgres database to pull additional data from. This should map to a " "key in settings.DATABASES. If not specified no additional postgres data will be " "copied. This is currently used to pull CommCare Supply models.", ) def handle(self, sourcedb, case_id, domain, **options): # FIXME broken b/c https://github.com/dimagi/commcare-hq/pull/15896 source_couch = CouchConfig(sourcedb) doc_ids = [case_id] 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)