272 lines
9.9 KiB
Rust
272 lines
9.9 KiB
Rust
// src/tables_data/handlers/post_table_data.rs
|
|
|
|
use tonic::Status;
|
|
use sqlx::{PgPool, Arguments};
|
|
use sqlx::postgres::PgArguments;
|
|
use chrono::{DateTime, Utc};
|
|
use common::proto::multieko2::tables_data::{PostTableDataRequest, PostTableDataResponse};
|
|
use std::collections::HashMap;
|
|
use std::sync::Arc;
|
|
use crate::shared::schema_qualifier::qualify_table_name_for_data;
|
|
|
|
use crate::steel::server::execution::{self, Value};
|
|
use crate::steel::server::functions::SteelContext;
|
|
|
|
// Add these imports
|
|
use crate::indexer::{IndexCommand, IndexCommandData};
|
|
use tokio::sync::mpsc;
|
|
use tracing::error;
|
|
|
|
// MODIFIED: Function signature now accepts the indexer sender
|
|
pub async fn post_table_data(
|
|
db_pool: &PgPool,
|
|
request: PostTableDataRequest,
|
|
indexer_tx: &mpsc::Sender<IndexCommand>,
|
|
) -> Result<PostTableDataResponse, Status> {
|
|
let profile_name = request.profile_name;
|
|
let table_name = request.table_name;
|
|
let mut data = HashMap::new();
|
|
|
|
for (key, value) in request.data {
|
|
data.insert(key, value.trim().to_string());
|
|
}
|
|
|
|
// Lookup profile
|
|
let profile = sqlx::query!(
|
|
"SELECT id FROM profiles WHERE name = $1",
|
|
profile_name
|
|
)
|
|
.fetch_optional(db_pool)
|
|
.await
|
|
.map_err(|e| Status::internal(format!("Profile lookup error: {}", e)))?;
|
|
|
|
let profile_id = profile.ok_or_else(|| Status::not_found("Profile not found"))?.id;
|
|
|
|
// Lookup table_definition
|
|
let table_def = sqlx::query!(
|
|
r#"SELECT id, columns FROM table_definitions
|
|
WHERE profile_id = $1 AND table_name = $2"#,
|
|
profile_id,
|
|
table_name
|
|
)
|
|
.fetch_optional(db_pool)
|
|
.await
|
|
.map_err(|e| Status::internal(format!("Table lookup error: {}", e)))?;
|
|
|
|
let table_def = table_def.ok_or_else(|| Status::not_found("Table not found"))?;
|
|
|
|
// Parse columns from JSON
|
|
let columns_json: Vec<String> = serde_json::from_value(table_def.columns.clone())
|
|
.map_err(|e| Status::internal(format!("Column parsing error: {}", e)))?;
|
|
|
|
let mut columns = Vec::new();
|
|
for col_def in columns_json {
|
|
let parts: Vec<&str> = col_def.splitn(2, ' ').collect();
|
|
if parts.len() != 2 {
|
|
return Err(Status::internal("Invalid column format"));
|
|
}
|
|
let name = parts[0].trim_matches('"').to_string();
|
|
let sql_type = parts[1].to_string();
|
|
columns.push((name, sql_type));
|
|
}
|
|
|
|
// Get all foreign key columns for this table
|
|
let fk_columns = sqlx::query!(
|
|
r#"SELECT ltd.table_name
|
|
FROM table_definition_links tdl
|
|
JOIN table_definitions ltd ON tdl.linked_table_id = ltd.id
|
|
WHERE tdl.source_table_id = $1"#,
|
|
table_def.id
|
|
)
|
|
.fetch_all(db_pool)
|
|
.await
|
|
.map_err(|e| Status::internal(format!("Foreign key lookup error: {}", e)))?;
|
|
|
|
// Build system columns with foreign keys
|
|
let mut system_columns = vec!["deleted".to_string()];
|
|
for fk in fk_columns {
|
|
let base_name = fk.table_name.split('_').last().unwrap_or(&fk.table_name);
|
|
system_columns.push(format!("{}_id", base_name));
|
|
}
|
|
|
|
// Convert to HashSet for faster lookups
|
|
let system_columns_set: std::collections::HashSet<_> = system_columns.iter().map(|s| s.as_str()).collect();
|
|
|
|
// Validate all data columns
|
|
let user_columns: Vec<&String> = columns.iter().map(|(name, _)| name).collect();
|
|
for key in data.keys() {
|
|
if !system_columns_set.contains(key.as_str()) &&
|
|
!user_columns.contains(&&key.to_string()) {
|
|
return Err(Status::invalid_argument(format!("Invalid column: {}", key)));
|
|
}
|
|
}
|
|
|
|
// Validate Steel scripts
|
|
let scripts = sqlx::query!(
|
|
"SELECT target_column, script FROM table_scripts WHERE table_definitions_id = $1",
|
|
table_def.id
|
|
)
|
|
.fetch_all(db_pool)
|
|
.await
|
|
.map_err(|e| Status::internal(format!("Failed to fetch scripts: {}", e)))?;
|
|
|
|
for script_record in scripts {
|
|
let target_column = script_record.target_column;
|
|
|
|
// Ensure target column exists in submitted data
|
|
let user_value = data.get(&target_column)
|
|
.ok_or_else(|| Status::invalid_argument(
|
|
format!("Script target column '{}' is required", target_column)
|
|
))?;
|
|
|
|
// Create execution context
|
|
let context = SteelContext {
|
|
current_table: table_name.clone(), // Keep base name for scripts
|
|
profile_id,
|
|
row_data: data.clone(),
|
|
db_pool: Arc::new(db_pool.clone()),
|
|
};
|
|
|
|
// Execute validation script
|
|
let script_result = execution::execute_script(
|
|
script_record.script,
|
|
"STRINGS",
|
|
Arc::new(db_pool.clone()),
|
|
context,
|
|
)
|
|
.map_err(|e| Status::invalid_argument(
|
|
format!("Script execution failed for '{}': {}", target_column, e)
|
|
))?;
|
|
|
|
// Validate script output
|
|
let Value::Strings(mut script_output) = script_result else {
|
|
return Err(Status::internal("Script must return string values"));
|
|
};
|
|
|
|
let expected_value = script_output.pop()
|
|
.ok_or_else(|| Status::internal("Script returned no values"))?;
|
|
|
|
if user_value != &expected_value {
|
|
return Err(Status::invalid_argument(format!(
|
|
"Validation failed for column '{}': Expected '{}', Got '{}'",
|
|
target_column, expected_value, user_value
|
|
)));
|
|
}
|
|
}
|
|
|
|
// Prepare SQL parameters
|
|
let mut params = PgArguments::default();
|
|
let mut columns_list = Vec::new();
|
|
let mut placeholders = Vec::new();
|
|
let mut param_idx = 1;
|
|
|
|
for (col, value) in data {
|
|
let sql_type = if system_columns_set.contains(col.as_str()) {
|
|
match col.as_str() {
|
|
"deleted" => "BOOLEAN",
|
|
_ if col.ends_with("_id") => "BIGINT", // Handle foreign keys
|
|
_ => return Err(Status::invalid_argument("Invalid system column")),
|
|
}
|
|
} else {
|
|
columns.iter()
|
|
.find(|(name, _)| name == &col)
|
|
.map(|(_, sql_type)| sql_type.as_str())
|
|
.ok_or_else(|| Status::invalid_argument(format!("Column not found: {}", col)))?
|
|
};
|
|
|
|
match sql_type {
|
|
"TEXT" | "VARCHAR(15)" | "VARCHAR(255)" => {
|
|
if let Some(max_len) = sql_type.strip_prefix("VARCHAR(")
|
|
.and_then(|s| s.strip_suffix(')'))
|
|
.and_then(|s| s.parse::<usize>().ok())
|
|
{
|
|
if value.len() > max_len {
|
|
return Err(Status::internal(format!("Value too long for {}", col)));
|
|
}
|
|
}
|
|
params.add(value)
|
|
.map_err(|e| Status::invalid_argument(format!("Failed to add text parameter for {}: {}", col, e)))?;
|
|
},
|
|
"BOOLEAN" => {
|
|
let val = value.parse::<bool>()
|
|
.map_err(|_| Status::invalid_argument(format!("Invalid boolean for {}", col)))?;
|
|
params.add(val)
|
|
.map_err(|e| Status::invalid_argument(format!("Failed to add boolean parameter for {}: {}", col, e)))?;
|
|
},
|
|
"TIMESTAMPTZ" => {
|
|
let dt = DateTime::parse_from_rfc3339(&value)
|
|
.map_err(|_| Status::invalid_argument(format!("Invalid timestamp for {}", col)))?;
|
|
params.add(dt.with_timezone(&Utc))
|
|
.map_err(|e| Status::invalid_argument(format!("Failed to add timestamp parameter for {}: {}", col, e)))?;
|
|
},
|
|
"BIGINT" => {
|
|
let val = value.parse::<i64>()
|
|
.map_err(|_| Status::invalid_argument(format!("Invalid integer for {}", col)))?;
|
|
params.add(val)
|
|
.map_err(|e| Status::invalid_argument(format!("Failed to add integer parameter for {}: {}", col, e)))?;
|
|
},
|
|
_ => return Err(Status::invalid_argument(format!("Unsupported type {}", sql_type))),
|
|
}
|
|
|
|
columns_list.push(format!("\"{}\"", col));
|
|
placeholders.push(format!("${}", param_idx));
|
|
param_idx += 1;
|
|
}
|
|
|
|
if columns_list.is_empty() {
|
|
return Err(Status::invalid_argument("No valid columns to insert"));
|
|
}
|
|
|
|
// Qualify table name with schema
|
|
let qualified_table = qualify_table_name_for_data(&table_name)?;
|
|
|
|
let sql = format!(
|
|
"INSERT INTO {} ({}) VALUES ({}) RETURNING id",
|
|
qualified_table,
|
|
columns_list.join(", "),
|
|
placeholders.join(", ")
|
|
);
|
|
|
|
// Execute query with enhanced error handling
|
|
let result = sqlx::query_scalar_with::<_, i64, _>(&sql, params)
|
|
.fetch_one(db_pool)
|
|
.await;
|
|
|
|
let inserted_id = match result {
|
|
Ok(id) => id,
|
|
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 {}",
|
|
table_name, qualified_table
|
|
)));
|
|
}
|
|
}
|
|
return Err(Status::internal(format!("Insert failed: {}", e)));
|
|
}
|
|
};
|
|
|
|
// After a successful insert, send a command to the indexer.
|
|
let command = IndexCommand::AddOrUpdate(IndexCommandData {
|
|
table_name: table_name.clone(),
|
|
row_id: inserted_id,
|
|
});
|
|
|
|
if let Err(e) = indexer_tx.send(command).await {
|
|
// If sending fails, the DB is updated but the index will be stale.
|
|
// This is a critical situation to log and monitor.
|
|
error!(
|
|
"CRITICAL: DB insert for table '{}' (id: {}) succeeded but failed to queue for indexing: {}. Search index is now inconsistent.",
|
|
table_name, inserted_id, e
|
|
);
|
|
}
|
|
|
|
Ok(PostTableDataResponse {
|
|
success: true,
|
|
message: "Data inserted successfully".into(),
|
|
inserted_id,
|
|
})
|
|
}
|