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

Bump DB model to 5

parent bcef79fd
......@@ -340,4 +340,4 @@ endif
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = medialibrary.pc
EXTRA_DIST += medialibrary.pc \
src/database/migrations/migration3-4.sql
src/database/migrations/migration3-5.sql
......@@ -746,18 +746,29 @@ bool MediaLibrary::updateDatabaseModel( unsigned int previousVersion,
// Up until model 3, it's safer (and potentially more efficient with index changes) to drop the DB
// It's also way simpler to implement
// In case of downgrade, just recreate the database
// We might also have some special cases for failed upgrade (see
// comments below for per-version details)
if ( previousVersion < 3 ||
previousVersion > Settings::DbModelVersion )
previousVersion > Settings::DbModelVersion ||
previousVersion == 4 )
{
if( recreateDatabase( dbPath ) == false )
throw std::runtime_error( "Failed to recreate the database" );
return true;
}
/**
* Migration from 3 to 4 didn't happen so well and broke a few
* users DB. So:
* - Any v4 database will be dropped and recreated in v5
* - Any v3 database will be upgraded to v5
* V4 database is only used by VLC-android 2.5.6 // 2.5.8, which are
* beta versions.
*/
if ( previousVersion == 3 )
{
if ( migrateModel3to4() == false )
throw std::logic_error( "Failed to migrate from 3 to 4" );
previousVersion = 4;
if ( migrateModel3to5() == false )
throw std::logic_error( "Failed to migrate from 3 to 5" );
previousVersion = 5;
}
// To be continued in the future!
......@@ -814,7 +825,7 @@ bool MediaLibrary::recreateDatabase( const std::string& dbPath )
return true;
}
bool MediaLibrary::migrateModel3to4()
bool MediaLibrary::migrateModel3to5()
{
/*
* Disable Foreign Keys & recursive triggers to avoid cascading deletion
......@@ -826,7 +837,7 @@ bool MediaLibrary::migrateModel3to4()
// As SQLite do not allow us to remove or add some constraints,
// we use the method described here https://www.sqlite.org/faq.html#q11
std::string reqs[] = {
# include "database/migrations/migration3-4.sql"
# include "database/migrations/migration3-5.sql"
};
for ( const auto& req : reqs )
......
......@@ -160,7 +160,7 @@ class MediaLibrary : public IMediaLibrary, public IDeviceListerCb
bool recreateDatabase( const std::string& dbPath );
bool updateDatabaseModel( unsigned int previousVersion,
const std::string& path );
bool migrateModel3to4();
bool migrateModel3to5();
bool createAllTables();
void registerEntityHooks();
static bool validateSearchPattern( const std::string& pattern );
......
......@@ -32,7 +32,7 @@
namespace medialibrary
{
const uint32_t Settings::DbModelVersion = 4u;
const uint32_t Settings::DbModelVersion = 5u;
Settings::Settings( MediaLibrary* ml )
: m_ml( ml )
......
......@@ -108,7 +108,7 @@ public:
}
};
TEST_F( DbModel, Upgrade )
TEST_F( DbModel, Upgrade3to5 )
{
LoadFakeDB( SRC_DIR "/test/unittest/db_v3.sql" );
auto res = ml->initialize( "test.db", "/tmp", cbMock.get() );
......@@ -116,3 +116,17 @@ TEST_F( DbModel, Upgrade )
// All is done during the database initialization, we only care about no
// exception being thrown, and MediaLibrary::initialize() returning true
}
TEST_F( DbModel, Upgrade4to5 )
{
LoadFakeDB( SRC_DIR "/test/unittest/db_v4.sql" );
auto res = ml->initialize( "test.db", "/tmp", cbMock.get() );
ASSERT_TRUE( res );
// The culprit with V4 was an invalid migration, leading to missing fields
// in File and most likely Playlist tables. Simply try to create/fetch a file
auto m = ml->addFile( "test.mkv" );
ASSERT_NE( m, nullptr );
auto files = ml->files();
ASSERT_NE( files.size(), 0u );
}
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