Commit 05b177e6 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Album: Auto delete album with no tracks left

parent ee901bb3
......@@ -300,6 +300,13 @@ bool Album::createTriggers(DBConnection dbConnection)
"(SELECT COUNT(id_track) FROM " + policy::AlbumTrackTable::Name + " WHERE album_id=new.album_id AND is_present=1) "
"WHERE id_album=new.album_id;"
" END";
static const std::string deleteTriggerReq = "CREATE TRIGGER IF NOT EXISTS delete_album AFTER DELETE ON "
+ policy::AlbumTrackTable::Name +
" BEGIN "
" DELETE FROM " + policy::AlbumTable::Name +
" WHERE id_album=old.album_id AND "
"(SELECT COUNT(id_track) FROM " + policy::AlbumTrackTable::Name + " WHERE album_id=old.album_id) = 0;"
" END";
static const std::string vtriggerInsert = "CREATE TRIGGER IF NOT EXISTS insert_album_fts AFTER INSERT ON "
+ policy::AlbumTable::Name +
// Skip unknown albums
......@@ -315,6 +322,7 @@ bool Album::createTriggers(DBConnection dbConnection)
" DELETE FROM " + policy::AlbumTable::Name + "Fts WHERE rowid = old.id_album;"
" END";
return sqlite::Tools::executeRequest( dbConnection, triggerReq ) &&
sqlite::Tools::executeRequest( dbConnection, deleteTriggerReq ) &&
sqlite::Tools::executeRequest( dbConnection, vtriggerInsert ) &&
sqlite::Tools::executeRequest( dbConnection, vtriggerDelete );
}
......
......@@ -306,3 +306,18 @@ TEST_F( Albums, SearchAfterArtistUpdate )
albums = ml->searchAlbums( "ice" );
ASSERT_EQ( 1u, albums.size() );
}
TEST_F( Albums, AutoDelete )
{
auto a = ml->createAlbum( "album" );
auto m = ml->addFile( "media.mp3" );
auto t = a->addTrack( m, 1, 1 );
auto album = ml->album( a->id() );
ASSERT_NE( nullptr, album );
ml->deleteTrack( t->id() );
album = ml->album( a->id() );
ASSERT_EQ( nullptr, album );
}
......@@ -25,6 +25,7 @@
#include <memory>
#include "Album.h"
#include "AlbumTrack.h"
#include "Artist.h"
#include "Device.h"
#include "File.h"
......@@ -166,3 +167,8 @@ void MediaLibraryTester::setFsFactory(std::shared_ptr<factory::IFileSystem> fsFa
{
m_fsFactory = fsFactory;
}
void MediaLibraryTester::deleteTrack(unsigned int trackId)
{
AlbumTrack::destroy( this, trackId );
}
......@@ -44,6 +44,7 @@ public:
void deleteArtist( unsigned int artistId );
std::shared_ptr<Device> addDevice( const std::string& uuid, bool isRemovable );
void setFsFactory( std::shared_ptr<factory::IFileSystem> fsFactory );
void deleteTrack( unsigned int trackId );
private:
std::unique_ptr<fs::IDirectory> dummyDirectory;
......
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