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

MediaLibrary: Handle updates from 3 to 8 without recreating everything

parent c9906798
......@@ -262,7 +262,7 @@ void Artist::createTable( sqlite::Connection* dbConnection )
sqlite::Tools::executeRequest( dbConnection, reqFts );
}
void Artist::createTriggers( sqlite::Connection* dbConnection )
void Artist::createTriggers( sqlite::Connection* dbConnection, uint32_t dbModelVersion )
{
static const std::string triggerReq = "CREATE TRIGGER IF NOT EXISTS has_album_present AFTER UPDATE OF "
"is_present ON " + policy::AlbumTable::Name +
......@@ -315,7 +315,17 @@ void Artist::createTriggers( sqlite::Connection* dbConnection )
" END";
sqlite::Tools::executeRequest( dbConnection, triggerReq );
sqlite::Tools::executeRequest( dbConnection, autoDeleteAlbumTriggerReq );
sqlite::Tools::executeRequest( dbConnection, autoDeleteTrackTriggerReq );
// Don't create this trigger if the database is about to be migrated.
// This could make earlier migration fail, and needs to be done when
// migrating to v7 to v8.
// While the has_album_remaining trigger now also references the nb_tracks
// field, it was present from before version 3, so it wouldn't be recreated.
// As we don't support any model before 3 (or rather we just recreate
// everything), we don't have to bother here.
if ( dbModelVersion >= 8 )
{
sqlite::Tools::executeRequest( dbConnection, autoDeleteTrackTriggerReq );
}
sqlite::Tools::executeRequest( dbConnection, ftsInsertTrigger );
sqlite::Tools::executeRequest( dbConnection, ftsDeleteTrigger );
}
......
......@@ -67,7 +67,7 @@ public:
virtual unsigned int nbTracks() const override;
static void createTable( sqlite::Connection* dbConnection );
static void createTriggers( sqlite::Connection* dbConnection );
static void createTriggers( sqlite::Connection* dbConnection, uint32_t dbModelVersion );
static bool createDefaultArtists( sqlite::Connection* dbConnection );
static std::shared_ptr<Artist> create( MediaLibraryPtr ml, const std::string& name );
static std::vector<ArtistPtr> search( MediaLibraryPtr ml, const std::string& name );
......
......@@ -160,8 +160,9 @@ void MediaLibrary::createAllTables()
void MediaLibrary::createAllTriggers()
{
auto dbModelVersion = m_settings.dbModelVersion();
Album::createTriggers( m_dbConnection.get() );
Artist::createTriggers( m_dbConnection.get() );
Artist::createTriggers( m_dbConnection.get(), dbModelVersion );
Media::createTriggers( m_dbConnection.get() );
Genre::createTriggers( m_dbConnection.get() );
Playlist::createTriggers( m_dbConnection.get() );
......@@ -278,14 +279,14 @@ InitializeResult MediaLibrary::initialize( const std::string& dbPath,
{
auto t = m_dbConnection->newTransaction();
createAllTables();
createAllTriggers();
t->commit();
if ( m_settings.load() == false )
{
LOG_ERROR( "Failed to load settings" );
return InitializeResult::Failed;
}
createAllTriggers();
t->commit();
if ( m_settings.dbModelVersion() != Settings::DbModelVersion )
{
res = updateDatabaseModel( m_settings.dbModelVersion(), dbPath );
......@@ -911,7 +912,7 @@ void MediaLibrary::migrateModel7to8()
for ( const auto& req : reqs )
sqlite::Tools::executeRequest( getConn(), req );
// Re-create triggers removed in the process
Artist::createTriggers( getConn() );
Artist::createTriggers( getConn(), 8u );
t->commit();
}
......
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