support mysql -> sqlite fallback

This commit is contained in:
Martin Rotter 2021-03-15 13:58:26 +01:00
parent e306fc1182
commit 98be85fa14
2 changed files with 10 additions and 11 deletions

View File

@ -52,15 +52,14 @@ void DatabaseFactory::determineDriver() {
}
catch (const ApplicationException& ex) {
qCriticalNN << LOGSEC_DB
<< "Failed to reach connection to DB source, will fallback to SQLite:"
<< "Failed to reach connection to DB source, let's fallback to SQLite:"
<< QUOTE_W_SPACE_DOT(ex.message());
qApp->showGuiMessage(tr("Cannot connect to database"),
tr("Connection to your database was not established with error '%1'. "
"Falling back to SQLite.").arg(ex.message()),
QSystemTrayIcon::MessageIcon::Critical,
nullptr,
true);
MessageBox::show(nullptr,
QMessageBox::Icon::Critical,
tr("Cannot connect to database"),
tr("Connection to your database was not established with error: '%1'. "
"Falling back to SQLite.").arg(ex.message()));
m_dbDriver = boolinq::from(m_allDbDrivers).first([](DatabaseDriver* driv) {
return driv->driverType() == DatabaseDriver::DriverType::SQLite;

View File

@ -150,8 +150,8 @@ QSqlDatabase MariaDbDriver::initializeDatabase(const QString& connection_name) {
database.setPassword(qApp->settings()->password(GROUP(Database), SETTING(Database::MySQLPassword)).toString());
if (!database.open()) {
qFatal("Cannot open MySQL database: %s. Make sure your DB server is running and "
"start application again.", qPrintable(database.lastError().text()));
// NOTE: In this case throw exception and fallback SQL backend will be used.
throw ApplicationException(database.lastError().text());
}
else {
QSqlQuery query_db(database);
@ -279,8 +279,8 @@ QSqlDatabase MariaDbDriver::connection(const QString& connection_name, DatabaseD
}
if (!database.isOpen() && !database.open()) {
qFatal("MySQL database was NOT opened. Delivered error message: '%s'.",
qPrintable(database.lastError().text()));
// NOTE: In this case throw exception and fallback SQL backend will be used.
throw ApplicationException(database.lastError().text());
}
else {
qDebugNN << LOGSEC_DB