compiled with the profile to be schemas

This commit is contained in:
filipriec
2025-06-21 10:37:37 +02:00
parent 63f1b4da2e
commit 4e29d0084f
8 changed files with 42 additions and 40 deletions

View File

@@ -21,7 +21,8 @@ pub enum FunctionError {
#[derive(Clone)]
pub struct SteelContext {
pub current_table: String,
pub profile_id: i64,
pub schema_id: i64,
pub schema_name: String,
pub row_data: HashMap<String, String>,
pub db_pool: Arc<PgPool>,
}
@@ -30,8 +31,8 @@ impl SteelContext {
pub async fn get_related_table_name(&self, base_name: &str) -> Result<String, FunctionError> {
let table_def = sqlx::query!(
r#"SELECT table_name FROM table_definitions
WHERE profile_id = $1 AND table_name LIKE $2"#,
self.profile_id,
WHERE schema_id = $1 AND table_name LIKE $2"#,
self.schema_id,
format!("%_{}", base_name)
)
.fetch_optional(&*self.db_pool)
@@ -66,7 +67,7 @@ impl SteelContext {
// Add quotes around the table name
sqlx::query_scalar::<_, String>(
&format!("SELECT {} FROM \"{}\" WHERE id = $1", column, actual_table)
&format!("SELECT {} FROM \"{}\".\"{}\" WHERE id = $1", column, self.schema_name, actual_table)
)
.bind(fk_value.parse::<i64>().map_err(|_|
SteelVal::StringV("Invalid foreign key format".into()))?)

View File

@@ -49,7 +49,7 @@ pub async fn post_table_script(
) -> Result<TableScriptResponse, Status> {
// Fetch the table definition
let table_def = sqlx::query!(
r#"SELECT id, table_name, columns, profile_id
r#"SELECT id, table_name, columns, schema_id
FROM table_definitions WHERE id = $1"#,
request.table_definition_id
)
@@ -76,7 +76,7 @@ pub async fn post_table_script(
let script_record = sqlx::query!(
r#"INSERT INTO table_scripts
(table_definitions_id, target_table, target_column,
target_column_type, script, description, profile_id)
target_column_type, script, description, schema_id)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id"#,
request.table_definition_id,
@@ -85,7 +85,7 @@ pub async fn post_table_script(
column_type,
parsed_script,
request.description,
table_def.profile_id
table_def.schema_id
)
.fetch_one(db_pool)
.await

View File

@@ -9,24 +9,24 @@ pub async fn delete_table_data(
request: DeleteTableDataRequest,
) -> Result<DeleteTableDataResponse, Status> {
// Lookup profile
let profile = sqlx::query!(
"SELECT id FROM profiles WHERE name = $1",
let schema = sqlx::query!(
"SELECT id FROM schemas WHERE name = $1",
request.profile_name
)
.fetch_optional(db_pool)
.await
.map_err(|e| Status::internal(format!("Profile lookup error: {}", e)))?;
let profile_id = match profile {
Some(p) => p.id,
let schema_id = match schema {
Some(s) => s.id,
None => return Err(Status::not_found("Profile not found")),
};
// Verify table exists in profile
let table_exists = sqlx::query!(
"SELECT 1 AS exists FROM table_definitions
WHERE profile_id = $1 AND table_name = $2",
profile_id,
WHERE schema_id = $1 AND table_name = $2",
schema_id,
request.table_name
)
.fetch_optional(db_pool)

View File

@@ -15,21 +15,21 @@ pub async fn get_table_data(
let record_id = request.id;
// Lookup profile
let profile = sqlx::query!(
"SELECT id FROM profiles WHERE name = $1",
let schema = sqlx::query!(
"SELECT id FROM schemas WHERE name = $1",
profile_name
)
.fetch_optional(db_pool)
.await
.map_err(|e| Status::internal(format!("Profile lookup error: {}", e)))?;
let profile_id = profile.ok_or_else(|| Status::not_found("Profile not found"))?.id;
let schema_id = schema.ok_or_else(|| Status::not_found("Profile not found"))?.id;
// Lookup table_definition
let table_def = sqlx::query!(
r#"SELECT id, columns FROM table_definitions
WHERE profile_id = $1 AND table_name = $2"#,
profile_id,
WHERE schema_id = $1 AND table_name = $2"#,
schema_id,
table_name
)
.fetch_optional(db_pool)

View File

@@ -18,22 +18,22 @@ pub async fn get_table_data_by_position(
return Err(Status::invalid_argument("Position must be at least 1"));
}
let profile = sqlx::query!(
"SELECT id FROM profiles WHERE name = $1",
let schema = sqlx::query!(
"SELECT id FROM schemas WHERE name = $1",
profile_name
)
.fetch_optional(db_pool)
.await
.map_err(|e| Status::internal(format!("Profile lookup error: {}", e)))?;
let profile_id = profile.ok_or_else(|| Status::not_found("Profile not found"))?.id;
let schema_id = schema.ok_or_else(|| Status::not_found("Profile not found"))?.id;
let table_exists = sqlx::query_scalar!(
r#"SELECT EXISTS(
SELECT 1 FROM table_definitions
WHERE profile_id = $1 AND table_name = $2
WHERE schema_id = $1 AND table_name = $2
) AS "exists!""#,
profile_id,
schema_id,
table_name
)
.fetch_one(db_pool)

View File

@@ -12,15 +12,15 @@ pub async fn get_table_data_count(
// We still need to verify that the table is logically defined for the profile.
// The schema qualifier handles *how* to access it physically, but this check
// ensures the request is valid in the context of the application's definitions.
let profile = sqlx::query!(
"SELECT id FROM profiles WHERE name = $1",
let schema = sqlx::query!(
"SELECT id FROM schemas WHERE name = $1",
request.profile_name
)
.fetch_optional(db_pool)
.await
.map_err(|e| Status::internal(format!("Profile lookup error for '{}': {}", request.profile_name, e)))?;
let profile_id = match profile {
let schema_id = match schema {
Some(p) => p.id,
None => return Err(Status::not_found(format!("Profile '{}' not found", request.profile_name))),
};
@@ -28,9 +28,9 @@ pub async fn get_table_data_count(
let table_defined_for_profile = sqlx::query_scalar!(
r#"SELECT EXISTS(
SELECT 1 FROM table_definitions
WHERE profile_id = $1 AND table_name = $2
) AS "exists!" "#, // Added AS "exists!" for clarity with sqlx macro
profile_id,
WHERE schema_id = $1 AND table_name = $2
) AS "exists!" "#,
schema_id,
request.table_name
)
.fetch_one(db_pool)

View File

@@ -25,21 +25,21 @@ pub async fn post_table_data(
let table_name = request.table_name;
// Lookup profile
let profile = sqlx::query!(
"SELECT id FROM profiles WHERE name = $1",
let schema = sqlx::query!(
"SELECT id FROM schemas WHERE name = $1",
profile_name
)
.fetch_optional(db_pool)
.await
.map_err(|e| Status::internal(format!("Profile lookup error: {}", e)))?;
let profile_id = profile.ok_or_else(|| Status::not_found("Profile not found"))?.id;
let schema_id = schema.ok_or_else(|| Status::not_found("Profile not found"))?.id;
// Lookup table_definition
let table_def = sqlx::query!(
r#"SELECT id, columns FROM table_definitions
WHERE profile_id = $1 AND table_name = $2"#,
profile_id,
WHERE schema_id = $1 AND table_name = $2"#,
schema_id,
table_name
)
.fetch_optional(db_pool)
@@ -132,7 +132,8 @@ pub async fn post_table_data(
let context = SteelContext {
current_table: table_name.clone(),
profile_id,
schema_id,
schema_name: profile_name.clone(),
row_data: string_data_for_scripts.clone(),
db_pool: Arc::new(db_pool.clone()),
};

View File

@@ -21,21 +21,21 @@ pub async fn put_table_data(
}
// Lookup profile
let profile = sqlx::query!(
"SELECT id FROM profiles WHERE name = $1",
let schema = sqlx::query!(
"SELECT id FROM schemas WHERE name = $1",
profile_name
)
.fetch_optional(db_pool)
.await
.map_err(|e| Status::internal(format!("Profile lookup error: {}", e)))?;
let profile_id = profile.ok_or_else(|| Status::not_found("Profile not found"))?.id;
let schema_id = schema.ok_or_else(|| Status::not_found("Profile not found"))?.id;
// Lookup table_definition
let table_def = sqlx::query!(
r#"SELECT id, columns FROM table_definitions
WHERE profile_id = $1 AND table_name = $2"#,
profile_id,
WHERE schema_id = $1 AND table_name = $2"#,
schema_id,
table_name
)
.fetch_optional(db_pool)