Repository: meilisearch/meilisearch
File: index-scheduler/src/batch.rs (Rust)

Code before:
                    task.status = Status::Succeeded;
                    // TODO should we put a details = None, here?
                    // TODO we are putting Details::IndexInfo with a primary_key = None, this is not cool bro'
                    task.details = task.kind.default_details();
                }

                Ok(tasks)
            }
        }
    }

    fn apply_index_operation<'txn, 'i>(
        &self,
        index_wtxn: &'txn mut RwTxn<'i, '_>,
        index: &'i milli::Index,
        operation: IndexOperation,
    ) -> Result<Vec<Task>> {
        match operation {
            IndexOperation::DocumentClear { mut tasks, .. } => {
                let result = milli::update::ClearDocuments::new(index_wtxn, index).execute();
                for task in &mut tasks {
                    match result {
                        Ok(deleted_documents) => {
                            task.status = Status::Succeeded;
                            task.details = Some(Details::ClearAll {
                                deleted_documents: Some(deleted_documents),
                            });
                        }
                        Err(ref error) => {
                            task.status = Status::Failed;
                            task.error = Some(MilliError(error).into())
                        }
                    }
                }

                Ok(tasks)
            }
            IndexOperation::DocumentImport {
                index_uid: _,
                primary_key,
                method,
                documents_counts,
                content_files,
                mut tasks,
            } => {
                let indexer_config = self.index_mapper.indexer_config();
                // TODO use the code from the IndexCreate operation
                if let Some(primary_key) = primary_key {
                    if index.primary_key(index_wtxn)?.is_none() {
                        let mut builder =
                            milli::update::Settings::new(index_wtxn, index, indexer_config);

Review comment (suggestion):
Also, here we have a mixed bunch of tasks where some are IndexAddition / Update; thus we should generate the default task.details for everything except the DocumentClear.
And for the FIRST DocumentClear task we write the details as you've done. For the next one we write the `deleted_documents` set to zero.

Code after:
                    let index_rtxn = index.read_txn()?;
                    index.number_of_documents(&index_rtxn)?
                };

                // The write transaction is directly owned and commited inside.
                self.index_mapper.delete_index(wtxn, &index_uid)?;

                // We set all the tasks details to the default value.
                for task in &mut tasks {
                    task.status = Status::Succeeded;
                    task.details = match &task.kind {
                        KindWithContent::IndexDeletion { .. } => Some(Details::ClearAll {
                            deleted_documents: Some(number_of_documents),
                        }),
                        otherwise => otherwise.default_details(),
                    };
                }

                Ok(tasks)
            }
        }
    }

    fn apply_index_operation<'txn, 'i>(
        &self,
        index_wtxn: &'txn mut RwTxn<'i, '_>,
        index: &'i milli::Index,
        operation: IndexOperation,
    ) -> Result<Vec<Task>> {
        match operation {
            IndexOperation::DocumentClear { mut tasks, .. } => {
                let count = milli::update::ClearDocuments::new(index_wtxn, index).execute()?;

                let mut first_clear_found = false;
                for task in &mut tasks {
                    task.status = Status::Succeeded;
                    // The first document clear will effectively delete every documents
                    // in the database but the next ones will clear 0 documents.
                    task.details = match &task.kind {
                        KindWithContent::DocumentClear { .. } => {
                            let count = if first_clear_found { 0 } else { count };
                            first_clear_found = true;
                            Some(Details::ClearAll {
                                deleted_documents: Some(count),
                            })
                        }
                        otherwise => otherwise.default_details(),
                    };
                }

                Ok(tasks)