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

MediaLibrary: Bump the database model version in DB as part of each migration

Fix #69
parent f4a842c8
Pipeline #4827 passed with stage
in 5 minutes and 33 seconds
...@@ -920,9 +920,8 @@ InitializeResult MediaLibrary::updateDatabaseModel( unsigned int previousVersion ...@@ -920,9 +920,8 @@ InitializeResult MediaLibrary::updateDatabaseModel( unsigned int previousVersion
// Safety check: ensure we didn't forget a migration along the way // Safety check: ensure we didn't forget a migration along the way
assert( previousVersion == Settings::DbModelVersion ); assert( previousVersion == Settings::DbModelVersion );
m_settings.setDbModelVersion( Settings::DbModelVersion ); assert( previousVersion == m_settings.dbModelVersion() );
if ( m_settings.save() == false )
return InitializeResult::Failed;
return InitializeResult::Success; return InitializeResult::Success;
} }
catch( const std::exception& ex ) catch( const std::exception& ex )
...@@ -987,17 +986,23 @@ void MediaLibrary::migrateModel3to5() ...@@ -987,17 +986,23 @@ void MediaLibrary::migrateModel3to5()
// Re-create triggers removed in the process // Re-create triggers removed in the process
Media::createTriggers( getConn(), 5 ); Media::createTriggers( getConn(), 5 );
Playlist::createTriggers( getConn() ); Playlist::createTriggers( getConn() );
m_settings.setDbModelVersion( 5 );
m_settings.save();
t->commit(); t->commit();
} }
void MediaLibrary::migrateModel5to6() void MediaLibrary::migrateModel5to6()
{ {
// We can't create a transaction here since it would make the weak context
// creation fail.
std::string req = "DELETE FROM " + Media::Table::Name + " WHERE type = ?"; std::string req = "DELETE FROM " + Media::Table::Name + " WHERE type = ?";
sqlite::Tools::executeRequest( getConn(), req, Media::Type::Unknown ); sqlite::Tools::executeRequest( getConn(), req, Media::Type::Unknown );
sqlite::Connection::WeakDbContext weakConnCtx{ getConn() }; sqlite::Connection::WeakDbContext weakConnCtx{ getConn() };
req = "UPDATE " + Media::Table::Name + " SET is_present = 1 WHERE is_present != 0"; req = "UPDATE " + Media::Table::Name + " SET is_present = 1 WHERE is_present != 0";
sqlite::Tools::executeRequest( getConn(), req ); sqlite::Tools::executeRequest( getConn(), req );
m_settings.setDbModelVersion( 6 );
m_settings.save();
} }
void MediaLibrary::migrateModel7to8() void MediaLibrary::migrateModel7to8()
...@@ -1014,6 +1019,8 @@ void MediaLibrary::migrateModel7to8() ...@@ -1014,6 +1019,8 @@ void MediaLibrary::migrateModel7to8()
Artist::createTriggers( getConn(), 8u ); Artist::createTriggers( getConn(), 8u );
Media::createTriggers( getConn(), 5 ); Media::createTriggers( getConn(), 5 );
File::createTriggers( getConn() ); File::createTriggers( getConn() );
m_settings.setDbModelVersion( 8 );
m_settings.save();
t->commit(); t->commit();
} }
...@@ -1023,6 +1030,7 @@ void MediaLibrary::migrateModel8to9() ...@@ -1023,6 +1030,7 @@ void MediaLibrary::migrateModel8to9()
// first application run (after the migration). // first application run (after the migration).
// This could have caused media associated to deleted files not to be // This could have caused media associated to deleted files not to be
// deleted as well, so let's do that now. // deleted as well, so let's do that now.
auto t = getConn()->newTransaction();
const std::string req = "DELETE FROM " + Media::Table::Name + " " const std::string req = "DELETE FROM " + Media::Table::Name + " "
"WHERE id_media IN " "WHERE id_media IN "
"(SELECT id_media FROM " + Media::Table::Name + " m LEFT JOIN " + "(SELECT id_media FROM " + Media::Table::Name + " m LEFT JOIN " +
...@@ -1032,6 +1040,9 @@ void MediaLibrary::migrateModel8to9() ...@@ -1032,6 +1040,9 @@ void MediaLibrary::migrateModel8to9()
// Don't check for the return value, we don't mind if nothing deleted. // Don't check for the return value, we don't mind if nothing deleted.
// Quite the opposite actually :) // Quite the opposite actually :)
sqlite::Tools::executeDelete( getConn(), req ); sqlite::Tools::executeDelete( getConn(), req );
m_settings.setDbModelVersion( 9 );
m_settings.save();
t->commit();
} }
void MediaLibrary::migrateModel9to10() void MediaLibrary::migrateModel9to10()
...@@ -1048,6 +1059,8 @@ void MediaLibrary::migrateModel9to10() ...@@ -1048,6 +1059,8 @@ void MediaLibrary::migrateModel9to10()
LOG_INFO( "Converting ", f->rawMrl(), " to ", newMrl ); LOG_INFO( "Converting ", f->rawMrl(), " to ", newMrl );
f->setMrl( newMrl ); f->setMrl( newMrl );
} }
m_settings.setDbModelVersion( 10 );
m_settings.save();
t->commit(); t->commit();
} }
...@@ -1073,6 +1086,8 @@ void MediaLibrary::migrateModel10to11() ...@@ -1073,6 +1086,8 @@ void MediaLibrary::migrateModel10to11()
auto newMrl = utils::url::encode( utils::url::decode( f->rawMrl() ) ); auto newMrl = utils::url::encode( utils::url::decode( f->rawMrl() ) );
f->setMrl( std::move( newMrl ) ); f->setMrl( std::move( newMrl ) );
} }
m_settings.setDbModelVersion( 11 );
m_settings.save();
t->commit(); t->commit();
} }
...@@ -1109,6 +1124,8 @@ void MediaLibrary::migrateModel12to13() ...@@ -1109,6 +1124,8 @@ void MediaLibrary::migrateModel12to13()
" SET is_present = (SELECT is_present FROM " + Media::Table::Name + " SET is_present = (SELECT is_present FROM " + Media::Table::Name +
" WHERE id_media = media_id)"; " WHERE id_media = media_id)";
sqlite::Tools::executeUpdate( getConn(), migrateData ); sqlite::Tools::executeUpdate( getConn(), migrateData );
m_settings.setDbModelVersion( 13 );
m_settings.save();
t->commit(); t->commit();
} }
...@@ -1238,6 +1255,8 @@ void MediaLibrary::migrateModel13to14( uint32_t originalPreviousVersion ) ...@@ -1238,6 +1255,8 @@ void MediaLibrary::migrateModel13to14( uint32_t originalPreviousVersion )
{ {
f->setName( utils::file::directoryName( f->rawMrl() ) ); f->setName( utils::file::directoryName( f->rawMrl() ) );
} }
m_settings.setDbModelVersion( 14 );
m_settings.save();
t->commit(); t->commit();
} }
...@@ -1259,6 +1278,8 @@ void MediaLibrary::migrateModel14to15() ...@@ -1259,6 +1278,8 @@ void MediaLibrary::migrateModel14to15()
for ( const auto& req : reqs ) for ( const auto& req : reqs )
sqlite::Tools::executeRequest( dbConn, req ); sqlite::Tools::executeRequest( dbConn, req );
Folder::createTriggers( dbConn, 15 ); Folder::createTriggers( dbConn, 15 );
m_settings.setDbModelVersion( 15 );
m_settings.save();
t->commit(); 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