table definition now has serialization deserialization properly implemented
This commit is contained in:
@@ -29,6 +29,22 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
".komp_ac.table_validation.CountMode",
|
".komp_ac.table_validation.CountMode",
|
||||||
"#[derive(serde::Serialize, serde::Deserialize)] #[serde(rename_all = \"SCREAMING_SNAKE_CASE\")]",
|
"#[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(
|
.compile_protos(
|
||||||
&[
|
&[
|
||||||
"proto/common.proto",
|
"proto/common.proto",
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
// This file is @generated by prost-build.
|
// This file is @generated by prost-build.
|
||||||
|
#[derive(serde::Serialize, serde::Deserialize)]
|
||||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||||
pub struct TableLink {
|
pub struct TableLink {
|
||||||
#[prost(string, tag = "1")]
|
#[prost(string, tag = "1")]
|
||||||
@@ -6,6 +7,7 @@ pub struct TableLink {
|
|||||||
#[prost(bool, tag = "2")]
|
#[prost(bool, tag = "2")]
|
||||||
pub required: bool,
|
pub required: bool,
|
||||||
}
|
}
|
||||||
|
#[derive(serde::Serialize, serde::Deserialize)]
|
||||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||||
pub struct PostTableDefinitionRequest {
|
pub struct PostTableDefinitionRequest {
|
||||||
#[prost(string, tag = "1")]
|
#[prost(string, tag = "1")]
|
||||||
@@ -19,6 +21,7 @@ pub struct PostTableDefinitionRequest {
|
|||||||
#[prost(string, tag = "5")]
|
#[prost(string, tag = "5")]
|
||||||
pub profile_name: ::prost::alloc::string::String,
|
pub profile_name: ::prost::alloc::string::String,
|
||||||
}
|
}
|
||||||
|
#[derive(serde::Serialize, serde::Deserialize)]
|
||||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||||
pub struct ColumnDefinition {
|
pub struct ColumnDefinition {
|
||||||
#[prost(string, tag = "1")]
|
#[prost(string, tag = "1")]
|
||||||
@@ -26,6 +29,7 @@ pub struct ColumnDefinition {
|
|||||||
#[prost(string, tag = "2")]
|
#[prost(string, tag = "2")]
|
||||||
pub field_type: ::prost::alloc::string::String,
|
pub field_type: ::prost::alloc::string::String,
|
||||||
}
|
}
|
||||||
|
#[derive(serde::Serialize, serde::Deserialize)]
|
||||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||||
pub struct TableDefinitionResponse {
|
pub struct TableDefinitionResponse {
|
||||||
#[prost(bool, tag = "1")]
|
#[prost(bool, tag = "1")]
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ use tonic::Status;
|
|||||||
use sqlx::{PgPool, Transaction, Postgres};
|
use sqlx::{PgPool, Transaction, Postgres};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use common::proto::komp_ac::table_definition::{PostTableDefinitionRequest, TableDefinitionResponse};
|
use common::proto::komp_ac::table_definition::{PostTableDefinitionRequest, TableDefinitionResponse};
|
||||||
|
use common::proto::komp_ac::table_definition::ColumnDefinition;
|
||||||
|
|
||||||
// TODO CRITICAL add decimal with optional precision"
|
// TODO CRITICAL add decimal with optional precision"
|
||||||
const PREDEFINED_FIELD_TYPES: &[(&str, &str)] = &[
|
const PREDEFINED_FIELD_TYPES: &[(&str, &str)] = &[
|
||||||
@@ -299,20 +300,43 @@ async fn execute_table_definition(
|
|||||||
links.push((linked_id, link.required));
|
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(..) {
|
for col_def in request.columns.drain(..) {
|
||||||
let col_name = col_def.name.trim().to_string();
|
let col_name = col_def.name.trim().to_string();
|
||||||
validate_identifier_format(&col_name, "Column name")?;
|
validate_identifier_format(&col_name, "Column name")?;
|
||||||
|
|
||||||
if col_name.ends_with("_id") || col_name == "id" || col_name == "deleted" || col_name == "created_at" {
|
if col_name.ends_with("_id") || col_name == "id" || col_name == "deleted" || col_name == "created_at" {
|
||||||
return Err(Status::invalid_argument(format!(
|
return Err(Status::invalid_argument(format!(
|
||||||
"Column name '{}' cannot be 'id', 'deleted', 'created_at' or end with '_id'",
|
"Column name '{}' cannot be 'id', 'deleted', 'created_at' or end with '_id'",
|
||||||
col_name
|
col_name
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
let sql_type = map_field_type(&col_def.field_type)?;
|
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();
|
let mut indexes = Vec::new();
|
||||||
@@ -320,13 +344,13 @@ async fn execute_table_definition(
|
|||||||
let idx_name = idx.trim().to_string();
|
let idx_name = idx.trim().to_string();
|
||||||
validate_identifier_format(&idx_name, "Index name")?;
|
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)));
|
return Err(Status::invalid_argument(format!("Index column '{}' not found", idx_name)));
|
||||||
}
|
}
|
||||||
indexes.push(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
|
// Use schema_id instead of profile_id
|
||||||
let table_def = sqlx::query!(
|
let table_def = sqlx::query!(
|
||||||
@@ -336,8 +360,8 @@ async fn execute_table_definition(
|
|||||||
RETURNING id"#,
|
RETURNING id"#,
|
||||||
schema.id,
|
schema.id,
|
||||||
&table_name,
|
&table_name,
|
||||||
json!(columns),
|
serde_json::to_value(&stored_columns).unwrap(),
|
||||||
json!(indexes)
|
serde_json::to_value(&stored_indexes).unwrap()
|
||||||
)
|
)
|
||||||
.fetch_one(&mut **tx)
|
.fetch_one(&mut **tx)
|
||||||
.await
|
.await
|
||||||
@@ -351,7 +375,7 @@ async fn execute_table_definition(
|
|||||||
Status::internal(format!("Database error: {}", e))
|
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
|
// Column string looks like "\"name\" TYPE", split out identifier
|
||||||
let col_name = col_def.split_whitespace().next().unwrap_or("");
|
let col_name = col_def.split_whitespace().next().unwrap_or("");
|
||||||
let clean_col = col_name.trim_matches('"');
|
let clean_col = col_name.trim_matches('"');
|
||||||
|
|||||||
@@ -2,3 +2,6 @@
|
|||||||
|
|
||||||
pub mod models;
|
pub mod models;
|
||||||
pub mod handlers;
|
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