fix: updated_at was not 'updated' when updating the index name

This commit is contained in:
Tamo
2025-08-07 17:34:11 +02:00
parent ae2d0a67a4
commit ae5bd9d0e3
3 changed files with 43 additions and 6 deletions

View File

@ -10,6 +10,7 @@ use meilisearch_types::tasks::{Details, IndexSwap, Kind, KindWithContent, Status
use meilisearch_types::versioning::{VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH}; use meilisearch_types::versioning::{VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH};
use milli::update::Settings as MilliSettings; use milli::update::Settings as MilliSettings;
use roaring::RoaringBitmap; use roaring::RoaringBitmap;
use time::OffsetDateTime;
use super::create_batch::Batch; use super::create_batch::Batch;
use crate::processing::{ use crate::processing::{
@ -232,8 +233,15 @@ impl IndexScheduler {
Batch::IndexUpdate { index_uid, primary_key, new_index_uid, mut task } => { Batch::IndexUpdate { index_uid, primary_key, new_index_uid, mut task } => {
progress.update_progress(UpdateIndexProgress::UpdatingTheIndex); progress.update_progress(UpdateIndexProgress::UpdatingTheIndex);
// Get the index (renamed or not)
let rtxn = self.env.read_txn()?;
let index = self.index_mapper.index(&rtxn, &index_uid)?;
let mut index_wtxn = index.write_txn()?;
// Handle rename if new_index_uid is provided // Handle rename if new_index_uid is provided
let final_index_uid = if let Some(new_uid) = &new_index_uid { let final_index_uid = if let Some(new_uid) = &new_index_uid {
index.set_updated_at(&mut index_wtxn, &OffsetDateTime::now_utc())?;
let mut wtxn = self.env.write_txn()?; let mut wtxn = self.env.write_txn()?;
// Rename the index // Rename the index
@ -252,13 +260,9 @@ impl IndexScheduler {
} else { } else {
index_uid.clone() index_uid.clone()
}; };
// Get the index (renamed or not)
let rtxn = self.env.read_txn()?;
let index = self.index_mapper.index(&rtxn, &final_index_uid)?;
// Handle primary key update if provided // Handle primary key update if provided
if let Some(ref primary_key) = primary_key { if let Some(ref primary_key) = primary_key {
let mut index_wtxn = index.write_txn()?;
let mut builder = MilliSettings::new( let mut builder = MilliSettings::new(
&mut index_wtxn, &mut index_wtxn,
&index, &index,
@ -274,9 +278,9 @@ impl IndexScheduler {
current_batch.embedder_stats.clone(), current_batch.embedder_stats.clone(),
) )
.map_err(|e| Error::from_milli(e, Some(final_index_uid.to_string())))?; .map_err(|e| Error::from_milli(e, Some(final_index_uid.to_string())))?;
index_wtxn.commit()?;
} }
index_wtxn.commit()?;
// drop rtxn before starting a new wtxn on the same db // drop rtxn before starting a new wtxn on the same db
rtxn.commit()?; rtxn.commit()?;

View File

@ -1,3 +1,5 @@
use meili_snap::snapshot;
use time::format_description::well_known::Rfc3339; use time::format_description::well_known::Rfc3339;
use time::OffsetDateTime; use time::OffsetDateTime;
@ -106,3 +108,34 @@ async fn error_update_unexisting_index() {
assert_eq!(response["error"], expected_response); assert_eq!(response["error"], expected_response);
} }
#[actix_rt::test]
async fn update_index_name() {
let server = Server::new_shared();
let index = server.unique_index();
let (task, code) = index.create(None).await;
assert_eq!(code, 202);
server.wait_task(task.uid()).await.succeeded();
let new_index = server.unique_index();
let (task, _status_code) = index.update_raw(json!({ "uid": new_index.uid })).await;
server.wait_task(task.uid()).await.succeeded();
let (response, code) = new_index.get().await;
snapshot!(code, @"200 OK");
assert_eq!(response["uid"], new_index.uid);
assert!(response.get("createdAt").is_some());
assert!(response.get("updatedAt").is_some());
let created_at =
OffsetDateTime::parse(response["createdAt"].as_str().unwrap(), &Rfc3339).unwrap();
let updated_at =
OffsetDateTime::parse(response["updatedAt"].as_str().unwrap(), &Rfc3339).unwrap();
assert!(created_at < updated_at, "{created_at} should be inferior to {updated_at}");
snapshot!(response["primaryKey"], @"null");
snapshot!(response.as_object().unwrap().len(), @"4");
}

View File

@ -1457,7 +1457,7 @@ impl Index {
.0) .0)
} }
pub(crate) fn set_updated_at( pub fn set_updated_at(
&self, &self,
wtxn: &mut RwTxn<'_>, wtxn: &mut RwTxn<'_>,
time: &time::OffsetDateTime, time: &time::OffsetDateTime,