better schema to original migration files
This commit is contained in:
@@ -22,16 +22,21 @@ fn is_valid_identifier(s: &str) -> bool {
|
||||
!s.chars().next().unwrap().is_ascii_digit()
|
||||
}
|
||||
|
||||
fn sanitize_identifier(s: &str) -> String {
|
||||
fn sanitize_table_name(s: &str) -> String {
|
||||
let year = OffsetDateTime::now_utc().year();
|
||||
let cleaned = s.replace(|c: char| !c.is_ascii_alphanumeric() && c != '_', "")
|
||||
.trim()
|
||||
.to_lowercase();
|
||||
|
||||
// Format: "currentyear_tablename"
|
||||
format!("{}_{}", year, cleaned)
|
||||
}
|
||||
|
||||
fn sanitize_identifier(s: &str) -> String {
|
||||
s.replace(|c: char| !c.is_ascii_alphanumeric() && c != '_', "")
|
||||
.trim()
|
||||
.to_lowercase()
|
||||
}
|
||||
|
||||
fn map_field_type(field_type: &str) -> Result<&str, Status> {
|
||||
PREDEFINED_FIELD_TYPES
|
||||
.iter()
|
||||
@@ -44,8 +49,8 @@ pub async fn post_table_definition(
|
||||
db_pool: &PgPool,
|
||||
mut request: PostTableDefinitionRequest,
|
||||
) -> Result<TableDefinitionResponse, Status> {
|
||||
// Validate and sanitize inputs
|
||||
let table_name = sanitize_identifier(&request.table_name);
|
||||
// Validate and sanitize table name
|
||||
let table_name = sanitize_table_name(&request.table_name);
|
||||
if !is_valid_identifier(&request.table_name) {
|
||||
return Err(Status::invalid_argument("Invalid table name"));
|
||||
}
|
||||
@@ -61,8 +66,10 @@ pub async fn post_table_definition(
|
||||
.await
|
||||
.map_err(|e| Status::internal(format!("Profile error: {}", e)))?;
|
||||
|
||||
// Validate linked table if provided
|
||||
// Declare linked_table_id here
|
||||
let linked_table_id;
|
||||
|
||||
// Validate linked table if provided
|
||||
if let Some(lt_name) = &request.linked_table_name {
|
||||
let lt_record = sqlx::query!(
|
||||
"SELECT id FROM table_definitions
|
||||
@@ -81,19 +88,28 @@ pub async fn post_table_definition(
|
||||
} else {
|
||||
linked_table_id = None;
|
||||
}
|
||||
|
||||
// Validate columns and indexes
|
||||
|
||||
// Process columns without year prefix
|
||||
let mut columns = Vec::new();
|
||||
for col_def in request.columns.drain(..) {
|
||||
let col_name = sanitize_identifier(&col_def.name);
|
||||
let col_name = sanitize_identifier(&col_def.name); // No year prefix
|
||||
if !is_valid_identifier(&col_def.name) {
|
||||
return Err(Status::invalid_argument("Invalid column name"));
|
||||
}
|
||||
|
||||
let sql_type = map_field_type(&col_def.field_type)?;
|
||||
columns.push(format!("\"{}\" {}", col_name, sql_type));
|
||||
}
|
||||
|
||||
// Process indexes without year prefix
|
||||
let mut indexes = Vec::new();
|
||||
for idx in request.indexes.drain(..) {
|
||||
let idx_name = sanitize_identifier(&idx); // No year prefix
|
||||
if !is_valid_identifier(&idx) {
|
||||
return Err(Status::invalid_argument(format!("Invalid index name: {}", idx)));
|
||||
}
|
||||
indexes.push(idx_name);
|
||||
}
|
||||
|
||||
let mut indexes = Vec::new();
|
||||
for idx in request.indexes.drain(..) {
|
||||
let idx_name = sanitize_identifier(&idx);
|
||||
|
||||
Reference in New Issue
Block a user