feat(mongodb): improve sidebar index management

This commit is contained in:
onenewcode authored and GitHub committed 2026-08-02 22:45:33 +08:00
1 parent 22055e081f
commit 8e7b74d0ee
56 files changed
+3558 -246

No files matched your search

+40 -3
View File
@@ -971,6 +971,7 @@ pub enum AgentCapability {
EtcdWatch,
EtcdLease,
EtcdAuth,
MongoDropDatabase,
MultiSession,
StructuredErrorV1,
}
@@ -1053,7 +1054,7 @@ fn parse_agent_rpc_error_header(header: &str) -> (Option<i64>, String) {
}
impl AgentCapability {
pub const ALL: [Self; 20] = [
pub const ALL: [Self; 21] = [
Self::Connect,
Self::TestConnection,
Self::Metadata,
@@ -1072,6 +1073,7 @@ impl AgentCapability {
Self::EtcdWatch,
Self::EtcdLease,
Self::EtcdAuth,
Self::MongoDropDatabase,
Self::MultiSession,
Self::StructuredErrorV1,
];
@@ -1096,6 +1098,7 @@ impl AgentCapability {
Self::EtcdWatch => "etcd_watch",
Self::EtcdLease => "etcd_lease",
Self::EtcdAuth => "etcd_auth",
Self::MongoDropDatabase => "mongo_drop_database",
Self::MultiSession => "multi_session",
Self::StructuredErrorV1 => "structured_error_v1",
}
@@ -1275,6 +1278,7 @@ pub enum MongoAgentMethod {
CreateIndex,
DropIndexes,
DropCollection,
DropDatabase,
InsertDocument,
UpdateDocument,
UpdateDocuments,
@@ -1283,7 +1287,7 @@ pub enum MongoAgentMethod {
}
impl MongoAgentMethod {
pub const ALL: [Self; 14] = [
pub const ALL: [Self; 15] = [
Self::ListDatabases,
Self::ListCollections,
Self::FindDocuments,
@@ -1293,6 +1297,7 @@ impl MongoAgentMethod {
Self::CreateIndex,
Self::DropIndexes,
Self::DropCollection,
Self::DropDatabase,
Self::InsertDocument,
Self::UpdateDocument,
Self::UpdateDocuments,
@@ -1311,6 +1316,7 @@ impl MongoAgentMethod {
Self::CreateIndex => "create_index",
Self::DropIndexes => "drop_indexes",
Self::DropCollection => "drop_collection",
Self::DropDatabase => "drop_database",
Self::InsertDocument => "insert_document",
Self::UpdateDocument => "update_document",
Self::UpdateDocuments => "update_documents",
@@ -2438,6 +2444,19 @@ impl AgentDriverClient {
self.call_mongo_method(MongoAgentMethod::ListCollections, mongo_database_params(database)).await
}
/// Request collection metadata while accepting old Agents that still
/// return the historical string array and ignore `include_types`.
pub async fn mongo_list_collection_specs<T: DeserializeOwned + Send + 'static>(
&mut self,
database: &str,
) -> Result<T, String> {
self.call_mongo_method(
MongoAgentMethod::ListCollections,
serde_json::json!({ "database": database, "include_types": true }),
)
.await
}
pub async fn mongo_find_documents<T: DeserializeOwned + Send + 'static>(
&mut self,
params: Value,
@@ -2488,6 +2507,13 @@ impl AgentDriverClient {
self.call_mongo_method(MongoAgentMethod::DropCollection, params).await
}
pub async fn mongo_drop_database<T: DeserializeOwned + Send + 'static>(
&mut self,
params: Value,
) -> Result<T, String> {
self.call_mongo_method(MongoAgentMethod::DropDatabase, params).await
}
pub async fn mongo_insert_document<T: DeserializeOwned + Send + 'static>(
&mut self,
params: Value,
@@ -2660,6 +2686,7 @@ pub fn agent_supports_capability(handshake: Option<&AgentHandshake>, capability:
| AgentCapability::EtcdWatch
| AgentCapability::EtcdLease
| AgentCapability::EtcdAuth
| AgentCapability::MongoDropDatabase
) {
return handshake.map(|value| value.supports(capability)).unwrap_or(false);
}
@@ -4242,9 +4269,10 @@ for line in sys.stdin:
assert_eq!(AgentCapability::EtcdWatch.as_str(), "etcd_watch");
assert_eq!(AgentCapability::EtcdLease.as_str(), "etcd_lease");
assert_eq!(AgentCapability::EtcdAuth.as_str(), "etcd_auth");
assert_eq!(AgentCapability::MongoDropDatabase.as_str(), "mongo_drop_database");
assert_eq!(AgentCapability::MultiSession.as_str(), "multi_session");
assert_eq!(AgentCapability::ALL.len(), 20);
assert_eq!(AgentCapability::StructuredErrorV1.as_str(), "structured_error_v1");
assert_eq!(AgentCapability::ALL.len(), 21);
}
#[test]
@@ -4292,6 +4320,7 @@ for line in sys.stdin:
assert_eq!(MongoAgentMethod::CreateIndex.as_str(), "create_index");
assert_eq!(MongoAgentMethod::DropIndexes.as_str(), "drop_indexes");
assert_eq!(MongoAgentMethod::DropCollection.as_str(), "drop_collection");
assert_eq!(MongoAgentMethod::DropDatabase.as_str(), "drop_database");
assert_eq!(MongoAgentMethod::InsertDocument.as_str(), "insert_document");
assert_eq!(MongoAgentMethod::UpdateDocument.as_str(), "update_document");
assert_eq!(MongoAgentMethod::UpdateDocuments.as_str(), "update_documents");
@@ -4350,6 +4379,7 @@ for line in sys.stdin:
fn exposes_mongo_protocol_wrappers() {
let _mongo_list_databases = AgentDriverClient::mongo_list_databases::<serde_json::Value>;
let _mongo_list_collections = AgentDriverClient::mongo_list_collections::<serde_json::Value>;
let _mongo_list_collection_specs = AgentDriverClient::mongo_list_collection_specs::<serde_json::Value>;
let _mongo_find_documents = AgentDriverClient::mongo_find_documents::<serde_json::Value>;
let _mongo_find_documents_extended_json =
AgentDriverClient::mongo_find_documents_extended_json::<serde_json::Value>;
@@ -4357,6 +4387,7 @@ for line in sys.stdin:
let _mongo_create_index = AgentDriverClient::mongo_create_index::<serde_json::Value>;
let _mongo_drop_indexes = AgentDriverClient::mongo_drop_indexes::<serde_json::Value>;
let _mongo_drop_collection = AgentDriverClient::mongo_drop_collection::<serde_json::Value>;
let _mongo_drop_database = AgentDriverClient::mongo_drop_database::<serde_json::Value>;
let _mongo_insert_document = AgentDriverClient::mongo_insert_document::<serde_json::Value>;
let _mongo_update_document = AgentDriverClient::mongo_update_document::<serde_json::Value>;
let _mongo_update_documents = AgentDriverClient::mongo_update_documents::<serde_json::Value>;
@@ -4587,6 +4618,12 @@ for line in sys.stdin:
assert!(!agent_supports_capability(Some(&handshake), AgentCapability::KvStatus));
assert!(!agent_supports_capability(None, AgentCapability::KvHistory));
assert!(!agent_supports_capability(Some(&handshake), AgentCapability::KvHistory));
assert!(!agent_supports_capability(None, AgentCapability::MongoDropDatabase));
assert!(!agent_supports_capability(Some(&handshake), AgentCapability::MongoDropDatabase));
let mongo_handshake =
AgentHandshake { capabilities: vec![AgentCapability::MongoDropDatabase.as_str().to_string()], ..handshake };
assert!(agent_supports_capability(Some(&mongo_handshake), AgentCapability::MongoDropDatabase));
}
#[test]
+335 -46
View File
@@ -20,6 +20,14 @@ pub type MongoDocumentResult = DocumentQueryResult;
pub struct MongoDropIndexesResult {
pub dropped_names: Vec<String>,
pub affected_rows: u64,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub failures: Vec<MongoDropIndexFailure>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct MongoDropIndexFailure {
pub name: String,
pub message: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
@@ -222,6 +230,16 @@ impl MongoCollectionKind {
_ => Self::Collection,
}
}
/// Convert the Legacy Agent's optional listCollections metadata. Unknown
/// values stay compatible with the former name-only response.
pub fn from_metadata_kind(kind: Option<&str>) -> Self {
match kind.map(str::trim) {
Some(kind) if kind.eq_ignore_ascii_case("view") => Self::View,
Some(kind) if kind.eq_ignore_ascii_case("timeseries") => Self::Timeseries,
_ => Self::Collection,
}
}
}
/// Name + kind for a MongoDB collection/view/timeseries returned by full `listCollections`.
@@ -528,25 +546,26 @@ pub async fn create_database(client: &Client, database: &str) -> Result<(), Stri
}
pub async fn drop_database(client: &Client, database: &str) -> Result<(), String> {
let database = database.trim();
if database.is_empty() {
return Err("Database name is required".to_string());
}
let database = validate_mongo_namespace_name(database, "Database")?;
client.database(database).drop().await.map_err(|e| e.to_string())
}
pub async fn drop_collection(client: &Client, database: &str, collection: &str) -> Result<(), String> {
let database = database.trim();
let collection = collection.trim();
if database.is_empty() {
return Err("Database name is required".to_string());
}
if collection.is_empty() {
return Err("Collection name is required".to_string());
}
let database = validate_mongo_namespace_name(database, "Database")?;
let collection = validate_mongo_namespace_name(collection, "Collection")?;
client.database(database).collection::<Document>(collection).drop().await.map_err(|e| e.to_string())
}
/// MongoDB namespace identifiers must reach the server unchanged. In
/// particular, a collection name may legitimately contain leading or trailing
/// whitespace, so validation only rejects the empty identifier.
pub fn validate_mongo_namespace_name<'a>(name: &'a str, kind: &str) -> Result<&'a str, String> {
if name.is_empty() {
return Err(format!("{kind} name is required"));
}
Ok(name)
}
/// Build the admin `renameCollection` command document for a same-database rename.
///
/// Collection (and database) names are identifiers and must be passed exactly as provided —
@@ -1020,9 +1039,44 @@ pub async fn create_index(
keys_json: &str,
options_json: Option<&str>,
) -> Result<String, String> {
let database = validate_mongo_namespace_name(database, "Database")?;
let collection = validate_mongo_namespace_name(collection, "Collection")?;
let (command, name) = create_indexes_command(collection, keys_json, options_json)?;
client.database(database).run_command(command).await.map_err(|e| e.kind.to_string())?;
Ok(name)
}
/// Validate an index request before it reaches either the native driver or the
/// Legacy Agent. In particular, `key` belongs to the createIndexes command
/// itself and must not be smuggled through the options document.
pub fn validate_create_index_request(keys_json: &str, options_json: Option<&str>) -> Result<(), String> {
parse_create_index_spec(keys_json, options_json).map(|_| ())
}
fn create_indexes_command(
collection: &str,
keys_json: &str,
options_json: Option<&str>,
) -> Result<(Document, String), String> {
let (keys, options, name) = parse_create_index_spec(keys_json, options_json)?;
let mut index = doc! { "key": keys };
for (option, value) in options {
index.insert(option, value);
}
Ok((doc! { "createIndexes": collection, "indexes": [index] }, name))
}
fn parse_create_index_spec(
keys_json: &str,
options_json: Option<&str>,
) -> Result<(Document, Document, String), String> {
let keys_value: serde_json::Value =
serde_json::from_str(keys_json).map_err(|e| format!("Invalid index keys JSON: {e}"))?;
let keys = json_object_to_document(&keys_value).map_err(|e| format!("Invalid index keys: {e}"))?;
// The internal transport uses JSON rather than a Mongo shell expression.
// Keep ordinary strings literal while supporting official Extended JSON
// wrappers, matching the Legacy Agent's Document.parse behavior.
let keys = json_object_to_document_extended_json(&keys_value).map_err(|e| format!("Invalid index keys: {e}"))?;
if keys.is_empty() {
return Err("Index keys are required".to_string());
}
@@ -1031,16 +1085,40 @@ pub async fn create_index(
Some(json) => {
let value: serde_json::Value =
serde_json::from_str(json).map_err(|e| format!("Invalid index options JSON: {e}"))?;
let doc = json_object_to_document(&value).map_err(|e| format!("Invalid index options: {e}"))?;
Some(mongodb::bson::from_document::<IndexOptions>(doc).map_err(|e| format!("Invalid index options: {e}"))?)
json_object_to_document_extended_json(&value).map_err(|e| format!("Invalid index options: {e}"))?
}
None => None,
None => Document::new(),
};
if options.contains_key("key") {
return Err("Index options cannot contain \"key\"; specify index fields in keys JSON".to_string());
}
let name = match options.get("name") {
Some(Bson::String(name)) if !name.trim().is_empty() => name.clone(),
Some(_) => return Err("Index option \"name\" must be a non-empty string".to_string()),
None => default_index_name(&keys),
};
let col = client.database(database).collection::<Document>(collection);
let result =
col.create_index(IndexModel::builder().keys(keys).options(options).build()).await.map_err(|e| e.to_string())?;
Ok(result.index_name)
// Use the raw server command instead of deserializing into the driver's
// fixed IndexOptions struct. This keeps native and Legacy Agent requests
// equivalent and lets the connected MongoDB version validate new options.
let mut options = options;
if !options.contains_key("name") {
options.insert("name", name.clone());
}
Ok((keys, options, name))
}
fn default_index_name(keys: &Document) -> String {
keys.iter()
.map(|(field, value)| {
let value = match value {
Bson::String(value) => value.clone(),
value => value.to_string(),
};
format!("{field}_{value}")
})
.collect::<Vec<_>>()
.join("_")
}
pub async fn drop_indexes(
@@ -1050,14 +1128,8 @@ pub async fn drop_indexes(
indexes_json: Option<&str>,
single: bool,
) -> Result<MongoDropIndexesResult, String> {
let database = database.trim();
let collection = collection.trim();
if database.is_empty() {
return Err("Database name is required".to_string());
}
if collection.is_empty() {
return Err("Collection name is required".to_string());
}
let database = validate_mongo_namespace_name(database, "Database")?;
let collection = validate_mongo_namespace_name(collection, "Collection")?;
let index = parse_drop_indexes_value(indexes_json, single)?;
let before = list_indexes(client, database, collection).await?;
@@ -1068,7 +1140,57 @@ pub async fn drop_indexes(
.map_err(|e| e.to_string())?;
let after = list_indexes(client, database, collection).await?;
let dropped_names = diff_dropped_index_names(&before, &after);
Ok(MongoDropIndexesResult { affected_rows: dropped_names.len() as u64, dropped_names })
Ok(MongoDropIndexesResult { affected_rows: dropped_names.len() as u64, dropped_names, failures: Vec::new() })
}
/// Validate a drop-index request before it is sent to either MongoDB driver.
/// The Legacy Agent receives the original JSON, so keeping this validation
/// public gives both paths the same protection for MongoDB's default index.
pub fn validate_drop_indexes_request(indexes_json: Option<&str>, single: bool) -> Result<(), String> {
parse_drop_indexes_value(indexes_json, single).map(|_| ())
}
/// MongoDB added array-form `dropIndexes.index` in 4.2. Unknown version
/// strings preserve the modern single-command semantics instead of risking a
/// partially applied serial fallback.
pub fn mongo_server_requires_serial_drop_indexes(version: &str) -> bool {
let Some(start) = version.find(|character: char| character.is_ascii_digit()) else {
return false;
};
let mut components = version[start..].split('.');
let Some(major) = components.next().and_then(parse_version_component) else {
return false;
};
let Some(minor) = components.next().and_then(parse_version_component) else {
return false;
};
(major, minor) < (4, 2)
}
fn parse_version_component(component: &str) -> Option<u32> {
let digits = component.chars().take_while(|character| character.is_ascii_digit()).collect::<String>();
(!digits.is_empty()).then(|| digits.parse().ok()).flatten()
}
/// Return names that must be issued as individual dropIndex commands for
/// MongoDB 3.4 compatibility. The original request is fully parsed first, so
/// an invalid name (including `_id_`) cannot cause a partial batch mutation.
pub fn serial_drop_index_names(indexes_json: Option<&str>, single: bool) -> Result<Option<Vec<String>>, String> {
let Bson::Array(indexes) = parse_drop_indexes_value(indexes_json, single)? else {
return Ok(None);
};
let mut seen = HashSet::new();
let mut names = Vec::new();
for index in indexes {
let Bson::String(name) = index else {
return Err("dropIndexes only accepts arrays of string index names".to_string());
};
if seen.insert(name.clone()) {
names.push(name);
}
}
Ok(Some(names))
}
fn diff_dropped_index_names(before: &[IndexInfo], after: &[IndexInfo]) -> Vec<String> {
@@ -1086,51 +1208,64 @@ fn parse_drop_indexes_value(indexes_json: Option<&str>, single: bool) -> Result<
fn parse_drop_indexes_json(json: &str, single: bool) -> Result<Bson, String> {
let value: serde_json::Value = serde_json::from_str(json).map_err(|e| format!("Invalid index JSON: {e}"))?;
let bson = json_value_to_bson(&value);
if single {
validate_single_drop_index_value(&value)?;
validate_single_drop_index_value(&bson)?;
} else {
validate_multi_drop_indexes_value(&value)?;
validate_multi_drop_indexes_value(&bson)?;
}
Ok(json_value_to_bson(&value))
Ok(bson)
}
fn validate_single_drop_index_value(value: &serde_json::Value) -> Result<(), String> {
fn validate_single_drop_index_value(value: &Bson) -> Result<(), String> {
match value {
serde_json::Value::String(name) => {
Bson::String(name) => {
if name.trim().is_empty() {
Err("Index name is required".to_string())
} else if name == "*" {
Err(r#"dropIndex does not accept "*"; use dropIndexes() or dropIndexes("*") instead"#.to_string())
} else if name == "_id_" {
Err("The default MongoDB _id_ index cannot be dropped".to_string())
} else {
Ok(())
}
}
serde_json::Value::Object(doc) if doc.is_empty() => Err("Index specification is required".to_string()),
serde_json::Value::Object(_) => Ok(()),
serde_json::Value::Array(_) => {
Bson::Document(doc) if doc.is_empty() => Err("Index specification is required".to_string()),
Bson::Document(doc) if is_default_id_index_specification(doc) => {
Err("The default MongoDB _id_ index cannot be dropped".to_string())
}
Bson::Document(_) => Ok(()),
Bson::Array(_) => {
Err("dropIndex only accepts a string index name or JSON document; arrays are not supported".to_string())
}
_ => Err("dropIndex only accepts a string index name or JSON document".to_string()),
}
}
fn validate_multi_drop_indexes_value(value: &serde_json::Value) -> Result<(), String> {
fn validate_multi_drop_indexes_value(value: &Bson) -> Result<(), String> {
match value {
serde_json::Value::String(name) => {
Bson::String(name) => {
if name.trim().is_empty() {
Err("Index name is required".to_string())
} else if name == "_id_" {
Err("The default MongoDB _id_ index cannot be dropped".to_string())
} else {
Ok(())
}
}
serde_json::Value::Object(doc) if doc.is_empty() => Err("Index specification is required".to_string()),
serde_json::Value::Object(_) => Ok(()),
serde_json::Value::Array(items) if items.is_empty() => {
Err("dropIndexes only accepts non-empty string arrays".to_string())
Bson::Document(doc) if doc.is_empty() => Err("Index specification is required".to_string()),
Bson::Document(doc) if is_default_id_index_specification(doc) => {
Err("The default MongoDB _id_ index cannot be dropped".to_string())
}
serde_json::Value::Array(items) => {
if items.iter().all(|item| matches!(item, serde_json::Value::String(name) if !name.trim().is_empty())) {
Ok(())
Bson::Document(_) => Ok(()),
Bson::Array(items) if items.is_empty() => Err("dropIndexes only accepts non-empty string arrays".to_string()),
Bson::Array(items) => {
if items.iter().all(|item| matches!(item, Bson::String(name) if !name.trim().is_empty())) {
if items.iter().any(|item| matches!(item, Bson::String(name) if name == "_id_")) {
Err("The default MongoDB _id_ index cannot be dropped".to_string())
} else {
Ok(())
}
} else {
Err("dropIndexes only accepts arrays of string index names".to_string())
}
@@ -1139,6 +1274,20 @@ fn validate_multi_drop_indexes_value(value: &serde_json::Value) -> Result<(), St
}
}
fn is_default_id_index_specification(specification: &Document) -> bool {
specification.len() == 1 && specification.get("_id").is_some_and(is_bson_numeric_one)
}
fn is_bson_numeric_one(value: &Bson) -> bool {
match value {
Bson::Int32(value) => *value == 1,
Bson::Int64(value) => *value == 1,
Bson::Double(value) => *value == 1.0,
Bson::Decimal128(value) => value.to_string().parse::<f64>().is_ok_and(|value| value == 1.0),
_ => false,
}
}
pub async fn insert_document(
client: &Client,
database: &str,
@@ -2592,6 +2741,102 @@ mod tests {
assert!(index.is_primary);
}
#[test]
fn mongo_namespace_validation_preserves_whitespace_and_rejects_only_empty_names() {
assert_eq!(validate_mongo_namespace_name(" app ", "Database").unwrap(), " app ");
assert_eq!(validate_mongo_namespace_name(" users ", "Collection").unwrap(), " users ");
assert_eq!(validate_mongo_namespace_name(" ", "Collection").unwrap(), " ");
let error = validate_mongo_namespace_name("", "Collection").unwrap_err();
assert_eq!(error, "Collection name is required");
}
#[test]
fn create_indexes_command_keeps_raw_options_and_generates_a_name() {
let (command, name) = create_indexes_command(
"users",
r#"{"email":1,"createdAt":-1,"content":"text"}"#,
Some(
r#"{"unique":true,"partialFilterExpression":{"verified":true},"customServerOption":{"enabled":true}}"#,
),
)
.unwrap();
assert_eq!(name, "email_1_createdAt_-1_content_text");
assert_eq!(command.get_str("createIndexes").unwrap(), "users");
let index = command.get_array("indexes").unwrap()[0].as_document().unwrap();
assert_eq!(
index.get_document("key").unwrap().keys().collect::<Vec<_>>(),
vec!["email", "createdAt", "content"],
"compound-index key order is part of MongoDB index semantics"
);
assert_eq!(
index.get_document("key").unwrap(),
&doc! { "email": 1_i32, "createdAt": -1_i32, "content": "text" }
);
assert!(index.get_bool("unique").unwrap());
assert_eq!(index.get_str("name").unwrap(), "email_1_createdAt_-1_content_text");
assert_eq!(index.get_document("customServerOption").unwrap(), &doc! { "enabled": true });
}
#[test]
fn create_indexes_command_canonicalizes_a_whole_double_in_the_default_name() {
let (command, name) = create_indexes_command("users", r#"{"email":1.0}"#, None).unwrap();
assert_eq!(name, "email_1");
let index = command.get_array("indexes").unwrap()[0].as_document().unwrap();
assert_eq!(index.get_str("name").unwrap(), "email_1");
assert!(
matches!(index.get("key"), Some(Bson::Document(keys)) if matches!(keys.get("email"), Some(Bson::Double(value)) if *value == 1.0))
);
}
#[test]
fn create_indexes_command_preserves_extended_json_options() {
let (command, name) = create_indexes_command(
"events",
r#"{"expiresAt":1}"#,
Some(r#"{"name":"expires_ttl","partialFilterExpression":{"createdAt":{"$date":"2026-06-10T13:59:31.287Z"}}}"#),
)
.unwrap();
assert_eq!(name, "expires_ttl");
let index = command.get_array("indexes").unwrap()[0].as_document().unwrap();
let filter = index.get_document("partialFilterExpression").unwrap();
assert!(matches!(filter.get("createdAt"), Some(Bson::DateTime(_))));
}
#[test]
fn create_indexes_command_keeps_plain_json_strings_literal() {
let (command, _) = create_indexes_command(
"events",
r#"{"expiresAt":1}"#,
Some(r#"{"partialFilterExpression":{"source":"ISODate(\"2026-06-10T13:59:31.287Z\")"}}"#),
)
.unwrap();
let index = command.get_array("indexes").unwrap()[0].as_document().unwrap();
let filter = index.get_document("partialFilterExpression").unwrap();
assert!(
matches!(filter.get("source"), Some(Bson::String(value)) if value == "ISODate(\"2026-06-10T13:59:31.287Z\")")
);
}
#[test]
fn create_indexes_command_rejects_key_inside_options() {
let error = create_indexes_command("users", r#"{"email":1}"#, Some(r#"{"key":{"other":1}}"#)).unwrap_err();
assert!(error.contains("cannot contain \"key\""), "{error}");
}
#[test]
fn create_indexes_command_rejects_empty_or_non_string_names() {
for options in [r#"{"name":""}"#, r#"{"name":" "}"#, r#"{"name":null}"#, r#"{"name":1}"#] {
let error = create_indexes_command("users", r#"{"email":1}"#, Some(options)).unwrap_err();
assert!(error.contains("non-empty string"), "{error}");
}
}
#[test]
fn parse_drop_indexes_value_validates_drop_index_arguments() {
assert!(matches!(
@@ -2642,6 +2887,50 @@ mod tests {
assert!(invalid_array.contains("arrays of string index names"));
}
#[test]
fn drop_indexes_validation_rejects_the_default_id_index() {
for (indexes_json, single) in [
(Some(r#""_id_""#), true),
(Some(r#""_id_""#), false),
(Some(r#"{"_id":1}"#), true),
(Some(r#"{"_id":1}"#), false),
(Some(r#"{"_id":1.0}"#), true),
(Some(r#"{"_id":{"$numberInt":"1"}}"#), true),
(Some(r#"{"_id":{"$numberLong":"1"}}"#), false),
(Some(r#"{"_id":{"$numberDouble":"1.0"}}"#), false),
(Some(r#"{"_id":{"$numberDecimal":"1.0"}}"#), true),
(Some(r#"["email_1","_id_"]"#), false),
] {
let error = validate_drop_indexes_request(indexes_json, single).unwrap_err();
assert!(error.contains("_id_"), "{error}");
}
// MongoDB defines dropIndexes("*") to retain the default _id_ index.
assert!(matches!(validate_drop_indexes_request(Some(r#""*""#), false), Ok(())));
}
#[test]
fn serial_drop_index_names_is_portable_and_prevalidates_all_names() {
assert_eq!(
serial_drop_index_names(Some(r#"["email_1","email_1","createdAt_-1"]"#), false).unwrap(),
Some(vec!["email_1".to_string(), "createdAt_-1".to_string()])
);
assert_eq!(serial_drop_index_names(Some(r#""*""#), false).unwrap(), None);
let error = serial_drop_index_names(Some(r#"["email_1","_id_"]"#), false).unwrap_err();
assert!(error.contains("_id_"), "{error}");
}
#[test]
fn serial_drop_indexes_fallback_is_limited_to_pre_42_servers() {
for version in ["3.4.24", "4.0.28", "MongoDB 3.6.23"] {
assert!(mongo_server_requires_serial_drop_indexes(version), "{version}");
}
for version in ["4.2.0", "4.4.29", "5.0.0-rc0", "8.0.1", "unknown", "4"] {
assert!(!mongo_server_requires_serial_drop_indexes(version), "{version}");
}
}
#[test]
fn diff_dropped_index_names_reports_removed_indexes() {
let before = vec![