@@ -85,7 +85,7 @@ pub async fn rescan<C: Connection>(
8585 } ;
8686 info ! (
8787 "{} has conflicting metadata with index, {log_postfix}" ,
88- path. to_string_lossy ( ) ,
88+ path. display ( ) ,
8989 ) ;
9090
9191 match conflict_resolution_mode {
@@ -103,9 +103,8 @@ pub async fn rescan<C: Connection>(
103103 // if we have an error, delete the song from the library
104104 Err ( e) => {
105105 warn ! (
106- "Error reading metadata for {}: {}" ,
107- path. to_string_lossy( ) ,
108- e
106+ "Error reading metadata for {}: {e}" ,
107+ path. display( )
109108 ) ;
110109 info ! ( "assuming the file isn't a song or doesn't exist anymore, removing from library" ) ;
111110 Song :: delete ( db, song. id ) . await ?;
@@ -143,6 +142,8 @@ pub async fn rescan<C: Connection>(
143142
144143 visited_paths. insert ( path. path ( ) . to_owned ( ) ) ;
145144
145+ let displayable_path = path. path ( ) . display ( ) ;
146+
146147 // if the file is a song, add it to the library
147148 match SongMetadata :: load_from_path (
148149 path. path ( ) . to_owned ( ) ,
@@ -151,14 +152,10 @@ pub async fn rescan<C: Connection>(
151152 genre_separator,
152153 ) {
153154 Ok ( metadata) => Song :: try_load_into_db ( db, metadata) . await . map_or_else (
154- |e| warn ! ( "Error indexing {}: {}" , path. path( ) . to_string_lossy( ) , e) ,
155- |_| debug ! ( "Indexed {}" , path. path( ) . to_string_lossy( ) ) ,
156- ) ,
157- Err ( e) => warn ! (
158- "Error reading metadata for {}: {}" ,
159- path. path( ) . to_string_lossy( ) ,
160- e
155+ |e| warn ! ( "Error indexing {displayable_path}: {e}" ) ,
156+ |_| debug ! ( "Indexed {displayable_path}" ) ,
161157 ) ,
158+ Err ( e) => warn ! ( "Error reading metadata for {displayable_path}: {e}" ) ,
162159 }
163160 }
164161
@@ -168,52 +165,34 @@ pub async fn rescan<C: Connection>(
168165 . await ?;
169166
170167 // find and delete any remaining orphaned albums and artists
171- // TODO: create a custom query for this
172168
173- async {
174- for album in Album :: read_all ( db) . await ? {
175- if Album :: repair ( db, album. id . clone ( ) ) . await ? {
176- info ! ( "Deleted orphaned album {}" , album. id. clone( ) ) ;
177- Album :: delete ( db, album. id . clone ( ) ) . await ?;
178- }
179- }
180- <Result < ( ) , Error > >:: Ok ( ( ) )
169+ let orphans = Album :: delete_orphaned ( db)
170+ . instrument ( tracing:: info_span!( "Deleting orphaned albums" ) )
171+ . await ?;
172+ if !orphans. is_empty ( ) {
173+ info ! ( "Deleted orphaned albums: {orphans:?}" ) ;
181174 }
182- . instrument ( tracing:: info_span!( "Repairing albums" ) )
183- . await ?;
184- async {
185- for artist in Artist :: read_all ( db) . await ? {
186- if Artist :: repair ( db, artist. id . clone ( ) ) . await ? {
187- info ! ( "Deleted orphaned artist {}" , artist. id. clone( ) ) ;
188- Artist :: delete ( db, artist. id . clone ( ) ) . await ?;
189- }
190- }
191- <Result < ( ) , Error > >:: Ok ( ( ) )
175+
176+ let orphans = Artist :: delete_orphaned ( db)
177+ . instrument ( tracing:: info_span!( "Repairing artists" ) )
178+ . await ?;
179+ if !orphans. is_empty ( ) {
180+ info ! ( "Deleted orphaned artists: {orphans:?}" ) ;
192181 }
193- . instrument ( tracing:: info_span!( "Repairing artists" ) )
194- . await ?;
195- async {
196- for collection in Collection :: read_all ( db) . await ? {
197- if Collection :: repair ( db, collection. id . clone ( ) ) . await ? {
198- info ! ( "Deleted orphaned collection {}" , collection. id. clone( ) ) ;
199- Collection :: delete ( db, collection. id . clone ( ) ) . await ?;
200- }
201- }
202- <Result < ( ) , Error > >:: Ok ( ( ) )
182+
183+ let orphans = Collection :: delete_orphaned ( db)
184+ . instrument ( tracing:: info_span!( "Repairing collections" ) )
185+ . await ?;
186+ if !orphans. is_empty ( ) {
187+ info ! ( "Deleted orphaned collections: {orphans:?}" ) ;
203188 }
204- . instrument ( tracing:: info_span!( "Repairing collections" ) )
205- . await ?;
206- async {
207- for playlist in Playlist :: read_all ( db) . await ? {
208- if Playlist :: repair ( db, playlist. id . clone ( ) ) . await ? {
209- info ! ( "Deleted orphaned playlist {}" , playlist. id. clone( ) ) ;
210- Playlist :: delete ( db, playlist. id . clone ( ) ) . await ?;
211- }
212- }
213- <Result < ( ) , Error > >:: Ok ( ( ) )
189+
190+ let orphans = Playlist :: delete_orphaned ( db)
191+ . instrument ( tracing:: info_span!( "Repairing playlists" ) )
192+ . await ?;
193+ if !orphans. is_empty ( ) {
194+ info ! ( "Deleted orphaned playlists: {orphans:?}" ) ;
214195 }
215- . instrument ( tracing:: info_span!( "Repairing playlists" ) )
216- . await ?;
217196
218197 info ! ( "Library rescan complete" ) ;
219198 info ! ( "Library brief: {:?}" , brief( db) . await ?) ;
@@ -747,6 +726,11 @@ mod tests {
747726 // check that the album and artist deleted
748727 assert_eq ! ( Song :: read_all( & db) . await . unwrap( ) . len( ) , 0 ) ;
749728 assert_eq ! ( Album :: read_all( & db) . await . unwrap( ) . len( ) , 0 ) ;
729+ let artists = Artist :: read_all ( & db) . await . unwrap ( ) ;
730+ for artist in artists {
731+ assert_eq ! ( artist. album_count, 0 ) ;
732+ assert_eq ! ( artist. song_count, 0 ) ;
733+ }
750734 assert_eq ! ( Artist :: read_all( & db) . await . unwrap( ) . len( ) , 0 ) ;
751735 }
752736
0 commit comments