Commit 23a677f2 authored by Alexandre Fernandez's avatar Alexandre Fernandez Committed by Hugo Beauzée-Luyssen

MediaLibrary: Add migration from database model 3 to 4

Signed-off-by: default avatarHugo Beauzée-Luyssen <hugo@beauzee.fr>
parent b665e5ee
......@@ -333,4 +333,5 @@ endif
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = medialibrary.pc
EXTRA_DIST = medialibrary.pc
EXTRA_DIST = medialibrary.pc \
src/database/migrations/migration3-4.sql
......@@ -737,6 +737,12 @@ bool MediaLibrary::updateDatabaseModel( unsigned int previousVersion )
return false;
previousVersion = 3;
}
if ( previousVersion == 3 )
{
if ( migrateModel3to4() == false )
return false;
previousVersion = 4;
}
// To be continued in the future!
// Safety check: ensure we didn't forget a migration along the way
......@@ -746,6 +752,34 @@ bool MediaLibrary::updateDatabaseModel( unsigned int previousVersion )
return true;
}
bool MediaLibrary::migrateModel3to4()
{
/*
* Get a special SqliteConnection with Foreign Keys deactivated
* to avoid cascade deletion while remodeling the database into
* the transaction.
*/
SqliteConnection conn( getConn()->getDBPath(), false );
auto t = conn.newTransaction();
using namespace policy;
// 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"
};
for ( const auto& req : reqs )
{
if ( sqlite::Tools::executeRequest( &conn, req ) == false )
return false;
}
// Re-create triggers removed in the process
Media::createTriggers( &conn );
Playlist::createTriggers( &conn );
t->commit();
return true;
}
void MediaLibrary::reload()
{
if ( m_discovererWorker != nullptr )
......
......@@ -156,6 +156,7 @@ class MediaLibrary : public IMediaLibrary, public IDeviceListerCb
virtual void startDiscoverer();
virtual void startDeletionNotifier();
bool updateDatabaseModel( unsigned int previousVersion );
bool migrateModel3to4();
bool createAllTables();
void registerEntityHooks();
static bool validateSearchPattern( const std::string& pattern );
......
"CREATE TEMPORARY TABLE " + FileTable::Name + "_backup("
"id_file INTEGER PRIMARY KEY AUTOINCREMENT,"
"media_id INT NOT NULL,"
"mrl TEXT,"
"type UNSIGNED INTEGER,"
"last_modification_date UNSIGNED INT,"
"size UNSIGNED INT,"
"parser_step INTEGER NOT NULL DEFAULT 0,"
"parser_retries INTEGER NOT NULL DEFAULT 0,"
"folder_id UNSIGNED INTEGER,"
"is_present BOOLEAN NOT NULL DEFAULT 1,"
"is_removable BOOLEAN NOT NULL,"
"is_external BOOLEAN NOT NULL,"
"FOREIGN KEY (media_id) REFERENCES " + MediaTable::Name + "(id_media) ON DELETE CASCADE,"
"FOREIGN KEY (folder_id) REFERENCES " + FolderTable::Name + "(id_folder) ON DELETE CASCADE,"
"UNIQUE( mrl, folder_id ) ON CONFLICT FAIL);",
"INSERT INTO " + FileTable::Name + "_backup SELECT * FROM " + FileTable::Name + ";",
"DROP TABLE " + FileTable::Name + ";",
"CREATE TABLE " + FileTable::Name + "(id_file INTEGER PRIMARY KEY AUTOINCREMENT,"
"media_id UNSIGNED INT DEFAULT NULL,"
"playlist_id UNSIGNED INT DEFAULT NULL," // Added
"mrl TEXT,"
"type UNSIGNED INTEGER,"
"last_modification_date UNSIGNED INT,"
"size UNSIGNED INT,"
"parser_step INTEGER NOT NULL DEFAULT 0,"
"parser_retries INTEGER NOT NULL DEFAULT 0,"
"folder_id UNSIGNED INTEGER,"
"is_present BOOLEAN NOT NULL DEFAULT 1,"
"is_removable BOOLEAN NOT NULL,"
"is_external BOOLEAN NOT NULL,"
"FOREIGN KEY (media_id) REFERENCES " + MediaTable::Name + "(id_media) ON DELETE CASCADE,"
"FOREIGN KEY (playlist_id) REFERENCES " + PlaylistTable::Name + "(id_playlist) ON DELETE CASCADE," // Added
"FOREIGN KEY (folder_id) REFERENCES " + FolderTable::Name + "(id_folder) ON DELETE CASCADE,"
"UNIQUE( mrl, folder_id ) ON CONFLICT FAIL);",
"INSERT INTO " + FileTable::Name + "(id_file,media_id,mrl,type,last_modification_date,size,parser_step,parser_retries,folder_id,is_present,is_removable,is_external)"
" SELECT * FROM " + FileTable::Name + "_backup;",
"DROP TABLE " + FileTable::Name + "_backup;",
"CREATE TEMPORARY TABLE " + PlaylistTable::Name + "_backup("
"id_playlist INTEGER PRIMARY KEY AUTOINCREMENT,"
"name TEXT UNIQUE,"
"creation_date UNSIGNED INT NOT NULL);",
"INSERT INTO " + PlaylistTable::Name + "_backup SELECT * FROM Playlist;",
"DROP TABLE " + PlaylistTable::Name + ";",
"CREATE TABLE " + PlaylistTable::Name + "("
"id_playlist INTEGER PRIMARY KEY AUTOINCREMENT,"
"name TEXT UNIQUE,"
"file_id UNSIGNED INT DEFAULT NULL," // Added
"creation_date UNSIGNED INT NOT NULL,"
"artwork_mrl TEXT," // Added
"FOREIGN KEY (file_id) REFERENCES " + FileTable::Name + "(id_file) ON DELETE CASCADE);", // Added
"INSERT INTO " + PlaylistTable::Name + "(id_playlist,name,creation_date)"
" SELECT * FROM " + PlaylistTable::Name + "_backup;",
"DROP TABLE " + PlaylistTable::Name + "_backup;",
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