diff --git a/podcasts-data/src/dbqueries.rs b/podcasts-data/src/dbqueries.rs index a4b0d82..73b1502 100644 --- a/podcasts-data/src/dbqueries.rs +++ b/podcasts-data/src/dbqueries.rs @@ -392,7 +392,7 @@ pub fn is_episodes_populated() -> Result { /// Check if the `shows` table contains any rows /// -/// Return true if `shows table is populated. +/// Return true if `shows` table is populated. pub fn is_podcasts_populated(filter_ids: &[i32]) -> Result { use schema::shows::dsl::*; @@ -404,6 +404,20 @@ pub fn is_podcasts_populated(filter_ids: &[i32]) -> Result { .map_err(From::from) } +/// Check if the `source` table contains any rows +/// +/// Return true if `source` table is populated. +pub fn is_source_populated(filter_ids: &[i32]) -> Result { + use schema::source::dsl::*; + + let db = connection(); + let con = db.get()?; + + select(exists(source.filter(id.ne_all(filter_ids)))) + .get_result(&con) + .map_err(From::from) +} + pub(crate) fn index_new_episodes(eps: &[NewEpisode]) -> Result<(), DataError> { use schema::episodes::dsl::*; let db = connection(); diff --git a/podcasts-gtk/src/utils.rs b/podcasts-gtk/src/utils.rs index c78411d..a883e55 100644 --- a/podcasts-gtk/src/utils.rs +++ b/podcasts-gtk/src/utils.rs @@ -187,6 +187,19 @@ pub(crate) fn refresh(source: Option, sender: Sender) where S: IntoIterator + Send + 'static, { + // If we try to update the whole db, + // Exit early if `source` table is empty + if source.is_none() { + match dbqueries::is_source_populated(&[]) { + Ok(false) => { + info!("No source of feeds where found, returning"); + return; + } + Err(err) => debug_assert!(false, err), + _ => (), + }; + } + refresh_feed(source, sender) .map_err(|err| error!("Failed to update feeds: {}", err)) .ok();