now working with the gen schema in the database
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
use tonic::Status;
|
||||
use sqlx::PgPool;
|
||||
use common::proto::multieko2::tables_data::{DeleteTableDataRequest, DeleteTableDataResponse};
|
||||
use crate::shared::schema_qualifier::qualify_table_name_for_data; // Import schema qualifier
|
||||
|
||||
pub async fn delete_table_data(
|
||||
db_pool: &PgPool,
|
||||
@@ -36,20 +37,37 @@ pub async fn delete_table_data(
|
||||
return Err(Status::not_found("Table not found in profile"));
|
||||
}
|
||||
|
||||
// Perform soft delete
|
||||
// Qualify table name with schema
|
||||
let qualified_table = qualify_table_name_for_data(&request.table_name)?;
|
||||
|
||||
// Perform soft delete using qualified table name
|
||||
let query = format!(
|
||||
"UPDATE \"{}\"
|
||||
"UPDATE {}
|
||||
SET deleted = true
|
||||
WHERE id = $1 AND deleted = false",
|
||||
request.table_name
|
||||
qualified_table
|
||||
);
|
||||
|
||||
let rows_affected = sqlx::query(&query)
|
||||
let result = sqlx::query(&query)
|
||||
.bind(request.record_id)
|
||||
.execute(db_pool)
|
||||
.await
|
||||
.map_err(|e| Status::internal(format!("Delete operation failed: {}", e)))?
|
||||
.rows_affected();
|
||||
.await;
|
||||
|
||||
let rows_affected = match result {
|
||||
Ok(result) => result.rows_affected(),
|
||||
Err(e) => {
|
||||
// Handle "relation does not exist" error specifically
|
||||
if let Some(db_err) = e.as_database_error() {
|
||||
if db_err.code() == Some(std::borrow::Cow::Borrowed("42P01")) {
|
||||
return Err(Status::internal(format!(
|
||||
"Table '{}' is defined but does not physically exist in the database as {}",
|
||||
request.table_name, qualified_table
|
||||
)));
|
||||
}
|
||||
}
|
||||
return Err(Status::internal(format!("Delete operation failed: {}", e)));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(DeleteTableDataResponse {
|
||||
success: rows_affected > 0,
|
||||
|
||||
Reference in New Issue
Block a user