Commit 0425ba81 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

AlbumTrack: Drop is_present flag

The album now relies on the media is_present flag to compute its overall
presence.
This drops a layer of indirrection when (un)plugging a removable device
parent f7e5031f
......@@ -433,7 +433,7 @@ void Album::createTable( sqlite::Connection* dbConnection )
"thumbnail_id UNSIGNED INT,"
"nb_tracks UNSIGNED INTEGER DEFAULT 0,"
"duration UNSIGNED INTEGER NOT NULL DEFAULT 0,"
"is_present BOOLEAN NOT NULL DEFAULT 1,"
"is_present BOOLEAN NOT NULL DEFAULT 0,"
"FOREIGN KEY( artist_id ) REFERENCES " + Artist::Table::Name
+ "(id_artist) ON DELETE CASCADE,"
"FOREIGN KEY(thumbnail_id) REFERENCES " + Thumbnail::Table::Name
......@@ -464,14 +464,16 @@ void Album::createTriggers( sqlite::Connection* dbConnection )
const std::string indexReq = "CREATE INDEX IF NOT EXISTS album_artist_id_idx ON " +
Table::Name + "(artist_id)";
static const std::string triggerReq = "CREATE TRIGGER IF NOT EXISTS is_album_present AFTER UPDATE OF "
"is_present ON " + AlbumTrack::Table::Name +
"is_present ON " + Media::Table::Name +
" WHEN new.subtype = " +
std::to_string( static_cast<typename std::underlying_type<IMedia::SubType>::type>(
IMedia::SubType::AlbumTrack ) ) +
" BEGIN "
" UPDATE " + Table::Name + " SET is_present="
"(SELECT EXISTS("
"SELECT id_track FROM " + AlbumTrack::Table::Name +
" WHERE album_id=new.album_id AND is_present != 0 LIMIT 1"
") )"
"WHERE id_album=new.album_id;"
" UPDATE " + Table::Name + " SET is_present=is_present + "
"(CASE new.is_present WHEN 0 THEN -1 ELSE 1 END)"
"WHERE id_album = (SELECT album_id FROM " + AlbumTrack::Table::Name + " "
"WHERE media_id = new.id_media"
");"
" END";
static const std::string deleteTriggerReq = "CREATE TRIGGER IF NOT EXISTS delete_album_track AFTER DELETE ON "
+ AlbumTrack::Table::Name +
......@@ -479,6 +481,7 @@ void Album::createTriggers( sqlite::Connection* dbConnection )
" UPDATE " + Table::Name +
" SET"
" nb_tracks = nb_tracks - 1,"
" is_present = is_present - 1,"
" duration = duration - old.duration"
" WHERE id_album = old.album_id;"
" DELETE FROM " + Table::Name +
......@@ -489,7 +492,8 @@ void Album::createTriggers( sqlite::Connection* dbConnection )
" BEGIN"
" UPDATE " + Table::Name +
" SET duration = duration + new.duration,"
" nb_tracks = nb_tracks + 1"
" nb_tracks = nb_tracks + 1,"
" is_present = is_present + 1"
" WHERE id_album = new.album_id;"
" END";
static const std::string vtriggerInsert = "CREATE TRIGGER IF NOT EXISTS insert_album_fts AFTER INSERT ON "
......@@ -564,8 +568,10 @@ Query<IAlbum> Album::fromArtist( MediaLibraryPtr ml, int64_t artistId, const Que
std::string req = "FROM " + Table::Name + " alb "
"INNER JOIN " + AlbumTrack::Table::Name + " att "
"ON att.album_id = alb.id_album "
"INNER JOIN " + Media::Table::Name + " m "
"ON att.media_id = m.id_media "
"WHERE (att.artist_id = ? OR alb.artist_id = ?) "
"AND att.is_present != 0 ";
"AND m.is_present != 0 ";
std::string groupAndOrder = "GROUP BY att.album_id ORDER BY ";
auto sort = params != nullptr ? params->sort : SortingCriteria::Default;
auto desc = params != nullptr ? params->desc : false;
......
......@@ -50,7 +50,6 @@ AlbumTrack::AlbumTrack( MediaLibraryPtr ml, sqlite::Row& row )
, m_trackNumber( row.load<decltype(m_trackNumber)>( 5 ) )
, m_albumId( row.load<decltype(m_albumId)>( 6 ) )
, m_discNumber( row.load<decltype(m_discNumber)>( 7 ) )
, m_isPresent( row.load<decltype(m_isPresent)>( 8 ) )
{
}
......@@ -64,7 +63,6 @@ AlbumTrack::AlbumTrack( MediaLibraryPtr ml, int64_t mediaId, int64_t artistId, i
, m_trackNumber( trackNumber )
, m_albumId( albumId )
, m_discNumber( discNumber )
, m_isPresent( true )
{
}
......@@ -101,7 +99,6 @@ void AlbumTrack::createTable( sqlite::Connection* dbConnection )
"track_number UNSIGNED INTEGER,"
"album_id UNSIGNED INTEGER NOT NULL,"
"disc_number UNSIGNED INTEGER,"
"is_present BOOLEAN NOT NULL DEFAULT 1,"
"FOREIGN KEY (media_id) REFERENCES " + Media::Table::Name + "(id_media)"
" ON DELETE CASCADE,"
"FOREIGN KEY (artist_id) REFERENCES " + Artist::Table::Name + "(id_artist)"
......@@ -118,18 +115,10 @@ void AlbumTrack::createTable( sqlite::Connection* dbConnection )
void AlbumTrack::createTriggers(sqlite::Connection* dbConnection)
{
const std::string triggerReq = "CREATE TRIGGER IF NOT EXISTS is_track_present "
"AFTER UPDATE OF is_present "
"ON " + Media::Table::Name + " "
"BEGIN "
"UPDATE " + AlbumTrack::Table::Name + " "
"SET is_present = new.is_present WHERE media_id = new.id_media;"
"END";
const std::string indexReq = "CREATE INDEX IF NOT EXISTS "
"album_media_artist_genre_album_idx ON " +
AlbumTrack::Table::Name +
"(media_id, artist_id, genre_id, album_id)";
sqlite::Tools::executeRequest( dbConnection, triggerReq );
sqlite::Tools::executeRequest( dbConnection, indexReq );
}
......
......@@ -82,7 +82,6 @@ class AlbumTrack : public IAlbumTrack, public DatabaseHelpers<AlbumTrack>
const unsigned int m_trackNumber;
const int64_t m_albumId;
const unsigned int m_discNumber;
bool m_isPresent;
mutable Cache<std::weak_ptr<Album>> m_album;
mutable Cache<std::shared_ptr<Artist>> m_artist;
......
......@@ -209,6 +209,17 @@ IMedia::Type::Unknown ) ),
/******************* Delete removed triggers **********************************/
"DROP TRIGGER on_track_genre_changed",
// This trigger has changed and needs to be recreated
// However if migrating from a model below version 13, migration 12->13 will have
// already dropped it
"DROP TRIGGER IF EXISTS is_album_present",
// Other outdated triggers that we want to regenerate, however those we are sure
// to find and be able to delete (hence no `IF EXISTS` in the request)
"DROP TRIGGER add_album_track",
"DROP TRIGGER delete_album_track",
// Old Folder -> File is_present trigger
// is_folder_present has been implicitely removed by dropping the File table
......
......@@ -174,7 +174,7 @@ TEST_F( DbModel, Upgrade12to13 )
// We can't check for the number of albums anymore since they are deleted
// as part of 13 -> 14 migration
CheckNbTriggers( 31 );
CheckNbTriggers( 30 );
}
TEST_F( DbModel, Upgrade13to14 )
......@@ -217,5 +217,5 @@ TEST_F( DbModel, Upgrade13to14 )
ASSERT_EQ( 0u, std::static_pointer_cast<Media>( externalMedia )->nbPlaylists() );
CheckNbTriggers( 31 );
CheckNbTriggers( 30 );
}
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