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

migration: Handle playlist table changes

Fix #36
parent 2a90a4bb
......@@ -1165,6 +1165,8 @@ void MediaLibrary::migrateModel13to14()
Album::createTriggers( getConn() );
Artist::createTriggers( getConn(), 14 );
Show::createTriggers( getConn() );
Playlist::createTriggers( getConn() );
t->commit();
}
......
......@@ -297,6 +297,30 @@ void Playlist::clearExternalPlaylistContent(MediaLibraryPtr ml)
sqlite::Tools::executeDelete( ml->getConn(), req );
}
void Playlist::insertMrlFromMediaId( MediaLibraryPtr ml )
{
sqlite::Statement stmt{ ml->getConn()->handle(),
"SELECT * FROM PlaylistMediaRelation WHERE mrl IS NULL GROUP BY media_id" };
stmt.execute();
sqlite::Row row;
const std::string updateReq = "UPDATE PlaylistMediaRelation SET mrl = ? WHERE media_id = ?";
while ( ( row = stmt.row() ) != nullptr )
{
int64_t mediaId;
row >> mediaId;
auto media = ml->media( mediaId );
if ( media == nullptr )
continue;
auto files = media->files();
assert( files.size() > 0 );
auto mainFile = std::find_if( begin( files ), end( files ), []( const FilePtr& f) {
return f->type() == IFile::Type::Main;
});
sqlite::Tools::executeUpdate( ml->getConn(), updateReq, (*mainFile)->mrl(), mediaId );
}
sqlite::Tools::executeDelete( ml->getConn(), "DELETE FROM PlaylistMediaRelation WHERE mrl IS NULL" );
}
std::string Playlist::sortRequest( const QueryParameters* params )
{
std::string req = " ORDER BY ";
......
......@@ -82,6 +82,11 @@ public:
*/
static void clearExternalPlaylistContent( MediaLibraryPtr ml );
/**
* Fetches all PlaylistMediaRelation items without an MRL and assign it
*/
static void insertMrlFromMediaId( MediaLibraryPtr ml );
private:
static std::string sortRequest( const QueryParameters* params );
void curateNullMediaID() const;
......
......@@ -81,6 +81,35 @@
", type, value "
"FROM " + MetadataTable::Name + "_backup",
/******************* Migrate the playlist table *******************************/
"CREATE TEMPORARY TABLE " + PlaylistTable::Name + "_backup"
"("
+ PlaylistTable::PrimaryKeyColumn + " INTEGER PRIMARY KEY AUTOINCREMENT,"
"name TEXT,"
"file_id UNSIGNED INT DEFAULT NULL,"
"creation_date UNSIGNED INT NOT NULL,"
"artwork_mrl TEXT"
")",
"CREATE TEMPORARY TABLE PlaylistMediaRelation_backup"
"("
"media_id INTEGER,"
"playlist_id INTEGER,"
"position INTEGER"
")",
"INSERT INTO " + PlaylistTable::Name + "_backup SELECT * FROM " + PlaylistTable::Name,
"INSERT INTO PlaylistMediaRelation_backup SELECT * FROM PlaylistMediaRelation",
"DROP TABLE " + PlaylistTable::Name,
"DROP TABLE PlaylistMediaRelation",
#include "database/tables/Playlist_v14.sql"
"INSERT INTO " + PlaylistTable::Name + " SELECT * FROM " + PlaylistTable::Name + "_backup",
"INSERT INTO PlaylistMediaRelation SELECT media_id, NULL, playlist_id, position "
"FROM PlaylistMediaRelation_backup",
/******************* Delete other tables **************************************/
"DROP TABLE " + AlbumTable::Name,
......
......@@ -11,7 +11,7 @@
"CREATE TABLE IF NOT EXISTS PlaylistMediaRelation"
"("
"media_id INTEGER,"
"mrl STRING NOT NULL,"
"mrl STRING,"
"playlist_id INTEGER,"
"position INTEGER,"
"FOREIGN KEY(media_id) REFERENCES " + policy::MediaTable::Name + "("
......
......@@ -33,6 +33,7 @@
#include "Artist.h"
#include "Media.h"
#include "Metadata.h"
#include "Playlist.h"
class Misc : public Tests
{
......@@ -196,5 +197,12 @@ TEST_F( DbModel, Upgrade13to14 )
auto& meta = m->metadata( IMedia::MetadataType::Progress );
ASSERT_EQ( "fake progress", meta.str() );
auto playlists = ml->playlists( nullptr )->all();
ASSERT_EQ( 1u, playlists.size() );
auto playlistMedia = playlists[0]->media()->all();
ASSERT_EQ( 2u, playlistMedia.size() );
ASSERT_EQ( media[0]->id(), playlistMedia[0]->id() );
ASSERT_EQ( media[1]->id(), playlistMedia[1]->id() );
CheckNbTriggers( 32 );
}
......@@ -78,4 +78,7 @@ INSERT INTO `File` (id_file,media_id,playlist_id,mrl,type,last_modification_date
INSERT INTO `Media` (id_media,type,subtype,duration,play_count,last_played_date,insertion_date,release_date,thumbnail,title,filename,is_favorite,is_present) VALUES (2,3,NULL,-1,NULL,NULL,1522231538,NULL,'','media2.avi','media2.avi',0,1);
INSERT INTO `File` (id_file,media_id,playlist_id,mrl,type,last_modification_date,size,folder_id,is_present,is_removable,is_external) VALUES (2,2,NULL,'media2.avi',1,NULL,NULL,NULL,1,0,1);
INSERT INTO `MediaMetadata` VALUES(2, 50, "fake progress");
INSERT INTO `Playlist` (id_playlist,name,file_id,creation_date,artwork_mrl) VALUES (1,'test playlist',NULL,1530864508,'');
INSERT INTO `PlaylistMediaRelation` (media_id,playlist_id,position) VALUES (1,1,1);
INSERT INTO `PlaylistMediaRelation` (media_id,playlist_id,position) VALUES (2,1,2);
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