Пример #1
0
 def test_get_docs_by_database(self):
     # test_python_hashing_gives_correct_db ensures the hashing works correctly so this just tests
     # that get_docs_by_database is consistent with get_database_for_docs
     form_ids = [str(uuid4()) for i in range(100)]
     dbs_for_docs = ShardAccessor.get_database_for_docs(form_ids)
     docs_for_dbs = ShardAccessor.get_docs_by_database(form_ids)
     for db, doc_ids in docs_for_dbs.items():
         for doc_id in doc_ids:
             self.assertEqual(db, dbs_for_docs[doc_id])
Пример #2
0
def _publish_cases_for_sql(domain, case_records):
    records_with_types = filter(lambda r: r.doc_subtype, case_records)
    records_with_no_types = filter(lambda r: not r.doc_subtype, case_records)
    # if we already had a type just publish as-is
    for record in records_with_types:
        producer.send_change(
            topics.CASE_SQL,
            _change_meta_for_sql_case(domain, record.doc_id,
                                      record.doc_subtype))

    # else lookup the type from the database
    for record_chunk in chunked(records_with_no_types, 10000):
        # databases will contain a mapping of shard database ids to case_ids in that DB
        id_chunk = [r.doc_id for r in record_chunk]
        databases = ShardAccessor.get_docs_by_database(id_chunk)
        for db, doc_ids in databases.items():
            results = CommCareCaseSQL.objects.using(db).filter(
                case_id__in=doc_ids, ).values_list('case_id', 'type')
            # make sure we found the same number of IDs
            assert len(results) == len(doc_ids)
            for case_id, case_type in results:
                producer.send_change(
                    topics.CASE_SQL,
                    _change_meta_for_sql_case(domain, case_id, case_type))