Commit 87a35bf0 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Album: Update duration when tracks get deleted

parent e981a8d4
......@@ -379,7 +379,11 @@ bool Album::createTriggers(DBConnection dbConnection)
static const std::string deleteTriggerReq = "CREATE TRIGGER IF NOT EXISTS delete_album_track AFTER DELETE ON "
+ policy::AlbumTrackTable::Name +
" BEGIN "
" UPDATE " + policy::AlbumTable::Name + " SET nb_tracks = nb_tracks - 1 WHERE id_album = old.album_id;"
" UPDATE " + policy::AlbumTable::Name +
" SET"
" nb_tracks = nb_tracks - 1,"
" duration = duration - max(0, (SELECT duration FROM " + policy::MediaTable::Name + " WHERE id_media=old.media_id))"
" WHERE id_album = old.album_id;"
" DELETE FROM " + policy::AlbumTable::Name +
" WHERE id_album=old.album_id AND nb_tracks = 0;"
" END";
......
......@@ -451,16 +451,29 @@ TEST_F( Albums, Duration )
auto m2 = std::static_pointer_cast<Media>( ml->addMedia( "track2.mp3" ) );
m2->setDuration( 200 );
m2->save();
a->addTrack( m2, 1, 1, 0, 0 );
auto t2 = a->addTrack( m2, 1, 1, 0, 0 );
ASSERT_EQ( 300u, a->duration() );
// Check that we don't add negative durations (default sqlite duration is -1)
auto m3 = std::static_pointer_cast<Media>( ml->addMedia( "track3.mp3" ) );
a->addTrack( m3, 1, 1, 0, 0 );
auto t3 = a->addTrack( m3, 1, 1, 0, 0 );
ASSERT_EQ( 300u, a->duration() );
Reload();
auto a2 = ml->album( a->id() );
ASSERT_EQ( 300u, a2->duration() );
// Check that the duration is updated when a media/track gets removed
ml->deleteTrack( t2->id() );
Reload();
a2 = ml->album( a->id() );
ASSERT_EQ( 100u, a2->duration() );
// And check that we don't remove negative durations
ml->deleteTrack( t3->id() );
Reload();
a2 = ml->album( a->id() );
ASSERT_EQ( 100u, a2->duration() );
}
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