diff --git a/mediarepo-daemon/mediarepo-model/src/repo.rs b/mediarepo-daemon/mediarepo-model/src/repo.rs index 026e487..9e407fb 100644 --- a/mediarepo-daemon/mediarepo-model/src/repo.rs +++ b/mediarepo-daemon/mediarepo-model/src/repo.rs @@ -110,7 +110,7 @@ impl Repo { .map(|t| parse_namespace_and_tag(t.0.clone())) .collect(); - let db_tags = self.all_tags(parsed_tags).await?; + let db_tags = self.tags_by_names(parsed_tags).await?; let tag_map: HashMap = HashMap::from_iter(tags.into_iter()); let tag_ids: Vec<(i64, bool)> = db_tags @@ -197,7 +197,7 @@ impl Repo { /// Finds all tags by name #[tracing::instrument(level = "debug", skip(self))] - pub async fn all_tags(&self, tags: Vec<(Option, String)>) -> RepoResult> { + pub async fn tags_by_names(&self, tags: Vec<(Option, String)>) -> RepoResult> { Tag::all_by_name(self.db.clone(), tags).await } diff --git a/mediarepo-daemon/mediarepo-model/src/tag.rs b/mediarepo-daemon/mediarepo-model/src/tag.rs index 7a8ad42..3596781 100644 --- a/mediarepo-daemon/mediarepo-model/src/tag.rs +++ b/mediarepo-daemon/mediarepo-model/src/tag.rs @@ -95,6 +95,7 @@ impl Tag { let tags: Vec = tag::Entity::find() .find_also_related(namespace::Entity) .filter(or_condition) + .group_by(tag::Column::Id) .all(&db) .await? .into_iter() diff --git a/mediarepo-daemon/src/main.rs b/mediarepo-daemon/src/main.rs index 314454c..8b20fd4 100644 --- a/mediarepo-daemon/src/main.rs +++ b/mediarepo-daemon/src/main.rs @@ -238,16 +238,20 @@ async fn add_tags_from_tags_file( log::info!("Adding tags"); if tags_path.exists() { let mut tags = parse_tags_file(tags_path).await?; - let resolved_tags = repo.all_tags(tags.clone()).await?; + log::info!("Found {} tags in the tag file", tags.len()); + let resolved_tags = repo.tags_by_names(tags.clone()).await?; + tags.retain(|tag| { resolved_tags .iter() .find(|t| if let (Some(ns1), Some(ns2)) = (t.namespace(), &tag.0) { *ns1.name() == *ns2 - } else { false } && *t.name() == *tag.1) + } else { t.namespace().is_none() && tag.0.is_none() } && *t.name() == *tag.1) .is_none() }); let mut tag_ids: Vec = resolved_tags.into_iter().map(|t| t.id()).collect(); + log::info!("Existing tag_ids count is {}", tag_ids.len()); + log::info!("{} tags need to be created", tags.len()); for (namespace, name) in tags { let tag = if let Some(namespace) = namespace {