table definition now has serialization deserialization properly implemented
This commit is contained in:
@@ -4,6 +4,7 @@ use tonic::Status;
|
||||
use sqlx::{PgPool, Transaction, Postgres};
|
||||
use serde_json::json;
|
||||
use common::proto::komp_ac::table_definition::{PostTableDefinitionRequest, TableDefinitionResponse};
|
||||
use common::proto::komp_ac::table_definition::ColumnDefinition;
|
||||
|
||||
// TODO CRITICAL add decimal with optional precision"
|
||||
const PREDEFINED_FIELD_TYPES: &[(&str, &str)] = &[
|
||||
@@ -299,20 +300,43 @@ async fn execute_table_definition(
|
||||
links.push((linked_id, link.required));
|
||||
}
|
||||
|
||||
let mut columns = Vec::new();
|
||||
let mut stored_columns = Vec::new();
|
||||
let mut sql_columns = Vec::new();
|
||||
|
||||
for col_def in request.columns.drain(..) {
|
||||
let col_name = col_def.name.trim().to_string();
|
||||
validate_identifier_format(&col_name, "Column name")?;
|
||||
|
||||
if col_name.ends_with("_id") || col_name == "id" || col_name == "deleted" || col_name == "created_at" {
|
||||
return Err(Status::invalid_argument(format!(
|
||||
"Column name '{}' cannot be 'id', 'deleted', 'created_at' or end with '_id'",
|
||||
col_name
|
||||
"Column name '{}' cannot be 'id', 'deleted', 'created_at' or end with '_id'",
|
||||
col_name
|
||||
)));
|
||||
}
|
||||
|
||||
let sql_type = map_field_type(&col_def.field_type)?;
|
||||
columns.push(format!("\"{}\" {}", col_name, sql_type));
|
||||
sql_columns.push(format!("\"{}\" {}", col_name, sql_type));
|
||||
|
||||
// push the proto type (serde serializable)
|
||||
stored_columns.push(ColumnDefinition {
|
||||
name: col_name,
|
||||
field_type: col_def.field_type,
|
||||
});
|
||||
}
|
||||
|
||||
// Indexes
|
||||
let mut stored_indexes = Vec::new();
|
||||
for idx in request.indexes.drain(..) {
|
||||
let idx_name = idx.trim().to_string();
|
||||
validate_identifier_format(&idx_name, "Index name")?;
|
||||
|
||||
if !sql_columns.iter().any(|c| c.starts_with(&format!("\"{}\"", idx_name))) {
|
||||
return Err(Status::invalid_argument(format!(
|
||||
"Index column '{}' not found", idx_name
|
||||
)));
|
||||
}
|
||||
|
||||
stored_indexes.push(idx_name);
|
||||
}
|
||||
|
||||
let mut indexes = Vec::new();
|
||||
@@ -320,13 +344,13 @@ async fn execute_table_definition(
|
||||
let idx_name = idx.trim().to_string();
|
||||
validate_identifier_format(&idx_name, "Index name")?;
|
||||
|
||||
if !columns.iter().any(|c| c.starts_with(&format!("\"{}\"", idx_name))) {
|
||||
if !sql_columns.iter().any(|c| c.starts_with(&format!("\"{}\"", idx_name))) {
|
||||
return Err(Status::invalid_argument(format!("Index column '{}' not found", idx_name)));
|
||||
}
|
||||
indexes.push(idx_name);
|
||||
}
|
||||
|
||||
let (create_sql, index_sql) = generate_table_sql(tx, &profile_name, &table_name, &columns, &indexes, &links).await?;
|
||||
let (create_sql, index_sql) = generate_table_sql(tx, &profile_name, &table_name, &sql_columns, &indexes, &links).await?;
|
||||
|
||||
// Use schema_id instead of profile_id
|
||||
let table_def = sqlx::query!(
|
||||
@@ -336,8 +360,8 @@ async fn execute_table_definition(
|
||||
RETURNING id"#,
|
||||
schema.id,
|
||||
&table_name,
|
||||
json!(columns),
|
||||
json!(indexes)
|
||||
serde_json::to_value(&stored_columns).unwrap(),
|
||||
serde_json::to_value(&stored_indexes).unwrap()
|
||||
)
|
||||
.fetch_one(&mut **tx)
|
||||
.await
|
||||
@@ -351,7 +375,7 @@ async fn execute_table_definition(
|
||||
Status::internal(format!("Database error: {}", e))
|
||||
})?;
|
||||
|
||||
for col_def in &columns {
|
||||
for col_def in &sql_columns {
|
||||
// Column string looks like "\"name\" TYPE", split out identifier
|
||||
let col_name = col_def.split_whitespace().next().unwrap_or("");
|
||||
let clean_col = col_name.trim_matches('"');
|
||||
|
||||
@@ -2,3 +2,6 @@
|
||||
|
||||
pub mod models;
|
||||
pub mod handlers;
|
||||
pub mod repo;
|
||||
|
||||
pub use repo::*;
|
||||
|
||||
33
server/src/table_definition/repo.rs
Normal file
33
server/src/table_definition/repo.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
// src/table_definition/repo.rs
|
||||
use common::proto::komp_ac::table_definition::ColumnDefinition;
|
||||
use sqlx::PgPool;
|
||||
|
||||
pub struct TableDefRow {
|
||||
pub id: i64,
|
||||
pub table_name: String,
|
||||
pub columns: Vec<ColumnDefinition>,
|
||||
pub indexes: Vec<String>,
|
||||
}
|
||||
|
||||
pub async fn get_table_definition(
|
||||
db: &PgPool,
|
||||
id: i64,
|
||||
) -> Result<TableDefRow, anyhow::Error> {
|
||||
let rec = sqlx::query!(
|
||||
r#"
|
||||
SELECT id, table_name, columns, indexes
|
||||
FROM table_definitions
|
||||
WHERE id = $1
|
||||
"#,
|
||||
id
|
||||
)
|
||||
.fetch_one(db)
|
||||
.await?;
|
||||
|
||||
Ok(TableDefRow {
|
||||
id: rec.id,
|
||||
table_name: rec.table_name,
|
||||
columns: serde_json::from_value(rec.columns)?, // 🔑
|
||||
indexes: serde_json::from_value(rec.indexes)?,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user