working properly well creation of profiles and creation of tables by user
This commit is contained in:
Binary file not shown.
@@ -3,10 +3,21 @@
|
|||||||
pub struct PostTableDefinitionRequest {
|
pub struct PostTableDefinitionRequest {
|
||||||
#[prost(string, tag = "1")]
|
#[prost(string, tag = "1")]
|
||||||
pub table_name: ::prost::alloc::string::String,
|
pub table_name: ::prost::alloc::string::String,
|
||||||
#[prost(string, repeated, tag = "2")]
|
#[prost(message, repeated, tag = "2")]
|
||||||
pub columns: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
|
pub columns: ::prost::alloc::vec::Vec<ColumnDefinition>,
|
||||||
#[prost(string, repeated, tag = "3")]
|
#[prost(string, repeated, tag = "3")]
|
||||||
pub indexes: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
|
pub indexes: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
|
||||||
|
#[prost(string, tag = "4")]
|
||||||
|
pub profile_name: ::prost::alloc::string::String,
|
||||||
|
#[prost(string, optional, tag = "5")]
|
||||||
|
pub linked_table_name: ::core::option::Option<::prost::alloc::string::String>,
|
||||||
|
}
|
||||||
|
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||||
|
pub struct ColumnDefinition {
|
||||||
|
#[prost(string, tag = "1")]
|
||||||
|
pub name: ::prost::alloc::string::String,
|
||||||
|
#[prost(string, tag = "2")]
|
||||||
|
pub data_type: ::prost::alloc::string::String,
|
||||||
}
|
}
|
||||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||||
pub struct TableDefinitionResponse {
|
pub struct TableDefinitionResponse {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
CREATE TABLE table_definitions (
|
CREATE TABLE table_definitions (
|
||||||
id BIGSERIAL PRIMARY KEY,
|
id BIGSERIAL PRIMARY KEY,
|
||||||
deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
table_name TEXT NOT NULL UNIQUE,
|
table_name TEXT NOT NULL,
|
||||||
columns JSONB NOT NULL,
|
columns JSONB NOT NULL,
|
||||||
indexes JSONB NOT NULL,
|
indexes JSONB NOT NULL,
|
||||||
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
|||||||
@@ -1,17 +1,15 @@
|
|||||||
// src/table_definition/handlers/post_table_definition.rs
|
// src/table_definition/handlers/post_table_definition.rs
|
||||||
use tonic::Status;
|
use tonic::Status;
|
||||||
use sqlx::{PgPool, Row};
|
use sqlx::PgPool;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use common::proto::multieko2::table_definition::{PostTableDefinitionRequest, TableDefinitionResponse};
|
use common::proto::multieko2::table_definition::{PostTableDefinitionRequest, TableDefinitionResponse};
|
||||||
|
|
||||||
const VALID_DATA_TYPES: &[&str] = &["TEXT", "INTEGER", "BIGINT", "BOOLEAN", "TIMESTAMPTZ", "NUMERIC"];
|
const VALID_DATA_TYPES: &[&str] = &["TEXT", "INTEGER", "BIGINT", "BOOLEAN", "TIMESTAMPTZ", "NUMERIC"];
|
||||||
|
|
||||||
// Add to validation section
|
|
||||||
fn is_valid_data_type(dt: &str) -> bool {
|
fn is_valid_data_type(dt: &str) -> bool {
|
||||||
VALID_DATA_TYPES.contains(&dt.to_uppercase().as_str())
|
VALID_DATA_TYPES.contains(&dt.to_uppercase().as_str())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate SQL identifiers
|
|
||||||
fn is_valid_identifier(s: &str) -> bool {
|
fn is_valid_identifier(s: &str) -> bool {
|
||||||
!s.is_empty() &&
|
!s.is_empty() &&
|
||||||
s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') &&
|
s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') &&
|
||||||
@@ -19,7 +17,6 @@ fn is_valid_identifier(s: &str) -> bool {
|
|||||||
!s.chars().next().unwrap().is_ascii_digit()
|
!s.chars().next().unwrap().is_ascii_digit()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sanitize SQL identifiers
|
|
||||||
fn sanitize_identifier(s: &str) -> String {
|
fn sanitize_identifier(s: &str) -> String {
|
||||||
s.replace(|c: char| !c.is_ascii_alphanumeric() && c != '_', "")
|
s.replace(|c: char| !c.is_ascii_alphanumeric() && c != '_', "")
|
||||||
.trim()
|
.trim()
|
||||||
@@ -48,29 +45,31 @@ pub async fn post_table_definition(
|
|||||||
.map_err(|e| Status::internal(format!("Profile error: {}", e)))?;
|
.map_err(|e| Status::internal(format!("Profile error: {}", e)))?;
|
||||||
|
|
||||||
// Validate linked table if provided
|
// Validate linked table if provided
|
||||||
let linked_table_id = if let Some(linked_table) = &request.linked_table_name {
|
let linked_table_id;
|
||||||
let lt = sqlx::query!(
|
let linked_table_name = request.linked_table_name
|
||||||
"SELECT id FROM table_definitions
|
.as_ref()
|
||||||
WHERE profile_id = $1 AND table_name = $2",
|
.map(|lt| sanitize_identifier(lt));
|
||||||
|
|
||||||
|
if let Some(lt_name) = &linked_table_name {
|
||||||
|
let lt_record = sqlx::query!(
|
||||||
|
"SELECT id FROM table_definitions
|
||||||
|
WHERE profile_id = $1 AND table_name = $2",
|
||||||
profile.id,
|
profile.id,
|
||||||
sanitize_identifier(linked_table)
|
lt_name
|
||||||
)
|
)
|
||||||
.fetch_optional(db_pool)
|
.fetch_optional(db_pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| Status::internal(format!("Linked table lookup failed: {}", e)))?;
|
.map_err(|e| Status::internal(format!("Linked table lookup failed: {}", e)))?;
|
||||||
|
|
||||||
lt.map(|r| r.id)
|
linked_table_id = match lt_record {
|
||||||
.ok_or_else(|| Status::not_found("Linked table not found in profile"))?
|
Some(r) => Some(r.id),
|
||||||
|
None => return Err(Status::not_found("Linked table not found in profile")),
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
None
|
linked_table_id = None;
|
||||||
};
|
|
||||||
|
|
||||||
fn is_reserved_column(name: &str, linked_table: Option<&str>) -> bool {
|
|
||||||
let reserved = vec!["id", "deleted", "firma", "created_at"];
|
|
||||||
reserved.contains(&name) || linked_table.map(|lt| format!("{}_id", lt)) == Some(name.to_string())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate columns and indexes (add data type support)
|
// Validate columns and indexes
|
||||||
let mut columns = Vec::new();
|
let mut columns = Vec::new();
|
||||||
for col_def in request.columns.drain(..) {
|
for col_def in request.columns.drain(..) {
|
||||||
let col_name = sanitize_identifier(&col_def.name);
|
let col_name = sanitize_identifier(&col_def.name);
|
||||||
@@ -93,7 +92,12 @@ pub async fn post_table_definition(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Generate SQL
|
// Generate SQL
|
||||||
let (create_sql, index_sql) = generate_table_sql(&table_name, &columns, &indexes);
|
let (create_sql, index_sql) = generate_table_sql(
|
||||||
|
&table_name,
|
||||||
|
&columns,
|
||||||
|
&indexes,
|
||||||
|
linked_table_name.as_deref()
|
||||||
|
);
|
||||||
|
|
||||||
// Store definition
|
// Store definition
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
@@ -136,8 +140,6 @@ pub async fn post_table_definition(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Add system columns to SQL generation
|
|
||||||
fn generate_table_sql(
|
fn generate_table_sql(
|
||||||
table_name: &str,
|
table_name: &str,
|
||||||
columns: &[String],
|
columns: &[String],
|
||||||
@@ -145,9 +147,9 @@ fn generate_table_sql(
|
|||||||
linked_table: Option<&str>,
|
linked_table: Option<&str>,
|
||||||
) -> (String, Vec<String>) {
|
) -> (String, Vec<String>) {
|
||||||
let mut system_columns = vec![
|
let mut system_columns = vec![
|
||||||
"id BIGSERIAL PRIMARY KEY",
|
"id BIGSERIAL PRIMARY KEY".to_string(),
|
||||||
"deleted BOOLEAN NOT NULL DEFAULT FALSE",
|
"deleted BOOLEAN NOT NULL DEFAULT FALSE".to_string(),
|
||||||
"firma TEXT NOT NULL",
|
"firma TEXT NOT NULL".to_string(),
|
||||||
];
|
];
|
||||||
|
|
||||||
if let Some(linked) = linked_table {
|
if let Some(linked) = linked_table {
|
||||||
@@ -158,8 +160,8 @@ fn generate_table_sql(
|
|||||||
|
|
||||||
let all_columns = system_columns
|
let all_columns = system_columns
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| s.to_string())
|
.chain(columns.iter())
|
||||||
.chain(columns.iter().cloned())
|
.cloned()
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let create_sql = format!(
|
let create_sql = format!(
|
||||||
@@ -179,5 +181,13 @@ fn generate_table_sql(
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
(create_sql, [system_indexes, indexes.to_vec()].concat())
|
let all_indexes = system_indexes
|
||||||
|
.into_iter()
|
||||||
|
.chain(indexes.iter().map(|idx| {
|
||||||
|
format!("CREATE INDEX idx_{}_{} ON \"{}\" (\"{}\")",
|
||||||
|
table_name, idx, table_name, idx)
|
||||||
|
}))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
(create_sql, all_indexes)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user