From 02444de7fa43c8a8a50b28719da3a7c5b097809e Mon Sep 17 00:00:00 2001 From: Cameron Cordes Date: Wed, 22 Mar 2023 13:51:32 -0400 Subject: [PATCH] Fix tagging file with new tag When tagging a file with a brand new tag, we were using the number of rows affected as the ID instead of doing the query for the ID of the row we just inserted, this should fix when we tag a photo with a new tag. --- src/tags.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/tags.rs b/src/tags.rs index a55fbe5..a481b8f 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -185,7 +185,7 @@ pub struct SqliteTagDao { } impl SqliteTagDao { - fn new(connection: SqliteConnection) -> Self { + pub(crate) fn new(connection: SqliteConnection) -> Self { SqliteTagDao { connection } } } @@ -235,8 +235,7 @@ impl TagDao for SqliteTagDao { .and_then(|id| { debug!("Got id: {:?} for inserted tag: {:?}", id, name); tags::table - .left_join(tagged_photo::table) - .filter(tagged_photo::id.eq(id)) + .filter(tags::id.eq(id)) .select((tags::id, tags::name, tags::created_time)) .get_result::(self.connection.borrow_mut()) .with_context(|| "Unable to get tagged photo from Sqlite") @@ -274,13 +273,29 @@ impl TagDao for SqliteTagDao { created_time: Utc::now().timestamp(), }) .execute(self.connection.borrow_mut()) - .with_context(|| "Unable to tag file in sqlite") + .with_context(|| format!("Unable to tag file {:?} in sqlite", path)) + .and_then(|_| { + debug!("Inserted tagged photo: {:#} -> {:?}", tag_id, path); + no_arg_sql_function!( + last_insert_rowid, + diesel::sql_types::Integer, + "Represents the SQL last_insert_row() function" + ); + diesel::select(last_insert_rowid) + .get_result::(&mut self.connection) + .with_context(|| "Unable to get last inserted tag from Sqlite") + }) .and_then(|tagged_id| { debug!("Inserted tagged photo: {:?}", tagged_id); tagged_photo::table .find(tagged_id as i32) .first(self.connection.borrow_mut()) - .with_context(|| "Error getting inserted tagged photo") + .with_context(|| { + format!( + "Error getting inserted tagged photo with id: {:?}", + tagged_id + ) + }) }) } }