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

migration: 25 -> 26: Fixup badly encoded MRLs

Fix #250
parent 548898dd
Pipeline #17254 failed with stage
in 3 minutes and 47 seconds
......@@ -128,13 +128,18 @@ void File::setMrl( std::string mrl )
{
if ( m_mrl == mrl )
return;
const static std::string req = "UPDATE " + File::Table::Name + " SET "
"mrl = ? WHERE id_file = ?";
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, mrl, m_id ) == false )
if ( setMrl( m_ml, mrl, m_id ) == false )
return;
m_mrl = std::move( mrl );
}
bool File::setMrl( MediaLibraryPtr ml, const std::string& mrl, int64_t fileId )
{
const static std::string req = "UPDATE " + File::Table::Name + " SET "
"mrl = ? WHERE id_file = ?";
return sqlite::Tools::executeUpdate( ml->getConn(), req, mrl, fileId );
}
IFile::Type File::type() const
{
return m_type;
......
......@@ -58,6 +58,7 @@ public:
*/
const std::string& rawMrl() const;
void setMrl( std::string mrl );
static bool setMrl(MediaLibraryPtr ml, const std::string &mrl, int64_t fileId );
virtual Type type() const override;
virtual unsigned int lastModificationDate() const override;
virtual int64_t size() const override;
......
......@@ -1951,6 +1951,42 @@ void MediaLibrary::migrateModel25to26()
for ( const auto& req : reqs )
sqlite::Tools::executeRequest( dbConn, req );
{
sqlite::Row row;
int64_t fileId;
std::string mrl;
auto batchSize = 100u;
auto offset = 0u;
const std::string req = "SELECT id_file, mrl FROM " +
File::Table::Name + " WHERE is_external = 1 OR is_network = 1 "
"AND ( mrl LIKE '%#%40%' ESCAPE '#' OR mrl LIKE '%#%3A%' ESCAPE '#')"
"LIMIT ? OFFSET ?";
while ( true )
{
/*
* Since we change the parameter along the loop, we need to recreate
* the statement for each iteration, in order to reset the bindings
*/
sqlite::Statement stmt{ dbConn->handle(), req };
stmt.execute( batchSize, offset );
auto nbRow = 0u;
while ( ( row = stmt.row() ) != nullptr )
{
row >> fileId >> mrl;
auto newMrl = utils::url::encode( utils::url::decode( mrl ) );
if ( mrl != newMrl )
File::setMrl( this, newMrl, fileId );
nbRow++;
}
if ( nbRow < batchSize )
break;
offset += nbRow;
}
}
m_settings.setDbModelVersion( 26 );
t->commit();
}
......
......@@ -41,6 +41,7 @@
#include "Show.h"
#include "ShowEpisode.h"
#include "MediaGroup.h"
#include "File.h"
namespace
{
......@@ -682,4 +683,8 @@ TEST_F( DbModel, Upgrade25to26 )
ASSERT_EQ( 0u, mg->nbUnknown() );
ASSERT_EQ( 1u, mg->nbMedia() );
ASSERT_EQ( 2u, mg->nbTotalMedia() );
auto encodedFile = File::fetch( ml.get(), 6 );
ASSERT_NE( nullptr, encodedFile );
ASSERT_EQ( "udp://@224.10.50.36:5004", encodedFile->mrl() );
}
......@@ -132,11 +132,13 @@ INSERT INTO Media VALUES(2,2,3,10057,NULL,NULL,NULL,1585305577,2011,'Track 2','t
INSERT INTO Media VALUES(3,2,3,10057,NULL,NULL,NULL,1585305577,0,'Track 3','track3.mp3',0,1,1,0,1,0,NULL,0);
INSERT INTO Media VALUES(4,2,3,10057,NULL,NULL,NULL,1585305577,2013,'Track 4','track4.mp3',0,1,1,0,1,0,1,0);
INSERT INTO Media VALUES(5,2,3,10057,NULL,NULL,NULL,1585305577,2013,'Track 4','track4.mp3',0,0,4,0,1,0,1,0);
INSERT INTO Media VALUES(6,0,0,-1,NULL,NULL,NULL,1585305577,NULL,'BR HD','@224.10.50.36:5004',0,1,NULL,0,NULL,1,NULL,0);
INSERT INTO File VALUES(1,1,NULL,'file:///home/chouquette/dev/medialibrary/test/samples/samples/music/compilation/track.mp3',1,1574114470,139264,1,0,0,0);
INSERT INTO File VALUES(2,2,NULL,'file:///home/chouquette/dev/medialibrary/test/samples/samples/music/compilation/track2.mp3',1,1574114470,139264,1,0,0,0);
INSERT INTO File VALUES(3,3,NULL,'file:///home/chouquette/dev/medialibrary/test/samples/samples/music/compilation/track3.mp3',1,1574114470,139264,1,0,0,0);
INSERT INTO File VALUES(4,4,NULL,'file:///home/chouquette/dev/medialibrary/test/samples/samples/music/compilation/track4.mp3',1,1574114470,139264,1,0,0,0);
INSERT INTO File VALUES(5,5,NULL,'removabletrack.mp3',1,1574114470,139264,2,1,0,0);
INSERT INTO File VALUES(6,6,NULL,'udp://%40224.10.50.36%3A5004',1,0,0,NULL,0,1,0);
INSERT INTO Album VALUES(1,'Compilation',3,0,NULL,4,40228,1,4);
INSERT INTO AlbumTrack VALUES(1,1,10057,4,NULL,0,1,0);
INSERT INTO AlbumTrack VALUES(2,2,10057,5,NULL,0,1,0);
......
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