Skip to content

Commit 2057ddc

Browse files
minor: return an error if bulk_write is called on an unsupported server (#1117)
1 parent 4f454b9 commit 2057ddc

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/operation.rs

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub(crate) use update::{Update, UpdateOrReplace};
7676

7777
const SERVER_4_2_0_WIRE_VERSION: i32 = 8;
7878
const SERVER_4_4_0_WIRE_VERSION: i32 = 9;
79+
const SERVER_8_0_0_WIRE_VERSION: i32 = 25;
7980
// The maximum number of bytes that may be included in a write payload when auto-encryption is
8081
// enabled.
8182
const MAX_ENCRYPTED_WRITE_SIZE: usize = 2_097_152;

src/operation/bulk_write.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ use crate::{
2222
SessionCursor,
2323
};
2424

25-
use super::{ExecutionContext, Retryability, WriteResponseBody, OP_MSG_OVERHEAD_BYTES};
25+
use super::{
26+
ExecutionContext,
27+
Retryability,
28+
WriteResponseBody,
29+
OP_MSG_OVERHEAD_BYTES,
30+
SERVER_8_0_0_WIRE_VERSION,
31+
};
2632

2733
use server_responses::*;
2834

@@ -174,6 +180,13 @@ where
174180
const NAME: &'static str = "bulkWrite";
175181

176182
fn build(&mut self, description: &StreamDescription) -> Result<Command> {
183+
if description.max_wire_version.unwrap_or(0) < SERVER_8_0_0_WIRE_VERSION {
184+
return Err(ErrorKind::IncompatibleServer {
185+
message: "the bulk write feature is only supported on MongoDB 8.0+".to_string(),
186+
}
187+
.into());
188+
}
189+
177190
let max_message_size: usize =
178191
Checked::new(description.max_message_size_bytes).try_into()?;
179192
let max_operations: usize = Checked::new(description.max_write_batch_size).try_into()?;

src/test/bulk_write.rs

+18
Original file line numberDiff line numberDiff line change
@@ -564,3 +564,21 @@ async fn encryption_error() {
564564
Some("bulkWrite does not currently support automatic encryption".to_string())
565565
);
566566
}
567+
568+
#[tokio::test]
569+
async fn unsupported_server_client_error() {
570+
let client = Client::test_builder().build().await;
571+
572+
if client.server_version_gte(8, 0) {
573+
return;
574+
}
575+
576+
let error = client
577+
.bulk_write(vec![InsertOneModel::builder()
578+
.namespace(Namespace::new("db", "coll"))
579+
.document(doc! { "a": "b" })
580+
.build()])
581+
.await
582+
.unwrap_err();
583+
assert!(matches!(*error.kind, ErrorKind::IncompatibleServer { .. }));
584+
}

0 commit comments

Comments
 (0)