From e7994cdeb3f2660b959d8b8237643d6db6f26143 Mon Sep 17 00:00:00 2001 From: Gregory Conrad Date: Sun, 26 Mar 2023 12:18:39 -0400 Subject: [PATCH] feat: check to see if the PK changed before erroring out Previously, if the primary key was set and a Settings update contained a primary key, an error would be returned. However, this error is not needed if the new PK == the current PK. This commit just checks to see if the PK actually changes before raising an error. --- milli/src/update/settings.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/milli/src/update/settings.rs b/milli/src/update/settings.rs index 4f4fa25d6..3e271924b 100644 --- a/milli/src/update/settings.rs +++ b/milli/src/update/settings.rs @@ -565,8 +565,12 @@ impl<'a, 't, 'u, 'i> Settings<'a, 't, 'u, 'i> { self.index.put_primary_key(self.wtxn, primary_key)?; Ok(()) } else { - let primary_key = self.index.primary_key(self.wtxn)?.unwrap(); - Err(UserError::PrimaryKeyCannotBeChanged(primary_key.to_string()).into()) + let curr_primary_key = self.index.primary_key(self.wtxn)?.unwrap().to_string(); + if primary_key == &curr_primary_key { + Ok(()) + } else { + Err(UserError::PrimaryKeyCannotBeChanged(curr_primary_key).into()) + } } } Setting::Reset => { @@ -1332,6 +1336,17 @@ mod tests { .unwrap(); wtxn.commit().unwrap(); + // Updating settings with the same primary key should do nothing + let mut wtxn = index.write_txn().unwrap(); + index + .update_settings_using_wtxn(&mut wtxn, |settings| { + settings.set_primary_key(S("mykey")); + }) + .unwrap(); + assert_eq!(index.primary_key(&wtxn).unwrap(), Some("mykey")); + wtxn.commit().unwrap(); + + // Updating the settings with a different (or no) primary key causes an error let mut wtxn = index.write_txn().unwrap(); let error = index .update_settings_using_wtxn(&mut wtxn, |settings| {