Skip to content

Commit 7e69936

Browse files
committed
[auth][bugfix] Migrate qgis-auth.db from QGIS 2 to 3
Fixes #17403
1 parent cd23779 commit 7e69936

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

src/app/qgsversionmigration.cpp

Lines changed: 67 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,32 @@ 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+
for ( const auto &err : settingsErrors.messageList( ) )
62+
{
63+
errors.append( err );
64+
}
6165
}
62-
QgsError stylesError = migrateStyles();
63-
return error;
66+
QgsError stylesErrors = migrateStyles();
67+
if ( !stylesErrors.isEmpty() )
68+
{
69+
for ( const auto &err : stylesErrors.messageList( ) )
70+
{
71+
errors.append( err );
72+
}
73+
}
74+
QgsError authDbErrors = migrateAuthDb();
75+
if ( !authDbErrors.isEmpty() )
76+
{
77+
for ( const auto &err : authDbErrors.messageList( ) )
78+
{
79+
errors.append( err );
80+
}
81+
}
82+
return errors;
6483
}
6584

6685
bool Qgs2To3Migration::requiresMigration()
@@ -242,6 +261,50 @@ QgsError Qgs2To3Migration::migrateSettings()
242261
return error;
243262
}
244263

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

0 commit comments

Comments
 (0)