mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-23 17:28:29 +02:00
Delete duplicate folder environments on upsert
This commit is contained in:
@@ -73,11 +73,12 @@ impl YaakNotifier {
|
|||||||
|
|
||||||
self.last_check = SystemTime::now();
|
self.last_check = SystemTime::now();
|
||||||
|
|
||||||
let license_check = match check_license(window).await? {
|
let license_check = match check_license(window).await {
|
||||||
LicenseCheckStatus::PersonalUse { .. } => "personal".to_string(),
|
Ok(LicenseCheckStatus::PersonalUse { .. }) => "personal".to_string(),
|
||||||
LicenseCheckStatus::CommercialUse => "commercial".to_string(),
|
Ok(LicenseCheckStatus::CommercialUse) => "commercial".to_string(),
|
||||||
LicenseCheckStatus::InvalidLicense => "invalid_license".to_string(),
|
Ok(LicenseCheckStatus::InvalidLicense) => "invalid_license".to_string(),
|
||||||
LicenseCheckStatus::Trialing { .. } => "trialing".to_string(),
|
Ok(LicenseCheckStatus::Trialing { .. }) => "trialing".to_string(),
|
||||||
|
Err(_) => "unknown".to_string(),
|
||||||
};
|
};
|
||||||
let settings = window.db().get_settings();
|
let settings = window.db().get_settings();
|
||||||
let num_launches = get_num_launches(app_handle).await;
|
let num_launches = get_num_launches(app_handle).await;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use crate::error::Error::{
|
|||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
use crate::models::{Environment, EnvironmentIden, EnvironmentVariable};
|
use crate::models::{Environment, EnvironmentIden, EnvironmentVariable};
|
||||||
use crate::util::UpdateSource;
|
use crate::util::UpdateSource;
|
||||||
use log::info;
|
use log::{info, warn};
|
||||||
|
|
||||||
impl<'a> DbContext<'a> {
|
impl<'a> DbContext<'a> {
|
||||||
pub fn get_environment(&self, id: &str) -> Result<Environment> {
|
pub fn get_environment(&self, id: &str) -> Result<Environment> {
|
||||||
@@ -13,12 +13,10 @@ impl<'a> DbContext<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_environment_by_folder_id(&self, folder_id: &str) -> Result<Option<Environment>> {
|
pub fn get_environment_by_folder_id(&self, folder_id: &str) -> Result<Option<Environment>> {
|
||||||
let environments: Vec<Environment> =
|
let mut environments: Vec<Environment> =
|
||||||
self.find_many(EnvironmentIden::ParentId, folder_id, None)?;
|
self.find_many(EnvironmentIden::ParentId, folder_id, None)?;
|
||||||
if environments.len() > 1 {
|
// Sort so we return the most recently updated environment
|
||||||
return Err(MultipleFolderEnvironments(folder_id.to_string()));
|
environments.sort_by(|a, b| b.updated_at.cmp(&a.updated_at));
|
||||||
}
|
|
||||||
|
|
||||||
Ok(environments.get(0).cloned())
|
Ok(environments.get(0).cloned())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,6 +89,23 @@ impl<'a> DbContext<'a> {
|
|||||||
self.upsert_environment(&environment, source)
|
self.upsert_environment(&environment, source)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Find other environments with the same parent folder
|
||||||
|
fn list_duplicate_folder_environments(&self, environment: &Environment) -> Vec<Environment> {
|
||||||
|
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(
|
pub fn upsert_environment(
|
||||||
&self,
|
&self,
|
||||||
environment: &Environment,
|
environment: &Environment,
|
||||||
@@ -102,8 +117,31 @@ impl<'a> DbContext<'a> {
|
|||||||
.filter(|v| !v.name.is_empty() || !v.value.is_empty())
|
.filter(|v| !v.name.is_empty() || !v.value.is_empty())
|
||||||
.cloned()
|
.cloned()
|
||||||
.collect::<Vec<EnvironmentVariable>>();
|
.collect::<Vec<EnvironmentVariable>>();
|
||||||
|
|
||||||
|
// 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(
|
self.upsert(
|
||||||
&Environment {
|
&Environment {
|
||||||
|
name,
|
||||||
variables: cleaned_variables,
|
variables: cleaned_variables,
|
||||||
..environment.clone()
|
..environment.clone()
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user