Handle POST /rename-table - rename a table.
(
Extension(state): Extension<Arc<RESTServer>>,
Json(request): Json<RenameTableRequest>,
)
| 453 | |
| 454 | /// Handle POST /rename-table - rename a table. |
| 455 | pub async fn rename_table( |
| 456 | Extension(state): Extension<Arc<RESTServer>>, |
| 457 | Json(request): Json<RenameTableRequest>, |
| 458 | ) -> impl IntoResponse { |
| 459 | let mut s = state.inner.lock().unwrap(); |
| 460 | |
| 461 | let source_key = format!("{}.{}", request.source.database(), request.source.object()); |
| 462 | let dest_key = format!( |
| 463 | "{}.{}", |
| 464 | request.destination.database(), |
| 465 | request.destination.object() |
| 466 | ); |
| 467 | |
| 468 | // Check source table permission |
| 469 | if s.no_permission_tables.contains(&source_key) { |
| 470 | let err = ErrorResponse::new( |
| 471 | Some("table".to_string()), |
| 472 | Some(request.source.object().to_string()), |
| 473 | Some("No Permission".to_string()), |
| 474 | Some(403), |
| 475 | ); |
| 476 | return (StatusCode::FORBIDDEN, Json(err)).into_response(); |
| 477 | } |
| 478 | |
| 479 | // Check if source table exists |
| 480 | if let Some(table_response) = s.tables.remove(&source_key) { |
| 481 | // Check if destination already exists |
| 482 | if s.tables.contains_key(&dest_key) { |
| 483 | // Restore source table |
| 484 | s.tables.insert(source_key, table_response); |
| 485 | let err = ErrorResponse::new( |
| 486 | Some("table".to_string()), |
| 487 | Some(dest_key.clone()), |
| 488 | Some("Already Exists".to_string()), |
| 489 | Some(409), |
| 490 | ); |
| 491 | return (StatusCode::CONFLICT, Json(err)).into_response(); |
| 492 | } |
| 493 | |
| 494 | // Update the table name in response and insert at new location |
| 495 | let new_table_response = GetTableResponse::new( |
| 496 | Some(request.destination.object().to_string()), |
| 497 | Some(request.destination.object().to_string()), |
| 498 | table_response.path, |
| 499 | table_response.is_external, |
| 500 | table_response.schema_id, |
| 501 | table_response.schema, |
| 502 | table_response.audit, |
| 503 | ); |
| 504 | s.tables.insert(dest_key.clone(), new_table_response); |
| 505 | |
| 506 | // Update permission tracking if needed |
| 507 | if s.no_permission_tables.remove(&source_key) { |
| 508 | s.no_permission_tables.insert(dest_key.clone()); |
| 509 | } |
| 510 | |
| 511 | (StatusCode::OK, Json(serde_json::json!(""))).into_response() |
| 512 | } else { |
no test coverage detected