|
| 1 | +use std::{collections::HashMap, sync::Arc}; |
| 2 | + |
| 3 | +use axum::extract::{Extension, Json}; |
| 4 | +use columnq::error::ColumnQError; |
| 5 | +use columnq::table::TableSource; |
| 6 | +use log::info; |
| 7 | +use serde::Deserialize; |
| 8 | +use tokio::sync::Mutex; |
| 9 | + |
| 10 | +use crate::context::RoapiContext; |
| 11 | +use crate::error::ApiErrResp; |
| 12 | + |
| 13 | +#[derive(Debug, Deserialize)] |
| 14 | +pub struct SourceConfig { |
| 15 | + #[serde(rename = "tableName")] |
| 16 | + pub table_name: String, |
| 17 | +} |
| 18 | + |
| 19 | +pub async fn drop_table<H: RoapiContext>( |
| 20 | + Extension(ctx): Extension<Arc<H>>, |
| 21 | + Extension(tables): Extension<Arc<Mutex<HashMap<String, TableSource>>>>, |
| 22 | + Json(body): Json<Vec<SourceConfig>>, |
| 23 | +) -> Result<(), ApiErrResp> { |
| 24 | + let mut tables = tables.lock().await; |
| 25 | + for config in body { |
| 26 | + if let Some(t) = tables.get(&config.table_name) { |
| 27 | + info!("dropping table `{}`", t.name); |
| 28 | + ctx.drop_table(t) |
| 29 | + .await |
| 30 | + .map_err(ColumnQError::from) |
| 31 | + .map_err(ApiErrResp::drop_table)?; |
| 32 | + tables.remove(&config.table_name); |
| 33 | + info!("dropped table `{}`", config.table_name); |
| 34 | + } else { |
| 35 | + return Err(ApiErrResp::not_found(format!( |
| 36 | + "Table `{}` source does not exist", |
| 37 | + config.table_name |
| 38 | + ))); |
| 39 | + } |
| 40 | + } |
| 41 | + Ok(()) |
| 42 | +} |
| 43 | + |
| 44 | +pub async fn drop_table_read_only() -> Result<(), ApiErrResp> { |
| 45 | + Err(ApiErrResp::read_only_mode()) |
| 46 | +} |
0 commit comments