Commit 7d5bbd65 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

Playlist: Migrate to contiguous playlist position index

ref #67
parent d50a9fbe
Pipeline #4845 passed with stages
in 18 minutes and 45 seconds
......@@ -1293,6 +1293,7 @@ void MediaLibrary::migrateModel14to15()
* - Remove update_playlist_order trigger
* - Add update_playlist_order_on_delete
* - Update trigger update_playlist_order_on_insert
* - Enforce contiguous position indexes on PlaylistMediaRelation
*/
void MediaLibrary::migrateModel15to16()
{
......@@ -1304,7 +1305,7 @@ void MediaLibrary::migrateModel15to16()
for ( const auto& req : reqs )
sqlite::Tools::executeRequest( getConn(), req );
Media::createTriggers( dbConn, 15 );
m_settings.setDbModelVersion( 16 );
m_settings.save();
t->commit();
......
/**
* If migrating from an older version, these triggers might already be removed
* due to a migration on the playlist table, and updated Playlist_trigger file.
*/
"DROP TRIGGER IF EXISTS update_playlist_order",
"DROP TRIGGER IF EXISTS append_new_playlist_record",
/* This trigger however was always recreated until now, and must be present */
"DROP TRIGGER update_playlist_order_on_insert",
"DROP INDEX IF EXISTS playlist_media_pl_id_index",
/* Migrate to contiguous playlist position index */
"CREATE TEMPORARY TABLE PlaylistMediaRelation_backup"
"("
"media_id INTEGER,"
"mrl STRING,"
"playlist_id INTEGER,"
"position INTEGER"
")",
"INSERT INTO PlaylistMediaRelation_backup"
"(media_id, mrl, playlist_id, position) "
"SELECT media_id, mrl, playlist_id, ROW_NUMBER() OVER ("
"PARTITION BY playlist_id "
"ORDER BY position"
") - 1 "
"FROM PlaylistMediaRelation",
"DROP TABLE PlaylistMediaRelation",
#include "database/tables/Playlist_v16.sql"
"INSERT INTO PlaylistMediaRelation SELECT * FROM PlaylistMediaRelation_backup",
"DROP TABLE PlaylistMediaRelation_backup",
#include "database/tables/Playlist_triggers_v16.sql"
......@@ -86,4 +86,18 @@ CREATE TRIGGER append_new_playlist_record AFTER INSERT ON PlaylistMediaRelation
CREATE TRIGGER add_album_track AFTER INSERT ON AlbumTrack BEGIN UPDATE Album SET duration = duration + new.duration, nb_tracks = nb_tracks + 1, is_present = is_present + 1 WHERE id_album = new.album_id; END;
INSERT INTO `Device` (id_device,uuid,scheme,is_removable,is_present,last_seen) VALUES (1,NULL,NULL,NULL,NULL,NULL);
INSERT INTO `Folder` (id_folder,path,name,parent_id,is_banned,device_id,is_removable,nb_audio,nb_video) VALUES (1,'foo/','TestFolder',NULL,0,1,0,0,0);
INSERT INTO `Media` (id_media,type,subtype,duration,play_count,last_played_date,insertion_date,release_date,title,filename,is_favorite,is_present) VALUES (1,1,3,-1,NULL,NULL,1522231538,NULL,'Cool media','file%20with%20space.avi',0,1);
INSERT INTO `Media` (id_media,type,subtype,duration,play_count,last_played_date,insertion_date,release_date,title,filename,is_favorite,is_present) VALUES (2,1,3,-1,NULL,NULL,1522231538,NULL,'media2.avi','media2.avi',0,1);
INSERT INTO `Media` (id_media,type,subtype,duration,play_count,last_played_date,insertion_date,release_date,title,filename,is_favorite,is_present) VALUES (3,0,3,-1,NULL,NULL,1522231538,NULL,'external.avi','external.avi',0,1);
INSERT INTO `File` (id_file,media_id,playlist_id,mrl,type,last_modification_date,size,folder_id,is_removable,is_external,is_network) VALUES (1,1,NULL,'media.avi',1,NULL,NULL,1,0,0,0);
INSERT INTO `File` (id_file,media_id,playlist_id,mrl,type,last_modification_date,size,folder_id,is_removable,is_external,is_network) VALUES (2,2,NULL,'media2.avi',1,NULL,NULL,1,0,0,0);
INSERT INTO `File` (id_file,media_id,playlist_id,mrl,type,last_modification_date,size,folder_id,is_removable,is_external,is_network) VALUES (3,3,NULL,'external.avi',1,NULL,NULL,NULL,0,1, 0);
INSERT INTO `Playlist` (id_playlist, name, creation_date) VALUES(1, "contiguous", 123);
INSERT INTO `PlaylistMediaRelation` (media_id, playlist_id, position) VALUES(1, 1, 1);
INSERT INTO `PlaylistMediaRelation` (media_id, playlist_id, position) VALUES(1, 1, 3);
INSERT INTO `PlaylistMediaRelation` (media_id, playlist_id, position) VALUES(1, 1, 5);
INSERT INTO `Playlist` (id_playlist, name, creation_date) VALUES(2, "contiguous2", 123);
INSERT INTO `PlaylistMediaRelation` (media_id, playlist_id, position) VALUES(1, 2, 1);
INSERT INTO `PlaylistMediaRelation` (media_id, playlist_id, position) VALUES(2, 2, 3);
INSERT INTO `PlaylistMediaRelation` (media_id, playlist_id, position) VALUES(3, 2, 5);
COMMIT;
Supports Markdown
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