From 62da8bbb52e5a81d3004c50605a0b3c38c424248 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Sat, 25 Nov 2017 01:56:10 +0200 Subject: [PATCH] Use source_id as the unique identifieble field for the podcast table. --- .gitignore | 3 ++ .../2017-09-15-001128_init_schema/up.sql | 52 +++++++++---------- hammond-data/src/dbqueries.rs | 14 ++++- hammond-data/src/models/insertables.rs | 8 +-- hammond-downloader/src/downloader.rs | 17 +++--- hammond-gtk/src/widgets/podcast.rs | 16 +++--- 6 files changed, 65 insertions(+), 45 deletions(-) diff --git a/.gitignore b/.gitignore index 548520d..7e7a4a3 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,6 @@ Cargo.lock _build vendor/ .flatpak-builder/ +flatpak-repo/ +Makefile +hammond-repo/ diff --git a/hammond-data/migrations/2017-09-15-001128_init_schema/up.sql b/hammond-data/migrations/2017-09-15-001128_init_schema/up.sql index a76f707..62df22b 100644 --- a/hammond-data/migrations/2017-09-15-001128_init_schema/up.sql +++ b/hammond-data/migrations/2017-09-15-001128_init_schema/up.sql @@ -4,36 +4,36 @@ -- in order to change the db schema. CREATE TABLE `source` ( - `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - `uri` TEXT NOT NULL UNIQUE, - `last_modified` TEXT, - `http_etag` TEXT + `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, + `uri` TEXT NOT NULL UNIQUE, + `last_modified` TEXT, + `http_etag` TEXT ); CREATE TABLE `episode` ( - `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - `title` TEXT, - `uri` TEXT NOT NULL UNIQUE, - `local_uri` TEXT, - `description` TEXT, - `published_date` TEXT, - `epoch` INTEGER NOT NULL DEFAULT 0, - `length` INTEGER, - `guid` TEXT, - `played` INTEGER, - `favorite` INTEGER NOT NULL DEFAULT 0, - `archive` INTEGER NOT NULL DEFAULT 0, - `podcast_id` INTEGER NOT NULL + `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, + `title` TEXT, + `uri` TEXT NOT NULL UNIQUE, + `local_uri` TEXT, + `description` TEXT, + `published_date` TEXT, + `epoch` INTEGER NOT NULL DEFAULT 0, + `length` INTEGER, + `guid` TEXT, + `played` INTEGER, + `favorite` INTEGER NOT NULL DEFAULT 0, + `archive` INTEGER NOT NULL DEFAULT 0, + `podcast_id` INTEGER NOT NULL ); CREATE TABLE `podcast` ( - `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - `title` TEXT NOT NULL UNIQUE, - `link` TEXT NOT NULL, - `description` TEXT NOT NULL, - `image_uri` TEXT, - `favorite` INTEGER NOT NULL DEFAULT 0, - `archive` INTEGER NOT NULL DEFAULT 0, - `always_dl` INTEGER NOT NULL DEFAULT 0, - `source_id` INTEGER NOT NULL + `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, + `title` TEXT NOT NULL, + `link` TEXT NOT NULL, + `description` TEXT NOT NULL, + `image_uri` TEXT, + `favorite` INTEGER NOT NULL DEFAULT 0, + `archive` INTEGER NOT NULL DEFAULT 0, + `always_dl` INTEGER NOT NULL DEFAULT 0, + `source_id` INTEGER NOT NULL UNIQUE ); \ No newline at end of file diff --git a/hammond-data/src/dbqueries.rs b/hammond-data/src/dbqueries.rs index 4c7d3d2..5cfa5ea 100644 --- a/hammond-data/src/dbqueries.rs +++ b/hammond-data/src/dbqueries.rs @@ -133,13 +133,23 @@ pub fn get_source_from_uri(uri_: &str) -> QueryResult { source.filter(uri.eq(uri_)).get_result::(&*con) } -pub fn get_podcast_from_title(title_: &str) -> QueryResult { +// pub fn get_podcast_from_title(title_: &str) -> QueryResult { +// use schema::podcast::dsl::*; + +// let db = connection(); +// let con = db.get().unwrap(); +// podcast +// .filter(title.eq(title_)) +// .get_result::(&*con) +// } + +pub fn get_podcast_from_source_id(sid: i32) -> QueryResult { use schema::podcast::dsl::*; let db = connection(); let con = db.get().unwrap(); podcast - .filter(title.eq(title_)) + .filter(source_id.eq(sid)) .get_result::(&*con) } diff --git a/hammond-data/src/models/insertables.rs b/hammond-data/src/models/insertables.rs index a804a13..a0d950d 100644 --- a/hammond-data/src/models/insertables.rs +++ b/hammond-data/src/models/insertables.rs @@ -105,11 +105,11 @@ impl NewPodcast { // Look out for when tryinto lands into stable. pub fn into_podcast(self) -> Result { self.index()?; - Ok(dbqueries::get_podcast_from_title(&self.title)?) + Ok(dbqueries::get_podcast_from_source_id(self.source_id)?) } pub fn index(&self) -> QueryResult<()> { - let pd = dbqueries::get_podcast_from_title(&self.title); + let pd = dbqueries::get_podcast_from_source_id(self.source_id); let db = connection(); let con = db.get().unwrap(); @@ -119,7 +119,9 @@ impl NewPodcast { error!("NPD sid: {}, PD sid: {}", self.source_id, foo.source_id()); }; - if foo.link() != self.link { + if (foo.link() != self.link) || (foo.title() != self.title) + || (foo.image_uri() != self.image_uri.as_ref().map(|x| x.as_str())) + { dbqueries::replace_podcast(&con, self)?; } } diff --git a/hammond-downloader/src/downloader.rs b/hammond-downloader/src/downloader.rs index 284cc3d..5baf8a8 100644 --- a/hammond-downloader/src/downloader.rs +++ b/hammond-downloader/src/downloader.rs @@ -194,6 +194,7 @@ mod tests { use super::*; use hammond_data::Source; use hammond_data::dbqueries; + use diesel::Identifiable; use std::fs; @@ -208,14 +209,16 @@ mod tests { fn test_cache_image() { let url = "http://www.newrustacean.com/feed.xml"; - Source::from_url(url) - .unwrap() - .into_feed() - .unwrap() - .index() - .unwrap(); + // Create and index a source + let source = Source::from_url(url).unwrap(); + // Copy it's id + let sid = source.id().clone(); - let pd = dbqueries::get_podcast_from_title("New Rustacean").unwrap(); + // Convert Source it into a Feed and index it + source.into_feed().unwrap().index().unwrap(); + + // Get the Podcast + let pd = dbqueries::get_podcast_from_source_id(sid).unwrap(); let img_path = cache_image(&pd); let foo_ = format!( diff --git a/hammond-gtk/src/widgets/podcast.rs b/hammond-gtk/src/widgets/podcast.rs index 8de5d26..0117a2c 100644 --- a/hammond-gtk/src/widgets/podcast.rs +++ b/hammond-gtk/src/widgets/podcast.rs @@ -119,21 +119,23 @@ pub fn update_podcast_widget(stack: >k::Stack, pd: &Podcast) { #[cfg(test)] mod tests { use hammond_data::Source; + use diesel::Identifiable; use super::*; #[test] fn test_get_pixbuf_from_path() { let url = "http://www.newrustacean.com/feed.xml"; - Source::from_url(url) - .unwrap() - .into_feed() - .unwrap() - .index() - .unwrap(); + // Create and index a source + let source = Source::from_url(url).unwrap(); + // Copy it's id + let sid = source.id().clone(); - let pd = dbqueries::get_podcast_from_title("New Rustacean").unwrap(); + // Convert Source it into a Feed and index it + source.into_feed().unwrap().index().unwrap(); + // Get the Podcast + let pd = dbqueries::get_podcast_from_source_id(sid).unwrap(); let pxbuf = get_pixbuf_from_path(&pd); assert!(pxbuf.is_some()); }