changing profile id to schema in the whole project

This commit is contained in:
filipriec
2025-06-21 09:57:14 +02:00
parent 9477f53432
commit 63f1b4da2e
8 changed files with 84 additions and 64 deletions

View File

@@ -100,8 +100,8 @@ fn is_invalid_table_name(table_name: &str) -> bool {
fn is_reserved_schema(schema_name: &str) -> bool {
let lower = schema_name.to_lowercase();
lower == "public" ||
lower == "information_schema" ||
lower == "public" ||
lower == "information_schema" ||
lower.starts_with("pg_")
}
@@ -115,12 +115,12 @@ pub async fn post_table_definition(
// Apply same sanitization rules as table names
let sanitized_profile_name = sanitize_identifier(&request.profile_name);
// Add validation to prevent reserved schemas
if is_reserved_schema(&sanitized_profile_name) {
return Err(Status::invalid_argument("Profile name is reserved and cannot be used"));
}
if !is_valid_identifier(&sanitized_profile_name) {
return Err(Status::invalid_argument("Invalid profile name"));
}
@@ -184,28 +184,31 @@ async fn execute_table_definition(
table_name: String,
profile_name: String,
) -> Result<TableDefinitionResponse, Status> {
let profile = sqlx::query!(
"INSERT INTO profiles (name) VALUES ($1)
// CHANGED: Use schemas table instead of profiles table
let schema = sqlx::query!(
"INSERT INTO schemas (name) VALUES ($1)
ON CONFLICT (name) DO UPDATE SET name = EXCLUDED.name
RETURNING id",
request.profile_name
)
.fetch_one(&mut **tx)
.await
.map_err(|e| Status::internal(format!("Profile error: {}", e)))?;
.map_err(|e| Status::internal(format!("Schema error: {}", e)))?;
// Create schema if it doesn't exist
sqlx::query(&format!("CREATE SCHEMA IF NOT EXISTS \"{}\"", profile_name))
// Create PostgreSQL schema if it doesn't exist
let create_schema_sql = format!("CREATE SCHEMA IF NOT EXISTS \"{}\"", profile_name);
sqlx::query(&create_schema_sql)
.execute(&mut **tx)
.await
.map_err(|e| Status::internal(format!("Schema creation failed: {}", e)))?;
let mut links = Vec::new();
for link in request.links.drain(..) {
// CHANGED: Use schema_id instead of profile_id
let linked_table = sqlx::query!(
"SELECT id FROM table_definitions
WHERE profile_id = $1 AND table_name = $2",
profile.id,
WHERE schema_id = $1 AND table_name = $2",
schema.id,
link.linked_table_name
)
.fetch_optional(&mut **tx)
@@ -246,12 +249,13 @@ async fn execute_table_definition(
let (create_sql, index_sql) = generate_table_sql(tx, &profile_name, &table_name, &columns, &indexes, &links).await?;
// CHANGED: Use schema_id instead of profile_id
let table_def = sqlx::query!(
r#"INSERT INTO table_definitions
(profile_id, table_name, columns, indexes)
(schema_id, table_name, columns, indexes)
VALUES ($1, $2, $3, $4)
RETURNING id"#,
profile.id,
schema.id,
&table_name,
json!(columns),
json!(indexes)
@@ -260,7 +264,8 @@ async fn execute_table_definition(
.await
.map_err(|e| {
if let Some(db_err) = e.as_database_error() {
if db_err.constraint() == Some("idx_table_definitions_profile_table") {
// CHANGED: Update constraint name to match new schema
if db_err.constraint() == Some("idx_table_definitions_schema_table") {
return Status::already_exists("Table already exists in this profile");
}
}