Skip to content

Commit

Permalink
[auth][bugfix] Migrate qgis-auth.db from QGIS 2 to 3
Browse files Browse the repository at this point in the history
Fixes #17403
  • Loading branch information
elpaso committed Nov 6, 2017
1 parent cd23779 commit 7e69936
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
71 changes: 67 additions & 4 deletions src/app/qgsversionmigration.cpp
Expand Up @@ -23,6 +23,7 @@
#include "qgsstyle.h"
#include "qgssymbollayerutils.h"
#include "qgsreadwritecontext.h"
#include "qgsuserprofilemanager.h"

#include <QFile>
#include <QTextStream>
Expand Down Expand Up @@ -53,14 +54,32 @@ QgsVersionMigration *QgsVersionMigration::canMigrate( int fromVersion, int toVer

QgsError Qgs2To3Migration::runMigration()
{
QgsError error;
QgsError errors;
QgsError settingsErrors = migrateSettings();
if ( !settingsErrors.isEmpty() )
{
// TODO Merge error messages
for ( const auto &err : settingsErrors.messageList( ) )
{
errors.append( err );
}
}
QgsError stylesError = migrateStyles();
return error;
QgsError stylesErrors = migrateStyles();
if ( !stylesErrors.isEmpty() )
{
for ( const auto &err : stylesErrors.messageList( ) )
{
errors.append( err );
}
}
QgsError authDbErrors = migrateAuthDb();
if ( !authDbErrors.isEmpty() )
{
for ( const auto &err : authDbErrors.messageList( ) )
{
errors.append( err );
}
}
return errors;
}

bool Qgs2To3Migration::requiresMigration()
Expand Down Expand Up @@ -242,6 +261,50 @@ QgsError Qgs2To3Migration::migrateSettings()
return error;
}

QgsError Qgs2To3Migration::migrateAuthDb()
{
QgsError error;
QString oldHome = QStringLiteral( "%1/.qgis2" ).arg( QDir::homePath() );
QString oldAuthDbFilePath = QStringLiteral( "%1/qgis-auth.db" ).arg( oldHome );
// Try to retrieve the current profile folder (I didn't find an QgsApplication API for it)
QDir settingsDir = QFileInfo( QgsSettings().fileName() ).absoluteDir();
settingsDir.cdUp();
QString newAuthDbFilePath = QStringLiteral( "%1/qgis-auth.db" ).arg( settingsDir.absolutePath() );
// Do not overwrite!
if ( QFile( newAuthDbFilePath ).exists( ) )
{
QString msg = QString( "Could not copy old auth DB to %1: file already exists!" ).arg( newAuthDbFilePath );
QgsDebugMsg( msg );
error.append( msg );

}
else
{
QgsDebugMsg( QString( "OLD AUTH DB FILE %1" ).arg( oldAuthDbFilePath ) );
QFile oldDbFile( oldAuthDbFilePath );
if ( oldDbFile.exists( ) )
{
if ( oldDbFile.copy( newAuthDbFilePath ) )
{
QgsDebugMsg( QStringLiteral( "Old auth DB successfully copied to %1" ).arg( newAuthDbFilePath ) );
}
else
{
QString msg = QString( "Could not copy auth DB %1 to %2" ).arg( oldAuthDbFilePath, newAuthDbFilePath );
QgsDebugMsg( msg );
error.append( msg );
}
}
else
{
QString msg = QString( "Could not copy auth DB %1 to %2: old DB does not exists!" ).arg( oldAuthDbFilePath, newAuthDbFilePath );
QgsDebugMsg( msg );
error.append( msg );
}
}
return error;
}

QList<QPair<QString, QString> > Qgs2To3Migration::walk( QString group, QString newkey )
{
mOldSettings->beginGroup( group );
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsversionmigration.h
Expand Up @@ -58,6 +58,7 @@ class Qgs2To3Migration : public QgsVersionMigration
private:
QgsError migrateStyles();
QgsError migrateSettings();
QgsError migrateAuthDb();

QList<QPair<QString, QString>> walk( QString group, QString newkey );
QPair<QString, QString> transformKey( QString fullOldKey, QString newKeyPart );
Expand Down

0 comments on commit 7e69936

Please sign in to comment.