From b3d6d87bee4091ecf4da19337959035ace951a24 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Mon, 29 Sep 2025 07:48:07 -0700 Subject: [PATCH] Delete duplicate folder environments on upsert --- src-tauri/src/notifications.rs | 11 ++-- .../yaak-models/src/queries/environments.rs | 50 ++++++++++++++++--- 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/src-tauri/src/notifications.rs b/src-tauri/src/notifications.rs index d6e1d9b0..7c51bbba 100644 --- a/src-tauri/src/notifications.rs +++ b/src-tauri/src/notifications.rs @@ -73,11 +73,12 @@ impl YaakNotifier { self.last_check = SystemTime::now(); - let license_check = match check_license(window).await? { - LicenseCheckStatus::PersonalUse { .. } => "personal".to_string(), - LicenseCheckStatus::CommercialUse => "commercial".to_string(), - LicenseCheckStatus::InvalidLicense => "invalid_license".to_string(), - LicenseCheckStatus::Trialing { .. } => "trialing".to_string(), + let license_check = match check_license(window).await { + Ok(LicenseCheckStatus::PersonalUse { .. }) => "personal".to_string(), + Ok(LicenseCheckStatus::CommercialUse) => "commercial".to_string(), + Ok(LicenseCheckStatus::InvalidLicense) => "invalid_license".to_string(), + Ok(LicenseCheckStatus::Trialing { .. }) => "trialing".to_string(), + Err(_) => "unknown".to_string(), }; let settings = window.db().get_settings(); let num_launches = get_num_launches(app_handle).await; diff --git a/src-tauri/yaak-models/src/queries/environments.rs b/src-tauri/yaak-models/src/queries/environments.rs index 599753e8..f070ff56 100644 --- a/src-tauri/yaak-models/src/queries/environments.rs +++ b/src-tauri/yaak-models/src/queries/environments.rs @@ -5,7 +5,7 @@ use crate::error::Error::{ use crate::error::Result; use crate::models::{Environment, EnvironmentIden, EnvironmentVariable}; use crate::util::UpdateSource; -use log::info; +use log::{info, warn}; impl<'a> DbContext<'a> { pub fn get_environment(&self, id: &str) -> Result { @@ -13,12 +13,10 @@ impl<'a> DbContext<'a> { } pub fn get_environment_by_folder_id(&self, folder_id: &str) -> Result> { - let environments: Vec = + let mut environments: Vec = self.find_many(EnvironmentIden::ParentId, folder_id, None)?; - if environments.len() > 1 { - return Err(MultipleFolderEnvironments(folder_id.to_string())); - } - + // Sort so we return the most recently updated environment + environments.sort_by(|a, b| b.updated_at.cmp(&a.updated_at)); Ok(environments.get(0).cloned()) } @@ -91,6 +89,23 @@ impl<'a> DbContext<'a> { self.upsert_environment(&environment, source) } + /// Find other environments with the same parent folder + fn list_duplicate_folder_environments(&self, environment: &Environment) -> Vec { + if environment.parent_model != "folder" { + return Vec::new(); + } + + self.list_environments_ensure_base(&environment.workspace_id) + .unwrap_or_default() + .into_iter() + .filter(|e| { + e.id != environment.id + && e.parent_model == "folder" + && e.parent_id == environment.parent_id + }) + .collect() + } + pub fn upsert_environment( &self, environment: &Environment, @@ -102,8 +117,31 @@ impl<'a> DbContext<'a> { .filter(|v| !v.name.is_empty() || !v.value.is_empty()) .cloned() .collect::>(); + + // Sometimes a new environment can be created via sync/import, so we'll just delete + // the others when that happens. Not the best, but it's good for now. + let duplicates = self.list_duplicate_folder_environments(environment); + for duplicate in duplicates { + warn!( + "Deleting duplicate environment {} for folder {:?}", + duplicate.id, environment.parent_id + ); + _ = self.delete(&duplicate, source); + } + + // Automatically update the environment name based on the folder name + let mut name = environment.name.clone(); + match (environment.parent_model.as_str(), environment.parent_id.as_deref()) { + ("folder", Some(folder_id)) => { + let folder = self.get_folder(folder_id)?; + name = format!("{} Environment", folder.name); + } + _ => {} + } + self.upsert( &Environment { + name, variables: cleaned_variables, ..environment.clone() },