feat(vector): support renaming Milvus collections
This commit is contained in:
1 parent
4b75334164
commit
52c892676c
16 files changed
+461
-102
No files matched your search
@@ -264,6 +264,50 @@ pub async fn drop_collection(client: &VectorClient, database: &str, collection:
|
||||
.map(|_| ())
|
||||
}
|
||||
|
||||
fn validate_milvus_collection_rename(
|
||||
client: &VectorClient,
|
||||
database: &str,
|
||||
source_collection: &str,
|
||||
target_collection: &str,
|
||||
) -> Result<(), String> {
|
||||
if client.kind != VectorDbKind::Milvus {
|
||||
return Err("Collection rename is only supported for Milvus connections".to_string());
|
||||
}
|
||||
if database.is_empty() {
|
||||
return Err("Database name must not be empty".to_string());
|
||||
}
|
||||
if source_collection.is_empty() {
|
||||
return Err("Source collection name must not be empty".to_string());
|
||||
}
|
||||
if target_collection.is_empty() {
|
||||
return Err("Target collection name must not be empty".to_string());
|
||||
}
|
||||
if source_collection == target_collection {
|
||||
return Err("Source and target collection names must differ".to_string());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Rename a Milvus collection through the v2 REST API.
|
||||
pub async fn rename_collection(
|
||||
client: &VectorClient,
|
||||
database: &str,
|
||||
collection: &str,
|
||||
new_name: &str,
|
||||
) -> Result<(), String> {
|
||||
validate_milvus_collection_rename(client, database, collection, new_name)?;
|
||||
send_json(
|
||||
client.post("/v2/vectordb/collections/rename").json(&serde_json::json!({
|
||||
"dbName": database,
|
||||
"collectionName": collection,
|
||||
"newCollectionName": new_name,
|
||||
})),
|
||||
client.kind,
|
||||
)
|
||||
.await
|
||||
.map(|_| ())
|
||||
}
|
||||
|
||||
async fn list_milvus_databases(client: &VectorClient) -> Result<Vec<String>, String> {
|
||||
// Older Milvus versions (pre-2.2) do not expose the databases endpoint; fall back to the
|
||||
// configured database (or "default") so the connection stays browsable instead of failing the whole tree load.
|
||||
@@ -966,9 +1010,9 @@ fn format_reqwest_error(err: &reqwest::Error) -> String {
|
||||
mod tests {
|
||||
use super::{
|
||||
chroma_get_response_to_rows, default_collection_query, milvus_collection_schema, milvus_database_names,
|
||||
rest_query_result, starts_with_http_method, test_connection, test_connection_request, values_to_query_result,
|
||||
vector_auth, weaviate_collection_names_from_schema, weaviate_vector_dimension_from_graphql, CollectionInfo,
|
||||
VectorAuth, VectorClient, VectorDbKind,
|
||||
rename_collection, rest_query_result, starts_with_http_method, test_connection, test_connection_request,
|
||||
values_to_query_result, vector_auth, weaviate_collection_names_from_schema,
|
||||
weaviate_vector_dimension_from_graphql, CollectionInfo, VectorAuth, VectorClient, VectorDbKind,
|
||||
};
|
||||
use serde_json::{json, Value};
|
||||
use std::time::{Duration, Instant};
|
||||
@@ -1004,9 +1048,31 @@ mod tests {
|
||||
let (request_tx, request_rx) = tokio::sync::oneshot::channel();
|
||||
let server = tokio::spawn(async move {
|
||||
let (mut stream, _) = listener.accept().await.unwrap();
|
||||
let mut request = [0_u8; 8192];
|
||||
let read = stream.read(&mut request).await.unwrap();
|
||||
request_tx.send(String::from_utf8_lossy(&request[..read]).into_owned()).unwrap();
|
||||
let mut request = Vec::new();
|
||||
loop {
|
||||
let mut chunk = [0_u8; 2048];
|
||||
let read = stream.read(&mut chunk).await.unwrap();
|
||||
if read == 0 {
|
||||
break;
|
||||
}
|
||||
request.extend_from_slice(&chunk[..read]);
|
||||
let Some(header_end) = request.windows(4).position(|window| window == b"\r\n\r\n") else {
|
||||
continue;
|
||||
};
|
||||
let headers = String::from_utf8_lossy(&request[..header_end]);
|
||||
let content_length = headers
|
||||
.lines()
|
||||
.find_map(|line| {
|
||||
let (name, value) = line.split_once(':')?;
|
||||
name.eq_ignore_ascii_case("content-length").then(|| value.trim().to_owned())
|
||||
})
|
||||
.and_then(|length| length.parse::<usize>().ok())
|
||||
.unwrap_or(0);
|
||||
if request.len() >= header_end + 4 + content_length {
|
||||
break;
|
||||
}
|
||||
}
|
||||
request_tx.send(String::from_utf8_lossy(&request).into_owned()).unwrap();
|
||||
let response = format!(
|
||||
"HTTP/1.1 200 OK\r\ncontent-type: application/json\r\ncontent-length: {}\r\nconnection: close\r\n\r\n{}",
|
||||
body.len(),
|
||||
@@ -1295,6 +1361,36 @@ mod tests {
|
||||
assert!(request.headers().get("x-chroma-token").is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn milvus_rename_collection_uses_v2_api_and_expected_names() {
|
||||
let (url, request_rx, server) = spawn_recording_json_response_server(json!({ "code": 0 })).await;
|
||||
let client = VectorClient::new(VectorDbKind::Milvus, &url, None, None, false, Duration::from_secs(1));
|
||||
|
||||
rename_collection(&client, "analytics", "events", "events_archive").await.unwrap();
|
||||
let request = request_rx.await.unwrap();
|
||||
|
||||
assert!(request.starts_with("POST /v2/vectordb/collections/rename HTTP/1.1\r\n"));
|
||||
let request_body: Value = serde_json::from_str(request.split("\r\n\r\n").nth(1).unwrap()).unwrap();
|
||||
assert_eq!(
|
||||
request_body,
|
||||
json!({
|
||||
"dbName": "analytics",
|
||||
"collectionName": "events",
|
||||
"newCollectionName": "events_archive",
|
||||
})
|
||||
);
|
||||
server.await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn milvus_collection_rename_rejects_invalid_targets_before_sending() {
|
||||
let client =
|
||||
VectorClient::new(VectorDbKind::Milvus, "http://127.0.0.1:1", None, None, false, Duration::from_secs(1));
|
||||
|
||||
assert!(rename_collection(&client, "default", "events", "events").await.unwrap_err().contains("differ"));
|
||||
assert!(rename_collection(&client, "default", "events", "").await.unwrap_err().contains("must not be empty"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chroma_collection_paths_encode_every_dynamic_segment() {
|
||||
let client = VectorClient::new(
|
||||
|
||||
@@ -1118,6 +1118,24 @@ pub async fn drop_vector_collection_core(
|
||||
db::vector_driver::drop_collection(&client, database, collection).await
|
||||
}
|
||||
|
||||
pub async fn rename_vector_collection_core(
|
||||
state: &AppState,
|
||||
connection_id: &str,
|
||||
database: &str,
|
||||
collection: &str,
|
||||
new_name: &str,
|
||||
) -> Result<(), String> {
|
||||
let pool_key = state.get_or_create_metadata_pool_for_session(connection_id, Some(database), None).await?;
|
||||
let client = {
|
||||
let connections = state.connections.read().await;
|
||||
match connections.get(&pool_key) {
|
||||
Some(PoolKind::VectorDb(client)) => client.clone(),
|
||||
_ => return Err("Not a vector database connection".to_string()),
|
||||
}
|
||||
};
|
||||
db::vector_driver::rename_collection(&client, database, collection, new_name).await
|
||||
}
|
||||
|
||||
pub async fn get_table_comment_core(
|
||||
state: &AppState,
|
||||
connection_id: &str,
|
||||
|
||||
@@ -786,9 +786,11 @@ async fn main() {
|
||||
// MongoDB
|
||||
.route("/mongo/list-databases", post(routes::mongo::list_databases))
|
||||
.route("/mongo/list-collections", post(routes::mongo::list_collections))
|
||||
.route("/mongo/vector-collection-detail", post(routes::mongo::vector_collection_detail))
|
||||
.route("/vector/drop-database", post(routes::mongo::vector_drop_database))
|
||||
.route("/vector/drop-collection", post(routes::mongo::vector_drop_collection))
|
||||
.route("/mongo/vector-collection-detail", post(routes::vector::collection_detail))
|
||||
.route("/vector/collection-detail", post(routes::vector::collection_detail))
|
||||
.route("/vector/drop-database", post(routes::vector::drop_database))
|
||||
.route("/vector/drop-collection", post(routes::vector::drop_collection))
|
||||
.route("/vector/rename-collection", post(routes::vector::rename_collection))
|
||||
.route("/mongo/create-database", post(routes::mongo::create_database))
|
||||
.route("/mongo/drop-database", post(routes::mongo::drop_database))
|
||||
.route("/mongo/drop-collection", post(routes::mongo::drop_collection))
|
||||
|
||||
@@ -40,4 +40,5 @@ pub mod text_export;
|
||||
pub mod transfer;
|
||||
pub mod tunnel_profiles;
|
||||
pub mod update;
|
||||
pub mod vector;
|
||||
pub mod zookeeper;
|
||||
@@ -344,57 +344,6 @@ pub async fn list_collections(
|
||||
Ok(Json(result))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct VectorCollectionDetailRequest {
|
||||
pub connection_id: String,
|
||||
pub database: String,
|
||||
pub collection: String,
|
||||
}
|
||||
|
||||
pub async fn vector_collection_detail(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<VectorCollectionDetailRequest>,
|
||||
) -> Result<Json<dbx_core::db::vector_driver::CollectionInfo>, AppError> {
|
||||
let result = dbx_core::schema::get_vector_collection_detail_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&req.collection,
|
||||
)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
Ok(Json(result))
|
||||
}
|
||||
|
||||
pub async fn vector_drop_database(
|
||||
State(state): State<Arc<WebState>>,
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<MongoCollectionRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &req.database, "Drop database")
|
||||
.await?;
|
||||
ensure_writable(&state.app, &req.connection_id, "Drop database").await?;
|
||||
dbx_core::schema::drop_vector_database_core(&state.app, &req.connection_id, &req.database)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
Ok(Json(serde_json::json!({ "ok": true })))
|
||||
}
|
||||
|
||||
pub async fn vector_drop_collection(
|
||||
State(state): State<Arc<WebState>>,
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<MongoCollectionNameRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &req.database, "Drop collection")
|
||||
.await?;
|
||||
ensure_writable(&state.app, &req.connection_id, "Drop collection").await?;
|
||||
dbx_core::schema::drop_vector_collection_core(&state.app, &req.connection_id, &req.database, &req.collection)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
Ok(Json(serde_json::json!({ "ok": true })))
|
||||
}
|
||||
|
||||
pub async fn create_database(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<MongoCollectionRequest>,
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::extract::State;
|
||||
use axum::http::HeaderMap;
|
||||
use axum::Json;
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::error::AppError;
|
||||
use crate::state::WebState;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct VectorCollectionRequest {
|
||||
pub connection_id: String,
|
||||
pub database: String,
|
||||
pub collection: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct VectorDatabaseRequest {
|
||||
pub connection_id: String,
|
||||
pub database: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct VectorRenameCollectionRequest {
|
||||
pub connection_id: String,
|
||||
pub database: String,
|
||||
pub collection: String,
|
||||
pub new_name: String,
|
||||
}
|
||||
|
||||
async fn ensure_writable(state: &WebState, connection_id: &str, action: &str) -> Result<(), AppError> {
|
||||
if let Some(name) = dbx_core::query::connection_readonly_name(&state.app, connection_id).await {
|
||||
return Err(AppError::from(format!(
|
||||
"Read-only mode: connection '{}' has read-only protection enabled. {} blocked.",
|
||||
name, action
|
||||
)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn collection_detail(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<VectorCollectionRequest>,
|
||||
) -> Result<Json<dbx_core::db::vector_driver::CollectionInfo>, AppError> {
|
||||
dbx_core::schema::get_vector_collection_detail_core(&state.app, &req.connection_id, &req.database, &req.collection)
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(AppError::from)
|
||||
}
|
||||
|
||||
pub async fn drop_database(
|
||||
State(state): State<Arc<WebState>>,
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<VectorDatabaseRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &req.database, "Drop database")
|
||||
.await?;
|
||||
ensure_writable(&state, &req.connection_id, "Drop database").await?;
|
||||
dbx_core::schema::drop_vector_database_core(&state.app, &req.connection_id, &req.database)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
Ok(Json(serde_json::json!({ "ok": true })))
|
||||
}
|
||||
|
||||
pub async fn drop_collection(
|
||||
State(state): State<Arc<WebState>>,
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<VectorCollectionRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &req.database, "Drop collection")
|
||||
.await?;
|
||||
ensure_writable(&state, &req.connection_id, "Drop collection").await?;
|
||||
dbx_core::schema::drop_vector_collection_core(&state.app, &req.connection_id, &req.database, &req.collection)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
Ok(Json(serde_json::json!({ "ok": true })))
|
||||
}
|
||||
|
||||
pub async fn rename_collection(
|
||||
State(state): State<Arc<WebState>>,
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<VectorRenameCollectionRequest>,
|
||||
) -> Result<Json<serde_json::Value>, AppError> {
|
||||
super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &req.database, "Rename collection")
|
||||
.await?;
|
||||
ensure_writable(&state, &req.connection_id, "Rename collection").await?;
|
||||
dbx_core::schema::rename_vector_collection_core(
|
||||
&state.app,
|
||||
&req.connection_id,
|
||||
&req.database,
|
||||
&req.collection,
|
||||
&req.new_name,
|
||||
)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
Ok(Json(serde_json::json!({ "ok": true })))
|
||||
}
|
||||
Reference in new issue
Block a user