From 2809a7e4f2109b7a6d8190d21b2a71a03d3849e6 Mon Sep 17 00:00:00 2001 From: Vidyoot Senthil Date: Thu, 8 May 2025 01:16:31 -0500 Subject: [PATCH 1/4] refactor: modify dataset configuration update to include filtering options --- server/src/handlers/organization_handler.rs | 11 +++++--- server/src/operators/organization_operator.rs | 27 +++++++++++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/server/src/handlers/organization_handler.rs b/server/src/handlers/organization_handler.rs index 82006bfbae..7e84fb6861 100644 --- a/server/src/handlers/organization_handler.rs +++ b/server/src/handlers/organization_handler.rs @@ -434,8 +434,10 @@ pub async fn remove_user_from_org( } }))] pub struct UpdateAllOrgDatasetConfigsReqPayload { + /// The configuration to provide a filter on what datasets to update. + pub from_configuration: Option, /// The new configuration for all datasets in the organization. Only the specified keys in the configuration object will be changed per dataset such that you can preserve dataset unique values. - pub dataset_config: serde_json::Value, + pub to_configuration: serde_json::Value, } /// Update All Dataset Configurations @@ -469,9 +471,12 @@ pub async fn update_all_org_dataset_configs( return Ok(HttpResponse::Forbidden().finish()); }; - let new_dataset_config = req_payload.dataset_config.clone(); + let req_payload = req_payload.into_inner(); - update_all_org_dataset_configs_query(organization_id, new_dataset_config, pool).await?; + let new_dataset_config = req_payload.to_configuration.clone(); + let from_configuration = req_payload.from_configuration.clone(); + + update_all_org_dataset_configs_query(organization_id, new_dataset_config, from_configuration, pool).await?; Ok(HttpResponse::NoContent().finish()) } diff --git a/server/src/operators/organization_operator.rs b/server/src/operators/organization_operator.rs index 1526479885..fc854e79a0 100644 --- a/server/src/operators/organization_operator.rs +++ b/server/src/operators/organization_operator.rs @@ -987,22 +987,39 @@ pub async fn delete_actual_organization_query( Ok(()) } - pub async fn update_all_org_dataset_configs_query( org_id: uuid::Uuid, new_config: serde_json::Value, + from_config: Option, pool: web::Data, ) -> Result<(), ServiceError> { - let concat_configs_raw_query = sql_query(format!( - "UPDATE datasets SET server_configuration = server_configuration || '{}' WHERE organization_id = '{}';", + let mut concat_configs_raw_query = format!( + "UPDATE datasets SET server_configuration = server_configuration || '{}' WHERE organization_id = '{}'", new_config.to_string().replace('\'', "''"), org_id - )); + ); + + if let Some(from_config) = from_config { + let from_config_query = from_config.as_object().unwrap().iter().map(|(key, value)| { + format!( + "server_configuration::jsonb->>'{}' = '{}'", + key, + value.as_str().unwrap().replace('\'', "''") + ) + }).collect::>().join(" AND "); + + concat_configs_raw_query.push_str(&format!( + " AND ({})", + from_config_query + )); + } + + concat_configs_raw_query.push(';'); let mut conn = pool.get().await.map_err(|_e| { ServiceError::InternalServerError("Failed to get postgres connection".to_string()) })?; - concat_configs_raw_query + sql_query(concat_configs_raw_query) .execute(&mut conn) .await .map_err(|e| { From a45c0498ec0e20359914a35f839500f76c73990b Mon Sep 17 00:00:00 2001 From: Vidyoot Senthil Date: Thu, 8 May 2025 01:27:23 -0500 Subject: [PATCH 2/4] cleanup: fix linting errors --- server/src/handlers/organization_handler.rs | 8 +++++- server/src/operators/organization_operator.rs | 25 +++++++++++-------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/server/src/handlers/organization_handler.rs b/server/src/handlers/organization_handler.rs index 7e84fb6861..53cb0fc110 100644 --- a/server/src/handlers/organization_handler.rs +++ b/server/src/handlers/organization_handler.rs @@ -476,7 +476,13 @@ pub async fn update_all_org_dataset_configs( let new_dataset_config = req_payload.to_configuration.clone(); let from_configuration = req_payload.from_configuration.clone(); - update_all_org_dataset_configs_query(organization_id, new_dataset_config, from_configuration, pool).await?; + update_all_org_dataset_configs_query( + organization_id, + new_dataset_config, + from_configuration, + pool, + ) + .await?; Ok(HttpResponse::NoContent().finish()) } diff --git a/server/src/operators/organization_operator.rs b/server/src/operators/organization_operator.rs index fc854e79a0..2040de93d8 100644 --- a/server/src/operators/organization_operator.rs +++ b/server/src/operators/organization_operator.rs @@ -999,18 +999,21 @@ pub async fn update_all_org_dataset_configs_query( ); if let Some(from_config) = from_config { - let from_config_query = from_config.as_object().unwrap().iter().map(|(key, value)| { - format!( - "server_configuration::jsonb->>'{}' = '{}'", - key, - value.as_str().unwrap().replace('\'', "''") - ) - }).collect::>().join(" AND "); + let from_config_query = from_config + .as_object() + .unwrap() + .iter() + .map(|(key, value)| { + format!( + "server_configuration::jsonb->>'{}' = '{}'", + key, + value.as_str().unwrap().replace('\'', "''") + ) + }) + .collect::>() + .join(" AND "); - concat_configs_raw_query.push_str(&format!( - " AND ({})", - from_config_query - )); + concat_configs_raw_query.push_str(&format!(" AND ({})", from_config_query)); } concat_configs_raw_query.push(';'); From cc9d2a1cb623aef92ea2b4b881edb1394af6f11a Mon Sep 17 00:00:00 2001 From: Vidyoot Senthil Date: Thu, 8 May 2025 01:39:40 -0500 Subject: [PATCH 3/4] cleanup: use json format for server config query --- server/src/operators/organization_operator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/operators/organization_operator.rs b/server/src/operators/organization_operator.rs index 2040de93d8..f24c00f2e6 100644 --- a/server/src/operators/organization_operator.rs +++ b/server/src/operators/organization_operator.rs @@ -1005,7 +1005,7 @@ pub async fn update_all_org_dataset_configs_query( .iter() .map(|(key, value)| { format!( - "server_configuration::jsonb->>'{}' = '{}'", + "server_configuration::json->>'{}' = '{}'", key, value.as_str().unwrap().replace('\'', "''") ) From b98c8b3cbf292b3ba8d14bc697150461b33b36e4 Mon Sep 17 00:00:00 2001 From: Vidyoot Senthil Date: Thu, 15 May 2025 13:22:15 -0500 Subject: [PATCH 4/4] refactor: rename update dataset configuration field to match_configuration --- server/src/handlers/organization_handler.rs | 6 +++--- server/src/operators/organization_operator.rs | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/server/src/handlers/organization_handler.rs b/server/src/handlers/organization_handler.rs index 53cb0fc110..96d4f0a7bf 100644 --- a/server/src/handlers/organization_handler.rs +++ b/server/src/handlers/organization_handler.rs @@ -435,7 +435,7 @@ pub async fn remove_user_from_org( }))] pub struct UpdateAllOrgDatasetConfigsReqPayload { /// The configuration to provide a filter on what datasets to update. - pub from_configuration: Option, + pub match_configuration: Option, /// The new configuration for all datasets in the organization. Only the specified keys in the configuration object will be changed per dataset such that you can preserve dataset unique values. pub to_configuration: serde_json::Value, } @@ -474,12 +474,12 @@ pub async fn update_all_org_dataset_configs( let req_payload = req_payload.into_inner(); let new_dataset_config = req_payload.to_configuration.clone(); - let from_configuration = req_payload.from_configuration.clone(); + let match_configuration = req_payload.match_configuration.clone(); update_all_org_dataset_configs_query( organization_id, new_dataset_config, - from_configuration, + match_configuration, pool, ) .await?; diff --git a/server/src/operators/organization_operator.rs b/server/src/operators/organization_operator.rs index f24c00f2e6..fb601d24e0 100644 --- a/server/src/operators/organization_operator.rs +++ b/server/src/operators/organization_operator.rs @@ -990,7 +990,7 @@ pub async fn delete_actual_organization_query( pub async fn update_all_org_dataset_configs_query( org_id: uuid::Uuid, new_config: serde_json::Value, - from_config: Option, + match_config: Option, pool: web::Data, ) -> Result<(), ServiceError> { let mut concat_configs_raw_query = format!( @@ -998,8 +998,8 @@ pub async fn update_all_org_dataset_configs_query( new_config.to_string().replace('\'', "''"), org_id ); - if let Some(from_config) = from_config { - let from_config_query = from_config + if let Some(match_config) = match_config { + let match_config_query = match_config .as_object() .unwrap() .iter() @@ -1013,7 +1013,7 @@ pub async fn update_all_org_dataset_configs_query( .collect::>() .join(" AND "); - concat_configs_raw_query.push_str(&format!(" AND ({})", from_config_query)); + concat_configs_raw_query.push_str(&format!(" AND ({})", match_config_query)); } concat_configs_raw_query.push(';'); @@ -1027,7 +1027,7 @@ pub async fn update_all_org_dataset_configs_query( .await .map_err(|e| { log::error!( - "Error updating datasets in update_all_org_dataset_server_configs: {:?}", + "Error updating datasets in update_all_org_dataset_configs: {:?}", e ); ServiceError::BadRequest("Error updating datasets".to_string())