From da4d2e14876417dc8e8159f6a07dee24d75aff35 Mon Sep 17 00:00:00 2001 From: Michael Herger Date: Sat, 29 Jan 2022 01:04:09 +0100 Subject: [PATCH] #704 Try to get rid of empty album entries when an artist name changes. When an album's artist name changed, but other albums of the old artist name remained, an empty album would have showed up under the old name. This happened because we didn't clean up old entries in the `contributor_album` table. --- Changelog8.html | 1 + Slim/Schema.pm | 15 ++++++++++++++- Slim/Schema/Contributor.pm | 31 +++++++++++++++++++++++-------- Slim/Utils/Scanner/Local.pm | 6 +++--- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/Changelog8.html b/Changelog8.html index 49604bb2f6a..0a8575fac57 100644 --- a/Changelog8.html +++ b/Changelog8.html @@ -42,6 +42,7 @@

Version 8.3.0

  • #668 - Podcasts: Pre-caching image and more-info data can bring the server to a crawl #668 (thanks mw9!)
  • Fix image transformation if a cover requested using /current/cover is pointing to a local file.
  • #700/#718 - High CPU load during playback of certain radio streams (thanks philippe44!)
  • +
  • #704 - changed artist names remain in database after quick rescan
  • #749 - fix mp4 streams where audio offset comes from STCO (thanks philippe44 && bpa!)

  • diff --git a/Slim/Schema.pm b/Slim/Schema.pm index 864a0391503..8566e568d59 100644 --- a/Slim/Schema.pm +++ b/Slim/Schema.pm @@ -2871,11 +2871,16 @@ sub _postCheckAttributes { # Walk through the valid contributor roles, adding them to the database. my $contributors = $self->_mergeAndCreateContributors($attributes, $isCompilation, $create); + my $artist = $contributors->{ARTIST} || $contributors->{TRACKARTIST}; + if ($artist) { + $cols{primary_artist} = $artist->[0]; + } + ### Update Album row my $albumId = $self->_createOrUpdateAlbum($attributes, \%cols, # trackColumns $isCompilation, - $contributors->{'ALBUMARTIST'}->[0] || $contributors->{'ARTIST'}->[0], # primary contributor-id + $artist->[0], # primary contributor-id defined $contributors->{'ALBUMARTIST'}->[0] ? 1 : 0, # hasAlbumArtist $create, # create $track, # Track @@ -3000,6 +3005,14 @@ sub _createContributorRoleRelationships { $sth_delete_tracks->execute($trackId); # Using native DBI here to improve performance during scanning + if ( my $artist = $contributors->{ARTIST} || $contributors->{TRACKARTIST} ) { + my $sth_track_artist = $self->dbh->prepare_cached( qq( + UPDATE tracks + SET primary_artist = ? + WHERE id = ? + ) ); + $sth_track_artist->execute( $artist->[0], $trackId ); + } my $sth_track = $self->dbh->prepare_cached( qq{ REPLACE INTO contributor_track diff --git a/Slim/Schema/Contributor.pm b/Slim/Schema/Contributor.pm index 45df1014ebe..c5c3cd6c56e 100644 --- a/Slim/Schema/Contributor.pm +++ b/Slim/Schema/Contributor.pm @@ -221,19 +221,24 @@ sub isInLibrary { # Rescan list of contributors, this simply means to make sure at least 1 track # from this contributor still exists in the database. If not, delete the contributor. sub rescan { - my ( $class, @ids ) = @_; + my ( $class, $ids, $albumId ) = @_; my $log = logger('scan.scanner'); my $dbh = Slim::Schema->dbh; - for my $id ( @ids ) { - my $sth = $dbh->prepare_cached( qq{ - SELECT COUNT(*) FROM contributor_track WHERE contributor = ? - } ); - $sth->execute($id); - my ($count) = $sth->fetchrow_array; - $sth->finish; + my $contributorSth = $dbh->prepare_cached( qq{ + SELECT COUNT(*) FROM contributor_track WHERE contributor = ? + } ); + + my $albumSth = $dbh->prepare_cached( qq{ + SELECT COUNT(1) FROM tracks WHERE album = ? AND primary_artist = ? + } ); + + for my $id ( @$ids ) { + $contributorSth->execute($id); + my ($count) = $contributorSth->fetchrow_array; + $contributorSth->finish; if ( !$count ) { main::DEBUGLOG && $log->is_debug && $log->debug("Removing unused contributor: $id"); @@ -241,6 +246,16 @@ sub rescan { # This will cascade within the database to contributor_album and contributor_track $dbh->do( "DELETE FROM contributors WHERE id = ?", undef, $id ); } + # contributor->album relations aren't removed automatically when the last track with this primary_artist disappears + elsif ( $albumId ) { + $albumSth->execute($albumId, $id); + ($count) = $albumSth->fetchrow_array; + $albumSth->finish; + + if ( !$count ) { + $dbh->do( "DELETE FROM contributor_album WHERE role = 1 AND album = ? AND contributor = ?", undef, $albumId, $id ); + } + } } } diff --git a/Slim/Utils/Scanner/Local.pm b/Slim/Utils/Scanner/Local.pm index 22e61b33f53..d1154fceda1 100644 --- a/Slim/Utils/Scanner/Local.pm +++ b/Slim/Utils/Scanner/Local.pm @@ -682,7 +682,7 @@ sub deleted { # Tell Contributors to rescan, if no other tracks left, remove contributor. # This will also remove entries from contributor_track and contributor_album for ( @$contribs ) { - Slim::Schema::Contributor->rescan( $_->[0] ); + Slim::Schema::Contributor->rescan( [$_->[0]], $album && $album->id ); } @@ -801,7 +801,7 @@ sub deleted { Slim::Schema::Album->rescan( @albums ); # 3. Rescan contributors created from the cue sheet - Slim::Schema::Contributor->rescan( map { $_->{contributor} } @{$contribs} ); + Slim::Schema::Contributor->rescan( [map { $_->{contributor} } @{$contribs}] ); # 4. Rescan genres created from the cue sheet Slim::Schema::Genre->rescan( map { $_->{genre} } @{$genres} ); @@ -1001,7 +1001,7 @@ sub changed { # Tell Contributors to rescan, if no other tracks left, remove contributor. # This will also remove entries from contributor_track and contributor_album for my $contrib ( @{ $orig->{contribs} } ) { - Slim::Schema::Contributor->rescan( $contrib->{contributor} ); + Slim::Schema::Contributor->rescan( [$contrib->{contributor}], $origTrack->{album_id} ); } my $album = $track->album;