From 20319f797405e0527b39ee8b7edf8dbbac0aae21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Tue, 5 Nov 2019 12:02:42 +0100 Subject: [PATCH] Allow to introduce attributes only at the end of a schema --- meilidb-core/src/error.rs | 6 +++--- meilidb-core/src/update/schema_update.rs | 9 +++++++-- meilidb-schema/src/lib.rs | 4 ++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/meilidb-core/src/error.rs b/meilidb-core/src/error.rs index 3fb4d199c..7d58e2756 100644 --- a/meilidb-core/src/error.rs +++ b/meilidb-core/src/error.rs @@ -97,7 +97,7 @@ pub enum UnsupportedOperation { SchemaAlreadyExists, CannotUpdateSchemaIdentifier, CannotReorderSchemaAttribute, - CannotIntroduceNewSchemaAttribute, + CanOnlyIntroduceNewSchemaAttributesAtEnd, CannotRemoveSchemaAttribute, } @@ -108,8 +108,8 @@ impl fmt::Display for UnsupportedOperation { SchemaAlreadyExists => write!(f, "Cannot update index which already have a schema"), CannotUpdateSchemaIdentifier => write!(f, "Cannot update the identifier of a schema"), CannotReorderSchemaAttribute => write!(f, "Cannot reorder the attributes of a schema"), - CannotIntroduceNewSchemaAttribute => { - write!(f, "Cannot introduce new attributes in a schema") + CanOnlyIntroduceNewSchemaAttributesAtEnd => { + write!(f, "Can only introduce new attributes at end of a schema") } CannotRemoveSchemaAttribute => write!(f, "Cannot remove attributes from a schema"), } diff --git a/meilidb-core/src/update/schema_update.rs b/meilidb-core/src/update/schema_update.rs index 06f37b617..c033df6cf 100644 --- a/meilidb-core/src/update/schema_update.rs +++ b/meilidb-core/src/update/schema_update.rs @@ -14,7 +14,7 @@ pub fn apply_schema_update( docs_words_store: store::DocsWords, ) -> MResult<()> { use UnsupportedOperation::{ - CannotIntroduceNewSchemaAttribute, CannotRemoveSchemaAttribute, + CanOnlyIntroduceNewSchemaAttributesAtEnd, CannotRemoveSchemaAttribute, CannotReorderSchemaAttribute, CannotUpdateSchemaIdentifier, }; @@ -33,7 +33,12 @@ pub fn apply_schema_update( need_full_reindexing = true; } } - Diff::NewAttr { .. } => return Err(CannotIntroduceNewSchemaAttribute.into()), + Diff::NewAttr { pos, .. } => { + // new attribute not at the end of the schema + if pos < old_schema.number_of_attributes() { + return Err(CanOnlyIntroduceNewSchemaAttributesAtEnd.into()); + } + } Diff::RemovedAttr { .. } => return Err(CannotRemoveSchemaAttribute.into()), } } diff --git a/meilidb-schema/src/lib.rs b/meilidb-schema/src/lib.rs index 5ec64f3b6..929316d41 100644 --- a/meilidb-schema/src/lib.rs +++ b/meilidb-schema/src/lib.rs @@ -169,6 +169,10 @@ impl Schema { attributes } + pub fn number_of_attributes(&self) -> usize { + self.inner.attrs.len() + } + pub fn props(&self, attr: SchemaAttr) -> SchemaProps { let (_, props) = self.inner.props[attr.0 as usize]; props