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

Task: Add a is_refresh field

parent 5b4b3e91
......@@ -215,6 +215,11 @@ public:
* is returned, this item doesn't belong in any playlist.
*/
virtual unsigned int parentPlaylistIndex() const = 0;
/**
* @brief isRefresh Returns true if this item is a refresh of an existing media
*/
virtual bool isRefresh() const = 0;
};
}
......
......@@ -58,6 +58,7 @@
#include "utils/Url.h"
#include "VideoTrack.h"
#include "Metadata.h"
#include "parser/Task.h"
#include "utils/Charsets.h"
// Discoverers:
......@@ -511,7 +512,8 @@ void MediaLibrary::addDiscoveredFile( std::shared_ptr<fs::IFile> fileFs,
}
// Don't move the file as we might need it for error handling
task = parser::Task::create( this, fileFs, std::move( parentFolder ),
std::move( parentFolderFs ), std::move( parentPlaylist ) );
std::move( parentFolderFs ),
std::move( parentPlaylist ) );
if ( task != nullptr && m_parser != nullptr )
m_parser->parse( task );
}
......@@ -839,6 +841,7 @@ InitializeResult MediaLibrary::updateDatabaseModel( unsigned int previousVersion
const std::string& dbPath )
{
LOG_INFO( "Updating database model from ", previousVersion, " to ", Settings::DbModelVersion );
auto originalPreviousVersion = 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
......@@ -935,7 +938,7 @@ InitializeResult MediaLibrary::updateDatabaseModel( unsigned int previousVersion
// We need to recreate many thumbnail records, and hopefully
// generate better ones
needRescan = true;
migrateModel13to14();
migrateModel13to14( originalPreviousVersion );
previousVersion = 14;
}
// To be continued in the future!
......@@ -1144,7 +1147,7 @@ void MediaLibrary::migrateModel12to13()
* - Add Media.thumbnail_id
* - Add Media.thumbnail_generated
*/
void MediaLibrary::migrateModel13to14()
void MediaLibrary::migrateModel13to14( uint32_t originalPreviousVersion )
{
auto dbConn = getConn();
sqlite::Connection::WeakDbContext weakConnCtx{ dbConn };
......@@ -1156,6 +1159,39 @@ void MediaLibrary::migrateModel13to14()
for ( const auto& req : reqs )
sqlite::Tools::executeRequest( dbConn, req );
// Task table was introduced in model 8, so if the user is migrating from a
// version earlier than this one, the Task table will be properly created and
// doesn't need a migration
if ( originalPreviousVersion >= 8 )
{
const std::string migrateTaskReqs[] = {
"CREATE TEMPORARY TABLE " + parser::Task::Table::Name + "_backup"
"("
"id_task INTEGER PRIMARY KEY AUTOINCREMENT,"
"step INTEGER NOT NULL DEFAULT 0,"
"retry_count INTEGER NOT NULL DEFAULT 0,"
"mrl TEXT,"
"file_id UNSIGNED INTEGER,"
"parent_folder_id UNSIGNED INTEGER,"
"parent_playlist_id INTEGER,"
"parent_playlist_index UNSIGNED INTEGER"
")",
"INSERT INTO " + parser::Task::Table::Name + "_backup SELECT * FROM " + parser::Task::Table::Name,
"DROP TABLE " + parser::Task::Table::Name,
#include "database/tables/Task_v14.sql"
"INSERT INTO " + parser::Task::Table::Name + " SELECT "
"id_task, step, retry_count, mrl, file_id, parent_folder_id, parent_playlist_id,"
"parent_playlist_index, 0 FROM " + parser::Task::Table::Name + "_backup",
"DROP TABLE " + parser::Task::Table::Name + "_backup",
};
for ( const auto& req : migrateTaskReqs )
sqlite::Tools::executeRequest( dbConn, req );
}
// Re-create tables that we just removed
// We will run a re-scan, so we don't care about keeping their content
Album::createTable( dbConn );
......
......@@ -198,7 +198,7 @@ class MediaLibrary : public IMediaLibrary, public IDeviceListerCb
void migrateModel9to10();
void migrateModel10to11();
void migrateModel12to13();
void migrateModel13to14();
void migrateModel13to14( uint32_t originalPreviousVersion );
void createAllTables();
void createAllTriggers();
void registerEntityHooks();
......
"CREATE TABLE IF NOT EXISTS " + Task::Table::Name +
"CREATE TABLE IF NOT EXISTS " + parser::Task::Table::Name +
"("
"id_task INTEGER PRIMARY KEY AUTOINCREMENT,"
"step INTEGER NOT NULL DEFAULT 0,"
......@@ -8,7 +8,8 @@
"parent_folder_id UNSIGNED INTEGER,"
"parent_playlist_id INTEGER,"
"parent_playlist_index UNSIGNED INTEGER,"
"UNIQUE(mrl, parent_playlist_id) ON CONFLICT FAIL,"
"is_refresh BOOLEAN NOT NULL DEFAULT 0,"
"UNIQUE(mrl, parent_playlist_id, is_refresh) ON CONFLICT FAIL,"
"FOREIGN KEY (parent_folder_id) REFERENCES " + Folder::Table::Name
+ "(id_folder) ON DELETE CASCADE,"
"FOREIGN KEY (file_id) REFERENCES " + File::Table::Name
......
......@@ -55,6 +55,7 @@ Task::Task( MediaLibraryPtr ml, sqlite::Row& row )
{
std::string mrl;
unsigned int parentPlaylistIndex;
bool isRefresh;
row >> m_id
>> m_step
>> m_retryCount
......@@ -62,8 +63,9 @@ Task::Task( MediaLibraryPtr ml, sqlite::Row& row )
>> m_fileId
>> m_parentFolderId
>> m_parentPlaylistId
>> parentPlaylistIndex;
m_item = Item{ this, std::move( mrl ), parentPlaylistIndex };
>> parentPlaylistIndex
>> isRefresh;
m_item = Item{ this, std::move( mrl ), parentPlaylistIndex, isRefresh };
}
Task::Task( MediaLibraryPtr ml, std::shared_ptr<fs::IFile> fileFs,
......@@ -76,7 +78,8 @@ Task::Task( MediaLibraryPtr ml, std::shared_ptr<fs::IFile> fileFs,
, m_step( Step::None )
, m_fileId( 0 )
, m_item( this, std::move( fileFs ), std::move( parentFolder ),
std::move( parentFolderFs ), std::move( parentPlaylist ), parentPlaylistIndex )
std::move( parentFolderFs ), std::move( parentPlaylist ),
parentPlaylistIndex )
{
}
......@@ -148,18 +151,20 @@ Task::Item& Task::item()
return m_item;
}
Task::Item::Item( ITaskCb* taskCb, std::string mrl, unsigned int subitemPosition )
Task::Item::Item( ITaskCb* taskCb, std::string mrl, unsigned int subitemPosition,
bool isRefresh )
: m_taskCb( taskCb )
, m_mrl( std::move( mrl ) )
, m_duration( 0 )
, m_parentPlaylistIndex( subitemPosition )
, m_isRefresh( isRefresh )
{
}
Task::Item::Item( ITaskCb* taskCb, std::shared_ptr<fs::IFile> fileFs,
std::shared_ptr<Folder> parentFolder,
std::shared_ptr<fs::IDirectory> parentFolderFs,
std::shared_ptr<Playlist> parentPlaylist, unsigned int parentPlaylistIndex )
std::shared_ptr<Playlist> parentPlaylist, unsigned int parentPlaylistIndex )
: m_taskCb( taskCb )
, m_mrl( fileFs->mrl() )
, m_duration( 0 )
......@@ -168,6 +173,7 @@ Task::Item::Item( ITaskCb* taskCb, std::shared_ptr<fs::IFile> fileFs,
, m_parentFolderFs( std::move( parentFolderFs ) )
, m_parentPlaylist( std::move( parentPlaylist ) )
, m_parentPlaylistIndex( parentPlaylistIndex )
, m_isRefresh( false )
{
}
......@@ -206,7 +212,7 @@ const IItem& Task::Item::subItem( unsigned int index ) const
IItem& Task::Item::createSubItem( std::string mrl, unsigned int playlistIndex )
{
m_subItems.emplace_back( nullptr, std::move( mrl ), playlistIndex );
m_subItems.emplace_back( nullptr, std::move( mrl ), playlistIndex, false );
return m_subItems.back();
}
......@@ -277,6 +283,11 @@ unsigned int Task::Item::parentPlaylistIndex() const
return m_parentPlaylistIndex;
}
bool Task::Item::isRefresh() const
{
return m_isRefresh;
}
bool Task::restoreLinkedEntities()
{
LOG_INFO("Restoring linked entities of task ", m_id);
......@@ -461,11 +472,11 @@ Task::create( MediaLibraryPtr ml, std::shared_ptr<fs::IFile> fileFs,
std::move( parentFolder ), std::move( parentFolderFs ),
std::move( parentPlaylist.first ), parentPlaylist.second );
const std::string req = "INSERT INTO " + Task::Table::Name +
"(mrl, parent_folder_id, parent_playlist_id, parent_playlist_index) "
"VALUES(?, ?, ?, ?)";
"(mrl, parent_folder_id, parent_playlist_id, parent_playlist_index, is_refresh) "
"VALUES(?, ?, ?, ?, ?)";
if ( insert( ml, self, req, self->m_item.mrl(), parentFolderId,
sqlite::ForeignKey( parentPlaylistId ),
parentPlaylistIndex ) == false )
parentPlaylistIndex, false ) == false )
return nullptr;
return self;
}
......
......@@ -90,7 +90,7 @@ public:
*
* The position is used to keep subitems ordering for playlists
*/
Item( ITaskCb* taskCb, std::string mrl, unsigned int subitemIndex );
Item( ITaskCb* taskCb, std::string mrl, unsigned int subitemIndex, bool isRefresh );
Item( ITaskCb* taskCb, std::shared_ptr<fs::IFile> fileFs,
std::shared_ptr<Folder> folder, std::shared_ptr<fs::IDirectory> folderFs,
std::shared_ptr<Playlist> parentPlaylist, unsigned int parentPlaylistIndex );
......@@ -128,6 +128,8 @@ public:
virtual unsigned int parentPlaylistIndex() const override;
virtual bool isRefresh() const override;
private:
ITaskCb* m_taskCb;
......@@ -143,6 +145,7 @@ public:
std::shared_ptr<fs::IDirectory> m_parentFolderFs;
PlaylistPtr m_parentPlaylist;
unsigned int m_parentPlaylistIndex;
bool m_isRefresh;
};
static_assert( std::is_move_assignable<Item>::value, "Item must be move assignable" );
......@@ -199,7 +202,8 @@ public:
static std::shared_ptr<Task> create( MediaLibraryPtr ml, std::shared_ptr<fs::IFile> fileFs,
std::shared_ptr<Folder> parentFolder,
std::shared_ptr<fs::IDirectory> parentFolderFs,
std::pair<std::shared_ptr<Playlist>, unsigned int> parentPlaylist );
std::pair<std::shared_ptr<Playlist>,
unsigned int> parentPlaylist );
static void recoverUnscannedFiles( MediaLibraryPtr ml );
private:
......
......@@ -86,4 +86,5 @@ INSERT INTO `Playlist` (id_playlist,name,file_id,creation_date,artwork_mrl) VALU
INSERT INTO `PlaylistMediaRelation` (media_id,playlist_id,position) VALUES (1,1,1);
INSERT INTO `PlaylistMediaRelation` (media_id,playlist_id,position) VALUES (2,1,2);
INSERT INTO `PlaylistMediaRelation` (media_id,playlist_id,position) VALUES (3,1,3);
INSERT INTO `Task` (mrl, file_id, parent_folder_id, parent_playlist_id) VALUES("other_external.avi", 99, NULL, NULL);
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