Skip to content

Commit 7c9cd49

Browse files
authored
Merge pull request #5546 from elpaso/auth_migrate
[auth][bugfix] Auth DB migrate
2 parents c000cb8 + b57ec46 commit 7c9cd49

File tree

4 files changed

+82
-4
lines changed

4 files changed

+82
-4
lines changed

python/core/qgserror.sip

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ class QgsError
123123
Clear error messages
124124
%End
125125

126+
QList<QgsErrorMessage> messageList() const;
127+
%Docstring
128+
messageList return the list of current error messages
129+
:return: current list of error messages
130+
:rtype: list of QgsErrorMessage
131+
%End
132+
126133
};
127134

128135
/************************************************************************

src/app/qgsversionmigration.cpp

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "qgsstyle.h"
2424
#include "qgssymbollayerutils.h"
2525
#include "qgsreadwritecontext.h"
26+
#include "qgsuserprofilemanager.h"
2627

2728
#include <QFile>
2829
#include <QTextStream>
@@ -53,14 +54,35 @@ QgsVersionMigration *QgsVersionMigration::canMigrate( int fromVersion, int toVer
5354

5455
QgsError Qgs2To3Migration::runMigration()
5556
{
56-
QgsError error;
57+
QgsError errors;
5758
QgsError settingsErrors = migrateSettings();
5859
if ( !settingsErrors.isEmpty() )
5960
{
60-
// TODO Merge error messages
61+
const QList<QgsErrorMessage> errorList( settingsErrors.messageList( ) );
62+
for ( const auto &err : errorList )
63+
{
64+
errors.append( err );
65+
}
6166
}
62-
QgsError stylesError = migrateStyles();
63-
return error;
67+
QgsError stylesErrors = migrateStyles();
68+
if ( !stylesErrors.isEmpty() )
69+
{
70+
const QList<QgsErrorMessage> errorList( stylesErrors.messageList( ) );
71+
for ( const auto &err : errorList )
72+
{
73+
errors.append( err );
74+
}
75+
}
76+
QgsError authDbErrors = migrateAuthDb();
77+
if ( !authDbErrors.isEmpty() )
78+
{
79+
const QList<QgsErrorMessage> errorList( authDbErrors.messageList( ) );
80+
for ( const auto &err : errorList )
81+
{
82+
errors.append( err );
83+
}
84+
}
85+
return errors;
6486
}
6587

6688
bool Qgs2To3Migration::requiresMigration()
@@ -242,6 +264,48 @@ QgsError Qgs2To3Migration::migrateSettings()
242264
return error;
243265
}
244266

267+
QgsError Qgs2To3Migration::migrateAuthDb()
268+
{
269+
QgsError error;
270+
QString oldHome = QStringLiteral( "%1/.qgis2" ).arg( QDir::homePath() );
271+
QString oldAuthDbFilePath = QStringLiteral( "%1/qgis-auth.db" ).arg( oldHome );
272+
// Try to retrieve the current profile folder (I didn't find an QgsApplication API for it)
273+
QDir settingsDir = QFileInfo( QgsSettings().fileName() ).absoluteDir();
274+
settingsDir.cdUp();
275+
QString newAuthDbFilePath = QStringLiteral( "%1/qgis-auth.db" ).arg( settingsDir.absolutePath() );
276+
// Do not overwrite!
277+
if ( QFile( newAuthDbFilePath ).exists( ) )
278+
{
279+
QString msg = QStringLiteral( "Could not copy old auth DB to %1: file already exists!" ).arg( newAuthDbFilePath );
280+
QgsDebugMsg( msg );
281+
error.append( msg );
282+
}
283+
else
284+
{
285+
QFile oldDbFile( oldAuthDbFilePath );
286+
if ( oldDbFile.exists( ) )
287+
{
288+
if ( oldDbFile.copy( newAuthDbFilePath ) )
289+
{
290+
QgsDebugMsg( QStringLiteral( "Old auth DB successfully copied to %1" ).arg( newAuthDbFilePath ) );
291+
}
292+
else
293+
{
294+
QString msg = QStringLiteral( "Could not copy auth DB %1 to %2" ).arg( oldAuthDbFilePath, newAuthDbFilePath );
295+
QgsDebugMsg( msg );
296+
error.append( msg );
297+
}
298+
}
299+
else
300+
{
301+
QString msg = QStringLiteral( "Could not copy auth DB %1 to %2: old DB does not exists!" ).arg( oldAuthDbFilePath, newAuthDbFilePath );
302+
QgsDebugMsg( msg );
303+
error.append( msg );
304+
}
305+
}
306+
return error;
307+
}
308+
245309
QList<QPair<QString, QString> > Qgs2To3Migration::walk( QString group, QString newkey )
246310
{
247311
mOldSettings->beginGroup( group );

src/app/qgsversionmigration.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class Qgs2To3Migration : public QgsVersionMigration
5858
private:
5959
QgsError migrateStyles();
6060
QgsError migrateSettings();
61+
QgsError migrateAuthDb();
6162

6263
QList<QPair<QString, QString>> walk( QString group, QString newkey );
6364
QPair<QString, QString> transformKey( QString fullOldKey, QString newKeyPart );

src/core/qgserror.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ class CORE_EXPORT QgsError
128128
//! Clear error messages
129129
void clear() { mMessageList.clear(); }
130130

131+
/**
132+
* \brief messageList return the list of current error messages
133+
* \return current list of error messages
134+
*/
135+
QList<QgsErrorMessage> messageList() const { return mMessageList; }
136+
131137
private:
132138
//! List of messages
133139
QList<QgsErrorMessage> mMessageList;

0 commit comments

Comments
 (0)