Skip to content

Commit

Permalink
#704 Try to get rid of empty album entries when an artist name changes.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mherger committed Jan 29, 2022
1 parent f98f746 commit da4d2e1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
1 change: 1 addition & 0 deletions Changelog8.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ <h2><a name="v8.3.0" id="v8.3.0"></a>Version 8.3.0</h2>
<li><a href="https://github.com/Logitech/slimserver/pull/668">#668</a> - Podcasts: Pre-caching image and more-info data can bring the server to a crawl #668 (thanks mw9!)</li>
<li>Fix image transformation if a cover requested using /current/cover is pointing to a local file.</li>
<li><a href="https://github.com/Logitech/slimserver/issues/700">#700</a>/<a href="https://github.com/Logitech/slimserver/pull/718">#718</a> - High CPU load during playback of certain radio streams (thanks philippe44!)</li>
<li><a href="https://github.com/Logitech/slimserver/issues/704">#704</a> - changed artist names remain in database after quick rescan</li>
<li><a href="https://github.com/Logitech/slimserver/pull/749">#749</a> - fix mp4 streams where audio offset comes from STCO (thanks philippe44 && bpa!)</li>
</ul>
<br />
Expand Down
15 changes: 14 additions & 1 deletion Slim/Schema.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
31 changes: 23 additions & 8 deletions Slim/Schema/Contributor.pm
Original file line number Diff line number Diff line change
Expand Up @@ -221,26 +221,41 @@ 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");

# 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 );
}
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions Slim/Utils/Scanner/Local.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}


Expand Down Expand Up @@ -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} );
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit da4d2e1

Please sign in to comment.