Commit 76cb9c06 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Ensure is_present will always be 0/1 for Album/Artist/Media

This is a fixup for commits:
- d71d36bd
- 1a0d7284
- 41f045b9
parent 0fc6b3bb
......@@ -376,8 +376,10 @@ bool Album::createTriggers( sqlite::Connection* dbConnection )
"is_present ON " + policy::AlbumTrackTable::Name +
" BEGIN "
" UPDATE " + policy::AlbumTable::Name + " SET is_present="
"(SELECT COUNT(id_track) FROM " + policy::AlbumTrackTable::Name +
" WHERE album_id=new.album_id AND is_present=1 LIMIT 1) "
"(SELECT EXISTS("
"SELECT id_track FROM " + policy::AlbumTrackTable::Name +
" WHERE album_id=new.album_id AND is_present=1 LIMIT 1"
") )"
"WHERE id_album=new.album_id;"
" END";
static const std::string deleteTriggerReq = "CREATE TRIGGER IF NOT EXISTS delete_album_track AFTER DELETE ON "
......
......@@ -226,8 +226,10 @@ bool Artist::createTriggers( sqlite::Connection* dbConnection )
"is_present ON " + policy::AlbumTable::Name +
" BEGIN "
" UPDATE " + policy::ArtistTable::Name + " SET is_present="
"(SELECT COUNT(id_album) FROM " + policy::AlbumTable::Name +
" WHERE artist_id=new.artist_id AND is_present=1 LIMIT 1) "
"(SELECT EXISTS("
"SELECT id_album FROM " + policy::AlbumTable::Name +
" WHERE artist_id=new.artist_id AND is_present=1 LIMIT 1"
") )"
"WHERE id_artist=new.artist_id;"
" END";
// Automatically delete the artists that don't have any albums left, except the 2 special artists.
......
......@@ -555,8 +555,10 @@ bool Media::createTriggers( sqlite::Connection* connection )
"is_present ON " + policy::FileTable::Name +
" BEGIN "
" UPDATE " + policy::MediaTable::Name + " SET is_present="
"(SELECT COUNT(id_file) FROM " + policy::FileTable::Name +
" WHERE media_id=new.media_id AND is_present=1 LIMIT 1) "
"(SELECT EXISTS("
"SELECT id_file FROM " + policy::FileTable::Name +
" WHERE media_id=new.media_id AND is_present=1 LIMIT 1"
") )"
"WHERE id_media=new.media_id;"
" END;";
static const std::string triggerReq2 = "CREATE TRIGGER IF NOT EXISTS cascade_file_deletion AFTER DELETE ON "
......
......@@ -244,16 +244,20 @@ TEST_F( DeviceFs, RemoveAlbum )
// And an album that will disappear, along with its artist
{
auto album = std::static_pointer_cast<Album>( ml->createAlbum( "album 2" ) );
auto album2 = std::static_pointer_cast<Album>( ml->createAlbum( "album 3" ) );
auto media = ml->media( RemovableDeviceMountpoint + "removablefile.mp3" );
ml->media( RemovableDeviceMountpoint + "removablefile2.mp3" );
auto artist = ml->createArtist( "artist 2" );
album->addTrack( std::static_pointer_cast<Media>( media ), 1, 1, artist->id(), nullptr );
album->addTrack( std::static_pointer_cast<Media>( media ), 2, 1, artist->id(), nullptr );
album2->addTrack( std::static_pointer_cast<Media>( media ), 1, 1, artist->id(), nullptr );
album2->addTrack( std::static_pointer_cast<Media>( media ), 2, 1, artist->id(), nullptr );
album->setAlbumArtist( artist );
album2->setAlbumArtist( artist );
}
auto albums = ml->albums( SortingCriteria::Default, false );
ASSERT_EQ( 2u, albums.size() );
ASSERT_EQ( 3u, albums.size() );
auto artists = ml->artists( SortingCriteria::Default, false );
ASSERT_EQ( 2u, artists.size() );
......@@ -265,6 +269,17 @@ TEST_F( DeviceFs, RemoveAlbum )
ASSERT_EQ( 1u, albums.size() );
artists = ml->artists( SortingCriteria::Default, false );
ASSERT_EQ( 1u, artists.size() );
// Now check that everything appears again when we plug the device back in
fsMock->addDevice( device );
Reload();
albums = ml->albums( SortingCriteria::Default, false );
ASSERT_EQ( 3u, albums.size() );
artists = ml->artists( SortingCriteria::Default, false );
ASSERT_EQ( 2u, artists.size() );
}
TEST_F( DeviceFs, PartialAlbumRemoval )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment