Commit 217fc7bd authored by Antonio Larrosa Jimenez's avatar Antonio Larrosa Jimenez Committed by Antonio Larrosa Jimenez
Browse files

Check availability of Postgresql backend and clean a bit the code

Rename `areRequirementsAvailable` method to `isAvailable` and implement the
method for DbConfigPostgresql too.
Extract the code from `DbConfig::configuredDatabase` that selects the fallback
to use to a new separate static method, `defaultAvailableDatabaseBackend`
to be more clear about what that code does and simplify `configuredDatabase`.

Now if Postgresql is expected to be used (because akonadi was built like that)
but QPSQL is not available or the initdb/pg_ctl binaries don't exist, then we
fallback to trying to use QSQLITE3.

As with the `DbConfigMysql` change, if QSQLITE3 can't be used either, then
the code will continue trying to use postgresql and fail as expected.
parent 8240e111
......@@ -49,6 +49,28 @@ bool DbConfig::isConfigured()
return s_DbConfigInstance;
}
QString DbConfig::defaultAvailableDatabaseBackend(QSettings &settings)
{
QString driverName = QStringLiteral(AKONADI_DATABASE_BACKEND);
DbConfig *dbConfigFallbackTest = 0L;
if (driverName == QLatin1String("QMYSQL")) {
dbConfigFallbackTest = new DbConfigMysql;
} else if (driverName == QLatin1String("QPSQL")) {
dbConfigFallbackTest = new DbConfigPostgresql;
}
if (dbConfigFallbackTest && !dbConfigFallbackTest->isAvailable(settings)
&& DbConfigSqlite(DbConfigSqlite::Custom).isAvailable(settings)) {
qCWarning(AKONADISERVER_LOG) << driverName << " requirements not available. Falling back to using QSQLITE3.";
driverName = QStringLiteral("QSQLITE3");
}
delete dbConfigFallbackTest;
return driverName;
}
DbConfig *DbConfig::configuredDatabase()
{
if (!s_DbConfigInstance) {
......@@ -58,17 +80,8 @@ DbConfig *DbConfig::configuredDatabase()
// determine driver to use
QString driverName = settings.value(QStringLiteral("General/Driver")).toString();
if (driverName.isEmpty()) {
driverName = QStringLiteral(AKONADI_DATABASE_BACKEND);
// If we fallback to use QMYSQL by default, let's check that it's really available.
if (driverName == QLatin1String("QMYSQL")) {
DbConfigMysql dbConfigMysql;
if (!dbConfigMysql.areRequirementsAvailable(settings)
&& DbConfigSqlite(DbConfigSqlite::Custom).areRequirementsAvailable(settings)) {
qCWarning(AKONADISERVER_LOG) << "QMYSQL requirements not available. Falling back to using QSQLITE3.";
driverName = QStringLiteral("QSQLITE3");
}
}
driverName = defaultAvailableDatabaseBackend(settings);
// when using the default, write it explicitly, in case the default changes later
settings.setValue(QStringLiteral("General/Driver"), driverName);
settings.sync();
......
......@@ -55,6 +55,12 @@ public:
*/
virtual bool init(QSettings &settings, bool storeSettings=true) = 0;
/**
* This method checks if the requirements for this database connection are met
* in the system (i.e. QMYSQL driver is available, mysqld binary is found, etc.).
*/
virtual bool isAvailable(QSettings &settings) = 0;
/**
* This method applies the configured settings to the QtSql @p database
* instance.
......@@ -105,6 +111,13 @@ protected:
*/
static QString defaultDatabaseName();
/*
* Returns the Database backend we should use by default. Usually it should be the same value
* configured as AKONADI_DATABASE_BACKEND at build time, but this method checks if that
* backend is really available and if it's not, it falls back to returning "QSQLITE3".
*/
static QString defaultAvailableDatabaseBackend(QSettings &settings);
/**
* Calls QProcess::execute() and also prints the command and arguments via qCDebug()
*/
......
......@@ -158,7 +158,7 @@ bool DbConfigMysql::init(QSettings &settings, bool storeSettings)
return true;
}
bool DbConfigMysql::areRequirementsAvailable(QSettings &settings)
bool DbConfigMysql::isAvailable(QSettings &settings)
{
if (!QSqlDatabase::drivers().contains(driverName()))
return false;
......
......@@ -46,7 +46,7 @@ public:
* This method checks if the requirements for this database connection are met
* in the system (QMYSQL driver is available, mysqld binary is found, etc.).
*/
bool areRequirementsAvailable(QSettings &settings);
bool isAvailable(QSettings &settings) override;
/**
* This method applies the configured settings to the QtSql @p database
......
......@@ -205,6 +205,25 @@ bool DbConfigPostgresql::init(QSettings &settings, bool storeSettings)
return true;
}
bool DbConfigPostgresql::isAvailable(QSettings &settings)
{
if (!QSqlDatabase::drivers().contains(driverName()))
return false;
if (!init(settings, false))
return false;
if (mInternalServer) {
if (mInitDbPath.isEmpty() || !QFile::exists(mInitDbPath))
return false;
if (mServerPath.isEmpty() || !QFile::exists(mServerPath))
return false;
}
return true;
}
void DbConfigPostgresql::apply(QSqlDatabase &database)
{
if (!mDatabaseName.isEmpty()) {
......
......@@ -40,6 +40,13 @@ public:
*/
bool init(QSettings &settings, bool storeSettings=true) override;
/**
* This method checks if the requirements for this database connection are
* met in the system (QPOSTGRESQL driver is available, postgresql binary is
* found, etc.).
*/
bool isAvailable(QSettings &settings) override;
/**
* This method applies the configured settings to the QtSql @p database
* instance.
......
......@@ -100,7 +100,7 @@ bool DbConfigSqlite::init(QSettings &settings, bool storeSettings)
return true;
}
bool DbConfigSqlite::areRequirementsAvailable(QSettings &settings)
bool DbConfigSqlite::isAvailable(QSettings &settings)
{
if (!QSqlDatabase::drivers().contains(driverName()))
return false;
......
......@@ -48,7 +48,7 @@ public:
* This method checks if the requirements for this database connection are met
* in the system (QSQLITE/QSQLITE3 driver is available, object can be initialized, etc.).
*/
bool areRequirementsAvailable(QSettings &settings);
bool isAvailable(QSettings &settings) override;
/**
* This method applies the configured settings to the QtSql @p database
......
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