no ud_ in the table naming
This commit is contained in:
@@ -12,14 +12,10 @@ fn is_valid_data_type(dt: &str) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn is_valid_identifier(s: &str) -> bool {
|
fn is_valid_identifier(s: &str) -> bool {
|
||||||
let parts: Vec<&str> = s.split('_').collect();
|
!s.is_empty() &&
|
||||||
|
s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') &&
|
||||||
parts.len() >= 3 &&
|
!s.starts_with('_') &&
|
||||||
parts[0] == "ud" &&
|
!s.chars().next().unwrap().is_ascii_digit()
|
||||||
parts[1].len() == 4 &&
|
|
||||||
parts[1].chars().all(|c| c.is_ascii_digit()) &&
|
|
||||||
parts[2..].iter().all(|p| !p.is_empty()) &&
|
|
||||||
s.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sanitize_identifier(s: &str) -> String {
|
fn sanitize_identifier(s: &str) -> String {
|
||||||
@@ -27,8 +23,9 @@ fn sanitize_identifier(s: &str) -> String {
|
|||||||
let cleaned = s.replace(|c: char| !c.is_ascii_alphanumeric() && c != '_', "")
|
let cleaned = s.replace(|c: char| !c.is_ascii_alphanumeric() && c != '_', "")
|
||||||
.trim()
|
.trim()
|
||||||
.to_lowercase();
|
.to_lowercase();
|
||||||
|
|
||||||
format!("ud_{}_{}", year, cleaned)
|
// Format: "currentyear_tablename"
|
||||||
|
format!("{}_{}", year, cleaned)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn post_table_definition(
|
pub async fn post_table_definition(
|
||||||
@@ -37,7 +34,7 @@ pub async fn post_table_definition(
|
|||||||
) -> Result<TableDefinitionResponse, Status> {
|
) -> Result<TableDefinitionResponse, Status> {
|
||||||
// Validate and sanitize inputs
|
// Validate and sanitize inputs
|
||||||
let table_name = sanitize_identifier(&request.table_name);
|
let table_name = sanitize_identifier(&request.table_name);
|
||||||
if !is_valid_identifier(&table_name) {
|
if !is_valid_identifier(&request.table_name) {
|
||||||
return Err(Status::invalid_argument("Invalid table name"));
|
return Err(Status::invalid_argument("Invalid table name"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,11 +51,8 @@ pub async fn post_table_definition(
|
|||||||
|
|
||||||
// Validate linked table if provided
|
// Validate linked table if provided
|
||||||
let linked_table_id;
|
let linked_table_id;
|
||||||
let linked_table_name = request.linked_table_name
|
if let Some(lt_name) = &request.linked_table_name {
|
||||||
.as_ref()
|
// Lookup the table with the EXACT provided name
|
||||||
.map(|lt| sanitize_identifier(lt));
|
|
||||||
|
|
||||||
if let Some(lt_name) = &linked_table_name {
|
|
||||||
let lt_record = sqlx::query!(
|
let lt_record = sqlx::query!(
|
||||||
"SELECT id FROM table_definitions
|
"SELECT id FROM table_definitions
|
||||||
WHERE profile_id = $1 AND table_name = $2",
|
WHERE profile_id = $1 AND table_name = $2",
|
||||||
@@ -81,7 +75,7 @@ pub async fn post_table_definition(
|
|||||||
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);
|
||||||
if !is_valid_identifier(&col_name) {
|
if !is_valid_identifier(&col_def.name) {
|
||||||
return Err(Status::invalid_argument("Invalid column name"));
|
return Err(Status::invalid_argument("Invalid column name"));
|
||||||
}
|
}
|
||||||
if !is_valid_data_type(&col_def.data_type) {
|
if !is_valid_data_type(&col_def.data_type) {
|
||||||
@@ -93,7 +87,7 @@ pub async fn post_table_definition(
|
|||||||
let mut indexes = Vec::new();
|
let mut indexes = Vec::new();
|
||||||
for idx in request.indexes.drain(..) {
|
for idx in request.indexes.drain(..) {
|
||||||
let idx_name = sanitize_identifier(&idx);
|
let idx_name = sanitize_identifier(&idx);
|
||||||
if !is_valid_identifier(&idx_name) {
|
if !is_valid_identifier(&idx) {
|
||||||
return Err(Status::invalid_argument(format!("Invalid index name: {}", idx)));
|
return Err(Status::invalid_argument(format!("Invalid index name: {}", idx)));
|
||||||
}
|
}
|
||||||
indexes.push(idx_name);
|
indexes.push(idx_name);
|
||||||
@@ -104,7 +98,7 @@ pub async fn post_table_definition(
|
|||||||
&table_name,
|
&table_name,
|
||||||
&columns,
|
&columns,
|
||||||
&indexes,
|
&indexes,
|
||||||
linked_table_name.as_deref()
|
request.linked_table_name.as_deref()
|
||||||
);
|
);
|
||||||
|
|
||||||
// Store definition
|
// Store definition
|
||||||
@@ -162,12 +156,12 @@ fn generate_table_sql(
|
|||||||
|
|
||||||
if let Some(linked) = linked_table {
|
if let Some(linked) = linked_table {
|
||||||
// Extract base name without prefix for relationship
|
// Extract base name without prefix for relationship
|
||||||
let parts: Vec<&str> = linked.splitn(3, '_').collect();
|
let parts: Vec<&str> = linked.splitn(2, '_').collect();
|
||||||
let base_name = parts.get(2).unwrap_or(&linked);
|
let base_name = parts.get(1).unwrap_or(&linked); // "profile_table"
|
||||||
|
|
||||||
system_columns.push(
|
system_columns.push(
|
||||||
format!("\"{}_id\" BIGINT NOT NULL REFERENCES \"{}\"(id)",
|
format!("\"{}_id\" BIGINT NOT NULL REFERENCES \"{}\"(id)",
|
||||||
base_name,
|
base_name,
|
||||||
linked
|
linked
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@@ -190,17 +184,9 @@ fn generate_table_sql(
|
|||||||
];
|
];
|
||||||
|
|
||||||
if let Some(linked) = linked_table {
|
if let Some(linked) = linked_table {
|
||||||
let parts: Vec<&str> = linked.splitn(3, '_').collect();
|
let parts: Vec<&str> = linked.splitn(2, '_').collect();
|
||||||
let base_name = parts.get(2).unwrap_or(&linked);
|
let base_name = parts.get(1).unwrap_or(&linked);
|
||||||
|
|
||||||
system_columns.push(
|
|
||||||
format!("\"{}_id\" BIGINT NOT NULL REFERENCES \"{}\"(id)",
|
|
||||||
base_name,
|
|
||||||
linked
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// FIXED: Use base_name instead of linked
|
|
||||||
system_indexes.push(format!(
|
system_indexes.push(format!(
|
||||||
"CREATE INDEX idx_{}_{}_id ON \"{}\" (\"{}_id\")",
|
"CREATE INDEX idx_{}_{}_id ON \"{}\" (\"{}_id\")",
|
||||||
table_name, base_name, table_name, base_name
|
table_name, base_name, table_name, base_name
|
||||||
|
|||||||
Reference in New Issue
Block a user