From 2435f582568fe639fb44339771d482d8dc3db38e Mon Sep 17 00:00:00 2001 From: Priec Date: Tue, 16 Sep 2025 22:55:49 +0200 Subject: [PATCH] table definition now has serialization deserialization properly implemented --- common/build.rs | 16 +++++++ common/src/proto/komp_ac.table_definition.rs | 4 ++ .../handlers/post_table_definition.rs | 42 +++++++++++++++---- server/src/table_definition/mod.rs | 3 ++ server/src/table_definition/repo.rs | 33 +++++++++++++++ 5 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 server/src/table_definition/repo.rs diff --git a/common/build.rs b/common/build.rs index e7d026a..094cd8c 100644 --- a/common/build.rs +++ b/common/build.rs @@ -29,6 +29,22 @@ fn main() -> Result<(), Box> { ".komp_ac.table_validation.CountMode", "#[derive(serde::Serialize, serde::Deserialize)] #[serde(rename_all = \"SCREAMING_SNAKE_CASE\")]", ) + .type_attribute( + ".komp_ac.table_definition.ColumnDefinition", + "#[derive(serde::Serialize, serde::Deserialize)]", + ) + .type_attribute( + ".komp_ac.table_definition.TableLink", + "#[derive(serde::Serialize, serde::Deserialize)]" + ) + .type_attribute( + ".komp_ac.table_definition.PostTableDefinitionRequest", + "#[derive(serde::Serialize, serde::Deserialize)]", + ) + .type_attribute( + ".komp_ac.table_definition.TableDefinitionResponse", + "#[derive(serde::Serialize, serde::Deserialize)]" + ) .compile_protos( &[ "proto/common.proto", diff --git a/common/src/proto/komp_ac.table_definition.rs b/common/src/proto/komp_ac.table_definition.rs index 8038507..71137c6 100644 --- a/common/src/proto/komp_ac.table_definition.rs +++ b/common/src/proto/komp_ac.table_definition.rs @@ -1,4 +1,5 @@ // This file is @generated by prost-build. +#[derive(serde::Serialize, serde::Deserialize)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct TableLink { #[prost(string, tag = "1")] @@ -6,6 +7,7 @@ pub struct TableLink { #[prost(bool, tag = "2")] pub required: bool, } +#[derive(serde::Serialize, serde::Deserialize)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct PostTableDefinitionRequest { #[prost(string, tag = "1")] @@ -19,6 +21,7 @@ pub struct PostTableDefinitionRequest { #[prost(string, tag = "5")] pub profile_name: ::prost::alloc::string::String, } +#[derive(serde::Serialize, serde::Deserialize)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct ColumnDefinition { #[prost(string, tag = "1")] @@ -26,6 +29,7 @@ pub struct ColumnDefinition { #[prost(string, tag = "2")] pub field_type: ::prost::alloc::string::String, } +#[derive(serde::Serialize, serde::Deserialize)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct TableDefinitionResponse { #[prost(bool, tag = "1")] diff --git a/server/src/table_definition/handlers/post_table_definition.rs b/server/src/table_definition/handlers/post_table_definition.rs index 2413df1..1b376a1 100644 --- a/server/src/table_definition/handlers/post_table_definition.rs +++ b/server/src/table_definition/handlers/post_table_definition.rs @@ -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('"'); diff --git a/server/src/table_definition/mod.rs b/server/src/table_definition/mod.rs index 4347c98..379d89f 100644 --- a/server/src/table_definition/mod.rs +++ b/server/src/table_definition/mod.rs @@ -2,3 +2,6 @@ pub mod models; pub mod handlers; +pub mod repo; + +pub use repo::*; diff --git a/server/src/table_definition/repo.rs b/server/src/table_definition/repo.rs new file mode 100644 index 0000000..b8dc931 --- /dev/null +++ b/server/src/table_definition/repo.rs @@ -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, + pub indexes: Vec, +} + +pub async fn get_table_definition( + db: &PgPool, + id: i64, +) -> Result { + 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)?, + }) +}