+QString AbstractSqlStorage::queryString(const QString &queryName, int version)
+{
+ QFileInfo queryInfo;
+
+ // The current schema is stored in the root folder, while upgrade queries are stored in the
+ // 'versions/##' subfolders.
+ if (version == 0) {
+ // Use the current SQL schema, not a versioned request
+ queryInfo = QFileInfo(QString(":/SQL/%1/%2.sql").arg(displayName()).arg(queryName));
+ // If version is needed later, get it via version = schemaVersion();
+ } else {
+ // Use the specified schema version, not the general folder
+ queryInfo = QFileInfo(QString(":/SQL/%1/version/%2/%3.sql")
+ .arg(displayName()).arg(version).arg(queryName));
+ }
+
+ if (!queryInfo.exists() || !queryInfo.isFile() || !queryInfo.isReadable()) {
+ qCritical() << "Unable to read SQL-Query" << queryName << "for engine" << displayName();
+ return QString();
+ }
+
+ QFile queryFile(queryInfo.filePath());
+ if (!queryFile.open(QIODevice::ReadOnly | QIODevice::Text))
+ return QString();
+ QString query = QTextStream(&queryFile).readAll();
+ queryFile.close();
+
+ return query.trimmed();
+}
+
+
+QStringList AbstractSqlStorage::setupQueries()
+{
+ QStringList queries;
+ // The current schema is stored in the root folder, including setup scripts.
+ QDir dir = QDir(QString(":/SQL/%1/").arg(displayName()));
+ foreach(QFileInfo fileInfo, dir.entryInfoList(QStringList() << "setup*", QDir::NoFilter, QDir::Name)) {
+ queries << queryString(fileInfo.baseName());
+ }
+ return queries;
+}
+
+
+bool AbstractSqlStorage::setup(const QVariantMap &settings, const QProcessEnvironment &environment,
+ bool loadFromEnvironment)
+{
+ setConnectionProperties(settings, environment, loadFromEnvironment);
+ QSqlDatabase db = logDb();
+ if (!db.isOpen()) {
+ qCritical() << "Unable to setup Logging Backend!";
+ return false;
+ }
+
+ db.transaction();
+ foreach(QString queryString, setupQueries()) {
+ QSqlQuery query = db.exec(queryString);
+ if (!watchQuery(query)) {
+ qCritical() << "Unable to setup Logging Backend!";
+ db.rollback();
+ return false;
+ }
+ }
+ bool success = setupSchemaVersion(schemaVersion());
+ if (success)
+ db.commit();
+ else
+ db.rollback();
+ return success;
+}
+
+
+QStringList AbstractSqlStorage::upgradeQueries(int version)
+{
+ QStringList queries;
+ // Upgrade queries are stored in the 'version/##' subfolders.
+ QDir dir = QDir(QString(":/SQL/%1/version/%2/").arg(displayName()).arg(version));
+ foreach(QFileInfo fileInfo, dir.entryInfoList(QStringList() << "upgrade*", QDir::NoFilter, QDir::Name)) {
+ queries << queryString(fileInfo.baseName(), version);
+ }
+ return queries;
+}
+
+
+bool AbstractSqlStorage::upgradeDb()
+{
+ if (schemaVersion() <= installedSchemaVersion())
+ return true;
+
+ QSqlDatabase db = logDb();
+
+ // TODO: For databases that support it (e.g. almost only PostgreSQL), wrap upgrades in a
+ // transaction. This will need careful testing of potential additional space requirements and
+ // any database modifications that might not be allowed in a transaction.
+
+ for (int ver = installedSchemaVersion() + 1; ver <= schemaVersion(); ver++) {
+ foreach(QString queryString, upgradeQueries(ver)) {
+ QSqlQuery query = db.exec(queryString);
+ if (!watchQuery(query)) {
+ // Individual upgrade query failed, bail out
+ qCritical() << "Unable to upgrade Logging Backend! Upgrade query in schema version"
+ << ver << "failed.";
+ return false;
+ }
+ }
+
+ // Update the schema version for each intermediate step. This ensures that any interrupted
+ // upgrades have a greater chance of resuming correctly after core restart.
+ //
+ // Almost all databases make single queries atomic (fully works or fully fails, no partial),
+ // and with many of the longest migrations being a single query, this makes upgrade
+ // interruptions much more likely to leave the database in a valid intermediate schema
+ // version.
+ if (!updateSchemaVersion(ver)) {
+ // Updating the schema version failed, bail out
+ qCritical() << "Unable to upgrade Logging Backend! Setting schema version"
+ << ver << "failed.";
+ return false;
+ }
+ }
+
+ // Update the schema version for the final step. Split this out to offer more informative
+ // logging (though setting schema version really should not fail).
+ if (!updateSchemaVersion(schemaVersion())) {
+ // Updating the final schema version failed, bail out
+ qCritical() << "Unable to upgrade Logging Backend! Setting final schema version"
+ << schemaVersion() << "failed.";
+ return false;
+ }
+
+ // If we made it here, everything seems to have worked!
+ return true;
+}
+
+
+int AbstractSqlStorage::schemaVersion()
+{
+ // returns the newest Schema Version!
+ // not the currently used one! (though it can be the same)
+ if (_schemaVersion > 0)
+ return _schemaVersion;
+
+ int version;
+ bool ok;
+ // Schema versions are stored in the 'version/##' subfolders.
+ QDir dir = QDir(QString(":/SQL/%1/version/").arg(displayName()));
+ foreach(QFileInfo fileInfo, dir.entryInfoList()) {
+ if (!fileInfo.isDir())
+ continue;
+
+ version = fileInfo.fileName().toInt(&ok);
+ if (!ok)
+ continue;
+
+ if (version > _schemaVersion)
+ _schemaVersion = version;
+ }