def non_indexed_doc_ids_not_planned_to_index(
        document_type: DocumentType,
        pack_size: int = 100) -> Generator[List[int], None, None]:

    table_name = doc_fields_table_name(document_type.code)

    with connection.cursor() as cursor:
        # return documents of the specified type which
        # - do not exist in the corresponding fields cache
        # - have no planned but not-started reindex tasks on them
        cursor.execute(
            'select dd.id \n'
            'from document_document dd \n'
            'left outer join "{table_name}" df on dd.id = df.document_id \n'
            'left outer join lateral (select jsonb_array_elements(args->0) doc_id \n'
            '                         from task_task \n'
            '                         where name = %s \n'
            '                         and own_status = %s\n'
            '                         and date_work_start is null) tt on tt.doc_id = to_jsonb(dd.id) \n'
            'where dd.document_type_id = %s and df.document_id is null and tt.doc_id is null'
            .format(table_name=table_name),
            [_get_reindex_task_name(), 'PENDING', document_type.uid])

        rows = cursor.fetchmany(pack_size)
        while rows:
            yield [row[0] for row in rows]
            rows = cursor.fetchmany(pack_size)
예제 #2
0
def do_migrate(apps, schema_editor):
    DocumentType = apps.get_model('document', 'DocumentType')
    with connection.cursor() as cursor:
        for document_type in DocumentType.objects.all():
            table_name = doc_fields_table_name(
                document_type_code=document_type.code)
            cursor.execute('DROP TABLE IF EXISTS "{table_name}"'.format(
                table_name=table_name))
def there_are_non_indexed_docs_not_planned_to_index(
        document_type: DocumentType, log: ProcessLogger) -> bool:
    for doc_id in non_indexed_doc_ids_not_planned_to_index(document_type, 1):
        if doc_id:
            task_name = _get_reindex_task_name()
            fields_table = doc_fields_table_name(document_type.code)
            log.info(
                f'there_are_non_indexed_docs_not_planned_to_index: '
                f'found document id={doc_id} of type {document_type.code}, '
                f'task {task_name}. Fields table: {fields_table}')
            return True
    return False