Commit 62ff6dcc authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

Genre: Ensure nb track trigger won't violate CHECK constraint

Refs #425
parent 0a8cd45e
......@@ -289,14 +289,27 @@ std::string Genre::trigger( Triggers trigger, uint32_t dbModel )
" WHERE nb_tracks = 0;"
" END";
}
if ( dbModel == 34 )
{
return "CREATE TRIGGER " + triggerName( trigger, dbModel ) +
" AFTER DELETE ON " + Media::Table::Name +
" WHEN old.subtype = " +
utils::enum_to_string( IMedia::SubType::AlbumTrack ) +
" BEGIN"
" UPDATE " + Table::Name +
" SET nb_tracks = nb_tracks - 1,"
" is_present = is_present - IIF(old.is_present != 0, 1, 0)"
" WHERE id_genre = old.genre_id;"
" END";
}
return "CREATE TRIGGER " + triggerName( trigger, dbModel ) +
" AFTER DELETE ON " + Media::Table::Name +
" WHEN old.subtype = " +
utils::enum_to_string( IMedia::SubType::AlbumTrack ) +
" BEGIN"
" UPDATE " + Table::Name +
" SET nb_tracks = nb_tracks - 1,"
" is_present = is_present - IIF(old.is_present != 0, 1, 0)"
" SET is_present = is_present - IIF(old.is_present != 0, 1, 0),"
" nb_tracks = nb_tracks - 1"
" WHERE id_genre = old.genre_id;"
" END";
case Triggers::UpdateIsPresent:
......
......@@ -2029,6 +2029,13 @@ void MediaLibrary::migrateModel34to35()
sqlite::Connection::WeakDbContext weakConnCtx{ dbConn };
auto t = dbConn->newTransaction();
std::string reqs[] = {
# include "database/migrations/migration34-35.sql"
};
for ( const auto& req : reqs )
sqlite::Tools::executeRequest( dbConn, req );
m_settings.setDbModelVersion( 35 );
t->commit();
}
......
/* Ensure we update is_present before the total number of track */
"DROP TRIGGER " + Genre::triggerName( Genre::Triggers::UpdateOnTrackDelete, 34 ),
Genre::trigger( Genre::Triggers::UpdateOnTrackDelete, 35 ),
Supports Markdown
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