try to fix broken databases (by rotating the old file and starting a new one)
This commit is contained in:
parent
c40a9eaa45
commit
d800f6cf4f
@ -123,6 +123,7 @@
|
|||||||
#include <QSqlError>
|
#include <QSqlError>
|
||||||
#include <QMetaEnum>
|
#include <QMetaEnum>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
#include <QFileInfo>
|
||||||
|
|
||||||
#define DB_SCHEMA_VERSION 2
|
#define DB_SCHEMA_VERSION 2
|
||||||
|
|
||||||
@ -145,14 +146,20 @@ LogEngine::LogEngine(QObject *parent):
|
|||||||
|
|
||||||
if (!m_db.isValid()) {
|
if (!m_db.isValid()) {
|
||||||
qCWarning(dcLogEngine) << "Database not valid:" << m_db.lastError().driverText() << m_db.lastError().databaseText();
|
qCWarning(dcLogEngine) << "Database not valid:" << m_db.lastError().driverText() << m_db.lastError().databaseText();
|
||||||
return;
|
rotate(m_db.databaseName());
|
||||||
}
|
}
|
||||||
if (!m_db.open()) {
|
if (!m_db.open()) {
|
||||||
qCWarning(dcLogEngine) << "Error opening log database:" << m_db.lastError().driverText() << m_db.lastError().databaseText();
|
qCWarning(dcLogEngine) << "Error opening log database:" << m_db.lastError().driverText() << m_db.lastError().databaseText();
|
||||||
return;
|
rotate(m_db.databaseName());
|
||||||
}
|
}
|
||||||
|
|
||||||
initDB();
|
if (!initDB()) {
|
||||||
|
qCWarning(dcLogEngine()) << "Error initializing database. Trying to correct it.";
|
||||||
|
rotate(m_db.databaseName());
|
||||||
|
if (!initDB()) {
|
||||||
|
qCWarning(dcLogEngine()) << "Error fixing log database. Giving up. Logs can't be stored.";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! Destructs the \l{LogEngine}. */
|
/*! Destructs the \l{LogEngine}. */
|
||||||
@ -376,7 +383,22 @@ void LogEngine::checkDBSize()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void LogEngine::initDB()
|
void LogEngine::rotate(const QString &dbName)
|
||||||
|
{
|
||||||
|
int index = 1;
|
||||||
|
while (QFileInfo(QString("%1.%2").arg(dbName).arg(index)).exists()) {
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
qCDebug(dcLogEngine()) << "Backing up old database file to" << QString("%1.%2").arg(dbName).arg(index);
|
||||||
|
QFile f(dbName);
|
||||||
|
if (!f.rename(QString("%1.%2").arg(dbName).arg(index))) {
|
||||||
|
qCWarning(dcLogEngine()) << "Error backing up old database.";
|
||||||
|
} else {
|
||||||
|
qCDebug(dcLogEngine()) << "Successfully moved old databse";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LogEngine::initDB()
|
||||||
{
|
{
|
||||||
m_db.close();
|
m_db.close();
|
||||||
m_db.open();
|
m_db.open();
|
||||||
@ -397,6 +419,7 @@ void LogEngine::initDB()
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
qCWarning(dcLogEngine) << "Broken log database. Version not found in metadata table.";
|
qCWarning(dcLogEngine) << "Broken log database. Version not found in metadata table.";
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!m_db.tables().contains("sourceTypes")) {
|
if (!m_db.tables().contains("sourceTypes")) {
|
||||||
@ -435,13 +458,16 @@ void LogEngine::initDB()
|
|||||||
"FOREIGN KEY(loggingEventType) REFERENCES loggingEventTypes(id)"
|
"FOREIGN KEY(loggingEventType) REFERENCES loggingEventTypes(id)"
|
||||||
");");
|
");");
|
||||||
|
|
||||||
if (m_db.lastError().isValid())
|
if (m_db.lastError().isValid()) {
|
||||||
qCWarning(dcLogEngine) << "Error creating log table in database. Driver error:" << m_db.lastError().driverText() << "Database error:" << m_db.lastError().databaseText();
|
qCWarning(dcLogEngine) << "Error creating log table in database. Driver error:" << m_db.lastError().driverText() << "Database error:" << m_db.lastError().databaseText();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
qCDebug(dcLogEngine) << "Initialized logging DB successfully. (maximum DB size:" << m_dbMaxSize << ")";
|
qCDebug(dcLogEngine) << "Initialized logging DB successfully. (maximum DB size:" << m_dbMaxSize << ")";
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -52,10 +52,12 @@ private:
|
|||||||
QSqlDatabase m_db;
|
QSqlDatabase m_db;
|
||||||
int m_dbMaxSize;
|
int m_dbMaxSize;
|
||||||
|
|
||||||
void initDB();
|
bool initDB();
|
||||||
void appendLogEntry(const LogEntry &entry);
|
void appendLogEntry(const LogEntry &entry);
|
||||||
void checkDBSize();
|
void checkDBSize();
|
||||||
|
|
||||||
|
void rotate(const QString &dbName);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Only GuhCore and ruleEngine are allowed to log events.
|
// Only GuhCore and ruleEngine are allowed to log events.
|
||||||
friend class GuhCore;
|
friend class GuhCore;
|
||||||
|
|||||||
Reference in New Issue
Block a user