1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "abstractsqlstorage.h"
23 #include <QMutexLocker>
29 #include "logmessage.h"
32 int AbstractSqlStorage::_nextConnectionId = 0;
33 AbstractSqlStorage::AbstractSqlStorage(QObject *parent)
40 AbstractSqlStorage::~AbstractSqlStorage()
42 // disconnect the connections, so their deletion is no longer interessting for us
43 QHash<QThread *, Connection *>::iterator conIter;
44 for (conIter = _connectionPool.begin(); conIter != _connectionPool.end(); ++conIter) {
45 QSqlDatabase::removeDatabase(conIter.value()->name());
46 disconnect(conIter.value(), 0, this, 0);
51 QSqlDatabase AbstractSqlStorage::logDb()
53 if (!_connectionPool.contains(QThread::currentThread()))
54 addConnectionToPool();
56 QSqlDatabase db = QSqlDatabase::database(_connectionPool[QThread::currentThread()]->name(),false);
59 qWarning() << "Database connection" << displayName() << "for thread" << QThread::currentThread() << "was lost, attempting to reconnect...";
67 void AbstractSqlStorage::addConnectionToPool()
69 QMutexLocker locker(&_connectionPoolMutex);
70 // we have to recheck if the connection pool already contains a connection for
71 // this thread. Since now (after the lock) we can only tell for sure
72 if (_connectionPool.contains(QThread::currentThread()))
75 QThread *currentThread = QThread::currentThread();
77 int connectionId = _nextConnectionId++;
79 Connection *connection = new Connection(QLatin1String(QString("quassel_%1_con_%2").arg(driverName()).arg(connectionId).toLatin1()));
80 connection->moveToThread(currentThread);
81 connect(this, SIGNAL(destroyed()), connection, SLOT(deleteLater()));
82 connect(currentThread, SIGNAL(destroyed()), connection, SLOT(deleteLater()));
83 connect(connection, SIGNAL(destroyed()), this, SLOT(connectionDestroyed()));
84 _connectionPool[currentThread] = connection;
86 QSqlDatabase db = QSqlDatabase::addDatabase(driverName(), connection->name());
87 db.setDatabaseName(databaseName());
89 if (!hostName().isEmpty())
90 db.setHostName(hostName());
95 if (!userName().isEmpty()) {
96 db.setUserName(userName());
97 db.setPassword(password());
104 void AbstractSqlStorage::dbConnect(QSqlDatabase &db)
107 quWarning() << "Unable to open database" << displayName() << "for thread" << QThread::currentThread();
108 quWarning() << "-" << db.lastError().text();
111 if (!initDbSession(db)) {
112 quWarning() << "Unable to initialize database" << displayName() << "for thread" << QThread::currentThread();
119 Storage::State AbstractSqlStorage::init(const QVariantMap &settings,
120 const QProcessEnvironment &environment,
121 bool loadFromEnvironment)
123 setConnectionProperties(settings, environment, loadFromEnvironment);
125 _debug = Quassel::isOptionSet("debug");
127 QSqlDatabase db = logDb();
128 if (!db.isValid() || !db.isOpen())
131 if (installedSchemaVersion() == -1) {
132 qCritical() << "Storage Schema is missing!";
136 if (installedSchemaVersion() > schemaVersion()) {
137 qCritical() << "Installed Schema is newer then any known Version.";
141 if (installedSchemaVersion() < schemaVersion()) {
142 quInfo() << qPrintable(tr("Installed database schema (version %1) is not up to date. Upgrading to "
143 "version %2... This may take a while for major upgrades."
144 ).arg(installedSchemaVersion()).arg(schemaVersion()));
145 emit dbUpgradeInProgress(true);
146 auto upgradeResult = upgradeDb();
147 emit dbUpgradeInProgress(false);
148 if (!upgradeResult) {
149 qWarning() << qPrintable(tr("Upgrade failed..."));
152 // Add a message when migration succeeds to avoid confusing folks by implying the schema upgrade failed if
153 // later functionality does not work.
154 quInfo() << qPrintable(tr("Installed database schema successfully upgraded to version %1.").arg(schemaVersion()));
157 quInfo() << qPrintable(displayName()) << "storage backend is ready. Schema version:" << installedSchemaVersion();
162 QString AbstractSqlStorage::queryString(const QString &queryName, int version)
166 // The current schema is stored in the root folder, while upgrade queries are stored in the
167 // 'versions/##' subfolders.
169 // Use the current SQL schema, not a versioned request
170 queryInfo = QFileInfo(QString(":/SQL/%1/%2.sql").arg(displayName()).arg(queryName));
171 // If version is needed later, get it via version = schemaVersion();
173 // Use the specified schema version, not the general folder
174 queryInfo = QFileInfo(QString(":/SQL/%1/version/%2/%3.sql")
175 .arg(displayName()).arg(version).arg(queryName));
178 if (!queryInfo.exists() || !queryInfo.isFile() || !queryInfo.isReadable()) {
179 qCritical() << "Unable to read SQL-Query" << queryName << "for engine" << displayName();
183 QFile queryFile(queryInfo.filePath());
184 if (!queryFile.open(QIODevice::ReadOnly | QIODevice::Text))
186 QString query = QTextStream(&queryFile).readAll();
189 return query.trimmed();
193 QStringList AbstractSqlStorage::setupQueries()
196 // The current schema is stored in the root folder, including setup scripts.
197 QDir dir = QDir(QString(":/SQL/%1/").arg(displayName()));
198 foreach(QFileInfo fileInfo, dir.entryInfoList(QStringList() << "setup*", QDir::NoFilter, QDir::Name)) {
199 queries << queryString(fileInfo.baseName());
205 bool AbstractSqlStorage::setup(const QVariantMap &settings, const QProcessEnvironment &environment,
206 bool loadFromEnvironment)
208 setConnectionProperties(settings, environment, loadFromEnvironment);
209 QSqlDatabase db = logDb();
211 qCritical() << "Unable to setup Logging Backend!";
216 foreach(QString queryString, setupQueries()) {
217 QSqlQuery query = db.exec(queryString);
218 if (!watchQuery(query)) {
219 qCritical() << "Unable to setup Logging Backend!";
224 bool success = setupSchemaVersion(schemaVersion());
233 QList<AbstractSqlStorage::SqlQueryResource> AbstractSqlStorage::upgradeQueries(int version)
235 QList<SqlQueryResource> queries;
236 // Upgrade queries are stored in the 'version/##' subfolders.
237 QDir dir = QDir(QString(":/SQL/%1/version/%2/").arg(displayName()).arg(version));
238 foreach(QFileInfo fileInfo, dir.entryInfoList(QStringList() << "upgrade*", QDir::NoFilter, QDir::Name)) {
239 queries << SqlQueryResource(queryString(fileInfo.baseName(), version), fileInfo.baseName());
245 bool AbstractSqlStorage::upgradeDb()
247 if (schemaVersion() <= installedSchemaVersion())
250 QSqlDatabase db = logDb();
252 // TODO: For databases that support it (e.g. almost only PostgreSQL), wrap upgrades in a
253 // transaction. This will need careful testing of potential additional space requirements and
254 // any database modifications that might not be allowed in a transaction.
256 // Check if we're resuming an interrupted multi-step upgrade: is an upgrade step stored?
257 const QString previousLaunchUpgradeStep = schemaVersionUpgradeStep();
258 bool resumingUpgrade = !previousLaunchUpgradeStep.isEmpty();
260 for (int ver = installedSchemaVersion() + 1; ver <= schemaVersion(); ver++) {
261 foreach (auto queryResource, upgradeQueries(ver)) {
262 if (resumingUpgrade) {
263 // An upgrade was interrupted. Check if this matches the the last successful query.
264 if (previousLaunchUpgradeStep == queryResource.queryFilename) {
265 // Found the matching query!
266 quInfo() << qPrintable(QString("Resuming interrupted upgrade for schema version %1 (last step: %2)")
267 .arg(QString::number(ver), previousLaunchUpgradeStep));
269 // Stop searching for queries
270 resumingUpgrade = false;
271 // Continue past the previous query with the next not-yet-tried query
275 // Not yet matched, keep looking
280 // Run the upgrade query
281 QSqlQuery query = db.exec(queryResource.queryString);
282 if (!watchQuery(query)) {
283 // Individual upgrade query failed, bail out
284 qCritical() << qPrintable(QString("Unable to upgrade Logging Backend! Upgrade query in schema version %1 failed (step: %2).")
285 .arg(QString::number(ver), queryResource.queryFilename));
289 // Mark as successful
290 setSchemaVersionUpgradeStep(queryResource.queryFilename);
294 if (resumingUpgrade) {
295 // Something went wrong and the last successful SQL query to resume from couldn't be
297 // 1. The storage of successful query glitched, or the database was manually changed
298 // 2. Quassel changed the filenames of upgrade queries, and the local Quassel core
299 // version was replaced during an interrupted schema upgrade
301 // Both are unlikely, but it's a good idea to handle it anyways.
303 qCritical() << qPrintable(QString("Unable to resume interrupted upgrade in Logging "
304 "Backend! Missing upgrade step in schema version %1 "
305 "(expected step: %2)")
306 .arg(QString::number(ver), previousLaunchUpgradeStep));
310 // Update the schema version for each intermediate step and mark the step as done. This
311 // ensures that any interrupted upgrades have a greater chance of resuming correctly after
314 // Almost all databases make single queries atomic (fully works or fully fails, no partial),
315 // and with many of the longest migrations being a single query, this makes upgrade
316 // interruptions much more likely to leave the database in a valid intermediate schema
318 if (!updateSchemaVersion(ver, true)) {
319 // Updating the schema version failed, bail out
320 qCritical() << "Unable to upgrade Logging Backend! Setting schema version"
326 // Update the schema version for the final step. Split this out to offer more informative
327 // logging (though setting schema version really should not fail).
328 if (!updateSchemaVersion(schemaVersion())) {
329 // Updating the final schema version failed, bail out
330 qCritical() << "Unable to upgrade Logging Backend! Setting final schema version"
331 << schemaVersion() << "failed.";
335 // If we made it here, everything seems to have worked!
340 int AbstractSqlStorage::schemaVersion()
342 // returns the newest Schema Version!
343 // not the currently used one! (though it can be the same)
344 if (_schemaVersion > 0)
345 return _schemaVersion;
349 // Schema versions are stored in the 'version/##' subfolders.
350 QDir dir = QDir(QString(":/SQL/%1/version/").arg(displayName()));
351 foreach(QFileInfo fileInfo, dir.entryInfoList()) {
352 if (!fileInfo.isDir())
355 version = fileInfo.fileName().toInt(&ok);
359 if (version > _schemaVersion)
360 _schemaVersion = version;
362 return _schemaVersion;
366 QString AbstractSqlStorage::schemaVersionUpgradeStep()
368 // By default, assume there's no pending upgrade
373 bool AbstractSqlStorage::watchQuery(QSqlQuery &query)
375 bool queryError = query.lastError().isValid();
376 if (queryError || _debug) {
378 qCritical() << "unhandled Error in QSqlQuery!";
379 qCritical() << " last Query:\n" << qPrintable(query.lastQuery());
380 qCritical() << " executed Query:\n" << qPrintable(query.executedQuery());
381 QVariantMap boundValues = query.boundValues();
382 QStringList valueStrings;
383 QVariantMap::const_iterator iter;
384 for (iter = boundValues.constBegin(); iter != boundValues.constEnd(); ++iter) {
387 if (query.driver()) {
388 // let the driver do the formatting
389 field.setType(iter.value().type());
390 if (iter.value().isNull())
393 field.setValue(iter.value());
394 value = query.driver()->formatValue(field);
397 switch (iter.value().type()) {
398 case QVariant::Invalid:
402 value = iter.value().toString();
405 value = QString("'%1'").arg(iter.value().toString());
408 valueStrings << QString("%1=%2").arg(iter.key(), value);
410 qCritical() << " bound Values:" << qPrintable(valueStrings.join(", "));
411 qCritical() << " Error Number:" << query.lastError().number();
412 qCritical() << " Error Message:" << qPrintable(query.lastError().text());
413 qCritical() << " Driver Message:" << qPrintable(query.lastError().driverText());
414 qCritical() << " DB Message:" << qPrintable(query.lastError().databaseText());
422 void AbstractSqlStorage::connectionDestroyed()
424 QMutexLocker locker(&_connectionPoolMutex);
425 _connectionPool.remove(sender()->thread());
429 // ========================================
430 // AbstractSqlStorage::Connection
431 // ========================================
432 AbstractSqlStorage::Connection::Connection(const QString &name, QObject *parent)
434 _name(name.toLatin1())
439 AbstractSqlStorage::Connection::~Connection()
442 QSqlDatabase db = QSqlDatabase::database(name(), false);
448 QSqlDatabase::removeDatabase(name());
452 // ========================================
453 // AbstractSqlMigrator
454 // ========================================
455 AbstractSqlMigrator::AbstractSqlMigrator()
461 void AbstractSqlMigrator::newQuery(const QString &query, QSqlDatabase db)
464 _query = new QSqlQuery(db);
465 _query->prepare(query);
469 void AbstractSqlMigrator::resetQuery()
476 bool AbstractSqlMigrator::exec()
480 return !_query->lastError().isValid();
484 QString AbstractSqlMigrator::migrationObject(MigrationObject moType)
488 return "QuasselUser";
494 return "IdentityNick";
504 return "UserSetting";
512 QVariantList AbstractSqlMigrator::boundValues()
518 int numValues = _query->boundValues().count();
519 for (int i = 0; i < numValues; i++) {
520 values << _query->boundValue(i);
526 void AbstractSqlMigrator::dumpStatus()
528 qWarning() << " executed Query:";
529 qWarning() << qPrintable(executedQuery());
530 qWarning() << " bound Values:";
531 QList<QVariant> list = boundValues();
532 for (int i = 0; i < list.size(); ++i)
533 qWarning() << i << ": " << list.at(i).toString().toLatin1().data();
534 qWarning() << " Error Number:" << lastError().number();
535 qWarning() << " Error Message:" << lastError().text();
539 // ========================================
540 // AbstractSqlMigrationReader
541 // ========================================
542 AbstractSqlMigrationReader::AbstractSqlMigrationReader()
543 : AbstractSqlMigrator(),
549 bool AbstractSqlMigrationReader::migrateTo(AbstractSqlMigrationWriter *writer)
551 if (!transaction()) {
552 qWarning() << "AbstractSqlMigrationReader::migrateTo(): unable to start reader's transaction!";
555 if (!writer->transaction()) {
556 qWarning() << "AbstractSqlMigrationReader::migrateTo(): unable to start writer's transaction!";
557 rollback(); // close the reader transaction;
563 // due to the incompatibility across Migration objects we can't run this in a loop... :/
564 QuasselUserMO quasselUserMo;
565 if (!transferMo(QuasselUser, quasselUserMo))
568 IdentityMO identityMo;
569 if (!transferMo(Identity, identityMo))
572 IdentityNickMO identityNickMo;
573 if (!transferMo(IdentityNick, identityNickMo))
577 if (!transferMo(Network, networkMo))
581 if (!transferMo(Buffer, bufferMo))
585 if (!transferMo(Sender, senderMo))
589 if (!transferMo(Backlog, backlogMo))
592 IrcServerMO ircServerMo;
593 if (!transferMo(IrcServer, ircServerMo))
596 UserSettingMO userSettingMo;
597 if (!transferMo(UserSetting, userSettingMo))
600 CoreStateMO coreStateMO;
601 if (!transferMo(CoreState, coreStateMO))
604 if (!_writer->postProcess())
606 return finalizeMigration();
610 void AbstractSqlMigrationReader::abortMigration(const QString &errorMsg)
612 qWarning() << "Migration Failed!";
613 if (!errorMsg.isNull()) {
614 qWarning() << qPrintable(errorMsg);
616 if (lastError().isValid()) {
617 qWarning() << "ReaderError:";
621 if (_writer->lastError().isValid()) {
622 qWarning() << "WriterError:";
623 _writer->dumpStatus();
632 bool AbstractSqlMigrationReader::finalizeMigration()
635 _writer->resetQuery();
638 if (!_writer->commit()) {
648 bool AbstractSqlMigrationReader::transferMo(MigrationObject moType, T &mo)
651 _writer->resetQuery();
653 if (!prepareQuery(moType)) {
654 abortMigration(QString("AbstractSqlMigrationReader::migrateTo(): unable to prepare reader query of type %1!").arg(AbstractSqlMigrator::migrationObject(moType)));
657 if (!_writer->prepareQuery(moType)) {
658 abortMigration(QString("AbstractSqlMigrationReader::migrateTo(): unable to prepare writer query of type %1!").arg(AbstractSqlMigrator::migrationObject(moType)));
662 qDebug() << qPrintable(QString("Transferring %1...").arg(AbstractSqlMigrator::migrationObject(moType)));
665 file.open(stdout, QIODevice::WriteOnly);
668 if (!_writer->writeMo(mo)) {
669 abortMigration(QString("AbstractSqlMigrationReader::transferMo(): unable to transfer Migratable Object of type %1!").arg(AbstractSqlMigrator::migrationObject(moType)));
687 uint qHash(const SenderData &key) {
688 return qHash(QString(key.sender + "\n" + key.realname + "\n" + key.avatarurl));
691 bool operator==(const SenderData &a, const SenderData &b) {
692 return a.sender == b.sender &&
693 a.realname == b.realname &&
694 a.avatarurl == b.avatarurl;