delete table
This commit is contained in:
@@ -7,6 +7,7 @@ import "common.proto";
|
|||||||
service TableDefinition {
|
service TableDefinition {
|
||||||
rpc PostTableDefinition (PostTableDefinitionRequest) returns (TableDefinitionResponse);
|
rpc PostTableDefinition (PostTableDefinitionRequest) returns (TableDefinitionResponse);
|
||||||
rpc GetProfileTree (multieko2.common.Empty) returns (ProfileTreeResponse);
|
rpc GetProfileTree (multieko2.common.Empty) returns (ProfileTreeResponse);
|
||||||
|
rpc DeleteTable (DeleteTableRequest) returns (DeleteTableResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
message PostTableDefinitionRequest {
|
message PostTableDefinitionRequest {
|
||||||
@@ -40,3 +41,13 @@ message ProfileTreeResponse {
|
|||||||
|
|
||||||
repeated Profile profiles = 1;
|
repeated Profile profiles = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
message DeleteTableRequest {
|
||||||
|
string profile_name = 1;
|
||||||
|
string table_name = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message DeleteTableResponse {
|
||||||
|
bool success = 1;
|
||||||
|
string message = 2;
|
||||||
|
}
|
||||||
|
|||||||
Binary file not shown.
@@ -48,6 +48,20 @@ pub mod profile_tree_response {
|
|||||||
pub tables: ::prost::alloc::vec::Vec<Table>,
|
pub tables: ::prost::alloc::vec::Vec<Table>,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||||
|
pub struct DeleteTableRequest {
|
||||||
|
#[prost(string, tag = "1")]
|
||||||
|
pub profile_name: ::prost::alloc::string::String,
|
||||||
|
#[prost(string, tag = "2")]
|
||||||
|
pub table_name: ::prost::alloc::string::String,
|
||||||
|
}
|
||||||
|
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||||
|
pub struct DeleteTableResponse {
|
||||||
|
#[prost(bool, tag = "1")]
|
||||||
|
pub success: bool,
|
||||||
|
#[prost(string, tag = "2")]
|
||||||
|
pub message: ::prost::alloc::string::String,
|
||||||
|
}
|
||||||
/// Generated client implementations.
|
/// Generated client implementations.
|
||||||
pub mod table_definition_client {
|
pub mod table_definition_client {
|
||||||
#![allow(
|
#![allow(
|
||||||
@@ -197,6 +211,35 @@ pub mod table_definition_client {
|
|||||||
);
|
);
|
||||||
self.inner.unary(req, path, codec).await
|
self.inner.unary(req, path, codec).await
|
||||||
}
|
}
|
||||||
|
pub async fn delete_table(
|
||||||
|
&mut self,
|
||||||
|
request: impl tonic::IntoRequest<super::DeleteTableRequest>,
|
||||||
|
) -> std::result::Result<
|
||||||
|
tonic::Response<super::DeleteTableResponse>,
|
||||||
|
tonic::Status,
|
||||||
|
> {
|
||||||
|
self.inner
|
||||||
|
.ready()
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
tonic::Status::unknown(
|
||||||
|
format!("Service was not ready: {}", e.into()),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
let codec = tonic::codec::ProstCodec::default();
|
||||||
|
let path = http::uri::PathAndQuery::from_static(
|
||||||
|
"/multieko2.table_definition.TableDefinition/DeleteTable",
|
||||||
|
);
|
||||||
|
let mut req = request.into_request();
|
||||||
|
req.extensions_mut()
|
||||||
|
.insert(
|
||||||
|
GrpcMethod::new(
|
||||||
|
"multieko2.table_definition.TableDefinition",
|
||||||
|
"DeleteTable",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
self.inner.unary(req, path, codec).await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Generated server implementations.
|
/// Generated server implementations.
|
||||||
@@ -226,6 +269,13 @@ pub mod table_definition_server {
|
|||||||
tonic::Response<super::ProfileTreeResponse>,
|
tonic::Response<super::ProfileTreeResponse>,
|
||||||
tonic::Status,
|
tonic::Status,
|
||||||
>;
|
>;
|
||||||
|
async fn delete_table(
|
||||||
|
&self,
|
||||||
|
request: tonic::Request<super::DeleteTableRequest>,
|
||||||
|
) -> std::result::Result<
|
||||||
|
tonic::Response<super::DeleteTableResponse>,
|
||||||
|
tonic::Status,
|
||||||
|
>;
|
||||||
}
|
}
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct TableDefinitionServer<T> {
|
pub struct TableDefinitionServer<T> {
|
||||||
@@ -398,6 +448,51 @@ pub mod table_definition_server {
|
|||||||
};
|
};
|
||||||
Box::pin(fut)
|
Box::pin(fut)
|
||||||
}
|
}
|
||||||
|
"/multieko2.table_definition.TableDefinition/DeleteTable" => {
|
||||||
|
#[allow(non_camel_case_types)]
|
||||||
|
struct DeleteTableSvc<T: TableDefinition>(pub Arc<T>);
|
||||||
|
impl<
|
||||||
|
T: TableDefinition,
|
||||||
|
> tonic::server::UnaryService<super::DeleteTableRequest>
|
||||||
|
for DeleteTableSvc<T> {
|
||||||
|
type Response = super::DeleteTableResponse;
|
||||||
|
type Future = BoxFuture<
|
||||||
|
tonic::Response<Self::Response>,
|
||||||
|
tonic::Status,
|
||||||
|
>;
|
||||||
|
fn call(
|
||||||
|
&mut self,
|
||||||
|
request: tonic::Request<super::DeleteTableRequest>,
|
||||||
|
) -> Self::Future {
|
||||||
|
let inner = Arc::clone(&self.0);
|
||||||
|
let fut = async move {
|
||||||
|
<T as TableDefinition>::delete_table(&inner, request).await
|
||||||
|
};
|
||||||
|
Box::pin(fut)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let accept_compression_encodings = self.accept_compression_encodings;
|
||||||
|
let send_compression_encodings = self.send_compression_encodings;
|
||||||
|
let max_decoding_message_size = self.max_decoding_message_size;
|
||||||
|
let max_encoding_message_size = self.max_encoding_message_size;
|
||||||
|
let inner = self.inner.clone();
|
||||||
|
let fut = async move {
|
||||||
|
let method = DeleteTableSvc(inner);
|
||||||
|
let codec = tonic::codec::ProstCodec::default();
|
||||||
|
let mut grpc = tonic::server::Grpc::new(codec)
|
||||||
|
.apply_compression_config(
|
||||||
|
accept_compression_encodings,
|
||||||
|
send_compression_encodings,
|
||||||
|
)
|
||||||
|
.apply_max_message_size_config(
|
||||||
|
max_decoding_message_size,
|
||||||
|
max_encoding_message_size,
|
||||||
|
);
|
||||||
|
let res = grpc.unary(method, req).await;
|
||||||
|
Ok(res)
|
||||||
|
};
|
||||||
|
Box::pin(fut)
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let mut response = http::Response::new(empty_body());
|
let mut response = http::Response::new(empty_body());
|
||||||
|
|||||||
@@ -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 tonic::{Request, Response, Status};
|
||||||
use common::proto::multieko2::{
|
use common::proto::multieko2::{
|
||||||
common::Empty,
|
common::Empty,
|
||||||
table_definition::{
|
table_definition::{
|
||||||
table_definition_server::TableDefinition,
|
table_definition_server::TableDefinition,
|
||||||
PostTableDefinitionRequest, TableDefinitionResponse, ProfileTreeResponse
|
PostTableDefinitionRequest, TableDefinitionResponse,
|
||||||
}
|
ProfileTreeResponse, DeleteTableRequest, DeleteTableResponse,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
use sqlx::PgPool;
|
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)]
|
#[derive(Debug)]
|
||||||
pub struct TableDefinitionService {
|
pub struct TableDefinitionService {
|
||||||
pub db_pool: PgPool,
|
pub db_pool: PgPool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TableDefinitionService {
|
||||||
|
pub fn new(db_pool: PgPool) -> Self {
|
||||||
|
Self { db_pool }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tonic::async_trait]
|
#[tonic::async_trait]
|
||||||
impl TableDefinition for TableDefinitionService {
|
impl TableDefinition for TableDefinitionService {
|
||||||
async fn post_table_definition(
|
async fn post_table_definition(
|
||||||
@@ -27,8 +34,16 @@ impl TableDefinition for TableDefinitionService {
|
|||||||
|
|
||||||
async fn get_profile_tree(
|
async fn get_profile_tree(
|
||||||
&self,
|
&self,
|
||||||
request: Request<Empty>, // Changed from Request<()>
|
request: Request<Empty>,
|
||||||
) -> Result<Response<ProfileTreeResponse>, Status> {
|
) -> Result<Response<ProfileTreeResponse>, Status> {
|
||||||
get_profile_tree::get_profile_tree(&self.db_pool, request).await
|
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))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
// server/src/table_definition/handlers.rs
|
// server/src/table_definition/handlers.rs
|
||||||
pub mod post_table_definition;
|
pub mod post_table_definition;
|
||||||
pub mod get_profile_tree;
|
pub mod get_profile_tree;
|
||||||
|
pub mod delete_table;
|
||||||
|
|
||||||
pub use post_table_definition::post_table_definition;
|
pub use post_table_definition::post_table_definition;
|
||||||
pub use get_profile_tree::get_profile_tree;
|
pub use get_profile_tree::get_profile_tree;
|
||||||
|
pub use delete_table::delete_table;
|
||||||
|
|||||||
84
server/src/table_definition/handlers/delete_table.rs
Normal file
84
server/src/table_definition/handlers/delete_table.rs
Normal 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),
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -101,15 +101,6 @@ pub async fn post_table_definition(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Process indexes without year prefix
|
// 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();
|
let mut indexes = Vec::new();
|
||||||
for idx in request.indexes.drain(..) {
|
for idx in request.indexes.drain(..) {
|
||||||
let idx_name = sanitize_identifier(&idx);
|
let idx_name = sanitize_identifier(&idx);
|
||||||
|
|||||||
Reference in New Issue
Block a user