delete table

This commit is contained in:
filipriec
2025-03-03 15:15:30 +01:00
parent 9e0881032f
commit f9cf54e67c
7 changed files with 212 additions and 14 deletions

View File

@@ -1,20 +1,27 @@
// server/src/server/services/table_definition_service.rs
// src/server/services/table_definition_service.rs
use tonic::{Request, Response, Status};
use common::proto::multieko2::{
common::Empty,
table_definition::{
table_definition_server::TableDefinition,
PostTableDefinitionRequest, TableDefinitionResponse, ProfileTreeResponse
}
PostTableDefinitionRequest, TableDefinitionResponse,
ProfileTreeResponse, DeleteTableRequest, DeleteTableResponse,
},
};
use sqlx::PgPool;
use crate::table_definition::handlers::{post_table_definition, get_profile_tree};
use crate::table_definition::handlers::{post_table_definition, get_profile_tree, delete_table};
#[derive(Debug)]
pub struct TableDefinitionService {
pub db_pool: PgPool,
}
impl TableDefinitionService {
pub fn new(db_pool: PgPool) -> Self {
Self { db_pool }
}
}
#[tonic::async_trait]
impl TableDefinition for TableDefinitionService {
async fn post_table_definition(
@@ -27,8 +34,16 @@ impl TableDefinition for TableDefinitionService {
async fn get_profile_tree(
&self,
request: Request<Empty>, // Changed from Request<()>
request: Request<Empty>,
) -> Result<Response<ProfileTreeResponse>, Status> {
get_profile_tree::get_profile_tree(&self.db_pool, request).await
}
async fn delete_table(
&self,
request: Request<DeleteTableRequest>,
) -> Result<Response<DeleteTableResponse>, Status> {
let response = delete_table(&self.db_pool, request.into_inner()).await?;
Ok(Response::new(response))
}
}

View File

@@ -1,6 +1,8 @@
// server/src/table_definition/handlers.rs
pub mod post_table_definition;
pub mod get_profile_tree;
pub mod delete_table;
pub use post_table_definition::post_table_definition;
pub use get_profile_tree::get_profile_tree;
pub use delete_table::delete_table;

View File

@@ -0,0 +1,84 @@
// server/src/table_definition/handlers/delete_table.rs
use tonic::Status;
use sqlx::{PgPool, Postgres, Transaction};
use common::proto::multieko2::table_definition::{DeleteTableRequest, DeleteTableResponse};
pub async fn delete_table(
db_pool: &PgPool,
request: DeleteTableRequest,
) -> Result<DeleteTableResponse, Status> {
let mut transaction = db_pool.begin().await
.map_err(|e| Status::internal(format!("Failed to start transaction: {}", e)))?;
// Step 1: Get profile and validate existence
let profile = sqlx::query!(
"SELECT id FROM profiles WHERE name = $1",
request.profile_name
)
.fetch_optional(&mut *transaction)
.await
.map_err(|e| Status::internal(format!("Profile lookup failed: {}", e)))?;
let profile_id = match profile {
Some(p) => p.id,
None => return Err(Status::not_found("Profile not found")),
};
// Step 2: Get table definition and validate existence
let table_def = sqlx::query!(
"SELECT id FROM table_definitions
WHERE profile_id = $1 AND table_name = $2",
profile_id,
request.table_name
)
.fetch_optional(&mut *transaction)
.await
.map_err(|e| Status::internal(format!("Table lookup failed: {}", e)))?;
let table_def_id = match table_def {
Some(t) => t.id,
None => return Err(Status::not_found("Table not found in profile")),
};
// Step 3: Drop the actual PostgreSQL table with CASCADE
sqlx::query(&format!(r#"DROP TABLE IF EXISTS "{}" CASCADE"#, request.table_name))
.execute(&mut *transaction)
.await
.map_err(|e| Status::internal(format!("Table drop failed: {}", e)))?;
// Step 4: Delete from table_definitions
sqlx::query!(
"DELETE FROM table_definitions WHERE id = $1",
table_def_id
)
.execute(&mut *transaction)
.await
.map_err(|e| Status::internal(format!("Definition deletion failed: {}", e)))?;
// Step 5: Check and clean up profile if empty
let remaining = sqlx::query!(
"SELECT COUNT(*) as count FROM table_definitions WHERE profile_id = $1",
profile_id
)
.fetch_one(&mut *transaction)
.await
.map_err(|e| Status::internal(format!("Count query failed: {}", e)))?;
if remaining.count.unwrap_or(1) == 0 {
sqlx::query!(
"DELETE FROM profiles WHERE id = $1",
profile_id
)
.execute(&mut *transaction)
.await
.map_err(|e| Status::internal(format!("Profile cleanup failed: {}", e)))?;
}
transaction.commit().await
.map_err(|e| Status::internal(format!("Transaction commit failed: {}", e)))?;
Ok(DeleteTableResponse {
success: true,
message: format!("Table '{}' and its definition were successfully removed", request.table_name),
})
}

View File

@@ -101,15 +101,6 @@ pub async fn post_table_definition(
}
// Process indexes without year prefix
let mut indexes = Vec::new();
for idx in request.indexes.drain(..) {
let idx_name = sanitize_identifier(&idx); // No year prefix
if !is_valid_identifier(&idx) {
return Err(Status::invalid_argument(format!("Invalid index name: {}", idx)));
}
indexes.push(idx_name);
}
let mut indexes = Vec::new();
for idx in request.indexes.drain(..) {
let idx_name = sanitize_identifier(&idx);