1 /***************************************************************************
2 * Copyright (C) 2005-2018 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)
37 AbstractSqlStorage::~AbstractSqlStorage()
39 // disconnect the connections, so their deletion is no longer interessting for us
40 QHash<QThread*, Connection*>::iterator conIter;
41 for (conIter = _connectionPool.begin(); conIter != _connectionPool.end(); ++conIter) {
42 QSqlDatabase::removeDatabase(conIter.value()->name());
43 disconnect(conIter.value(), nullptr, this, nullptr);
47 QSqlDatabase AbstractSqlStorage::logDb()
49 if (!_connectionPool.contains(QThread::currentThread()))
50 addConnectionToPool();
52 QSqlDatabase db = QSqlDatabase::database(_connectionPool[QThread::currentThread()]->name(), false);
55 qWarning() << "Database connection" << displayName() << "for thread" << QThread::currentThread()
56 << "was lost, attempting to reconnect...";
63 void AbstractSqlStorage::addConnectionToPool()
65 QMutexLocker locker(&_connectionPoolMutex);
66 // we have to recheck if the connection pool already contains a connection for
67 // this thread. Since now (after the lock) we can only tell for sure
68 if (_connectionPool.contains(QThread::currentThread()))
71 QThread* currentThread = QThread::currentThread();
73 int connectionId = _nextConnectionId++;
75 Connection* connection = new Connection(QLatin1String(QString("quassel_%1_con_%2").arg(driverName()).arg(connectionId).toLatin1()));
76 connection->moveToThread(currentThread);
77 connect(this, &QObject::destroyed, connection, &QObject::deleteLater);
78 connect(currentThread, &QObject::destroyed, connection, &QObject::deleteLater);
79 connect(connection, &QObject::destroyed, this, &AbstractSqlStorage::connectionDestroyed);
80 _connectionPool[currentThread] = connection;
82 QSqlDatabase db = QSqlDatabase::addDatabase(driverName(), connection->name());
83 db.setDatabaseName(databaseName());
85 if (!hostName().isEmpty())
86 db.setHostName(hostName());
91 if (!userName().isEmpty()) {
92 db.setUserName(userName());
93 db.setPassword(password());
99 void AbstractSqlStorage::dbConnect(QSqlDatabase& db)
102 quWarning() << "Unable to open database" << displayName() << "for thread" << QThread::currentThread();
103 quWarning() << "-" << db.lastError().text();
106 if (!initDbSession(db)) {
107 quWarning() << "Unable to initialize database" << displayName() << "for thread" << QThread::currentThread();
113 Storage::State AbstractSqlStorage::init(const QVariantMap& settings, const QProcessEnvironment& environment, bool loadFromEnvironment)
115 setConnectionProperties(settings, environment, loadFromEnvironment);
117 _debug = Quassel::isOptionSet("debug");
119 QSqlDatabase db = logDb();
120 if (!db.isValid() || !db.isOpen())
123 if (installedSchemaVersion() == -1) {
124 qCritical() << "Storage Schema is missing!";
128 if (installedSchemaVersion() > schemaVersion()) {
129 qCritical() << "Installed Schema is newer then any known Version.";
133 if (installedSchemaVersion() < schemaVersion()) {
134 quInfo() << qPrintable(tr("Installed database schema (version %1) is not up to date. Upgrading to "
135 "version %2... This may take a while for major upgrades.")
136 .arg(installedSchemaVersion())
137 .arg(schemaVersion()));
138 emit dbUpgradeInProgress(true);
139 auto upgradeResult = upgradeDb();
140 emit dbUpgradeInProgress(false);
141 if (!upgradeResult) {
142 qWarning() << qPrintable(tr("Upgrade failed..."));
145 // Add a message when migration succeeds to avoid confusing folks by implying the schema upgrade failed if
146 // later functionality does not work.
147 quInfo() << qPrintable(tr("Installed database schema successfully upgraded to version %1.").arg(schemaVersion()));
150 quInfo() << qPrintable(displayName()) << "storage backend is ready. Schema version:" << installedSchemaVersion();
154 QString AbstractSqlStorage::queryString(const QString& queryName, int version)
158 // The current schema is stored in the root folder, while upgrade queries are stored in the
159 // 'versions/##' subfolders.
161 // Use the current SQL schema, not a versioned request
162 queryInfo = QFileInfo(QString(":/SQL/%1/%2.sql").arg(displayName()).arg(queryName));
163 // If version is needed later, get it via version = schemaVersion();
166 // Use the specified schema version, not the general folder
167 queryInfo = QFileInfo(QString(":/SQL/%1/version/%2/%3.sql").arg(displayName()).arg(version).arg(queryName));
170 if (!queryInfo.exists() || !queryInfo.isFile() || !queryInfo.isReadable()) {
171 qCritical() << "Unable to read SQL-Query" << queryName << "for engine" << displayName();
175 QFile queryFile(queryInfo.filePath());
176 if (!queryFile.open(QIODevice::ReadOnly | QIODevice::Text))
178 QString query = QTextStream(&queryFile).readAll();
181 return query.trimmed();
184 QStringList AbstractSqlStorage::setupQueries()
187 // The current schema is stored in the root folder, including setup scripts.
188 QDir dir = QDir(QString(":/SQL/%1/").arg(displayName()));
189 foreach (QFileInfo fileInfo, dir.entryInfoList(QStringList() << "setup*", QDir::NoFilter, QDir::Name)) {
190 queries << queryString(fileInfo.baseName());
195 bool AbstractSqlStorage::setup(const QVariantMap& settings, const QProcessEnvironment& environment, bool loadFromEnvironment)
197 setConnectionProperties(settings, environment, loadFromEnvironment);
198 QSqlDatabase db = logDb();
200 qCritical() << "Unable to setup Logging Backend!";
205 foreach (QString queryString, setupQueries()) {
206 QSqlQuery query = db.exec(queryString);
207 if (!watchQuery(query)) {
208 qCritical() << "Unable to setup Logging Backend!";
213 bool success = setupSchemaVersion(schemaVersion());
221 QStringList AbstractSqlStorage::upgradeQueries(int version)
224 // Upgrade queries are stored in the 'version/##' subfolders.
225 QDir dir = QDir(QString(":/SQL/%1/version/%2/").arg(displayName()).arg(version));
226 foreach (QFileInfo fileInfo, dir.entryInfoList(QStringList() << "upgrade*", QDir::NoFilter, QDir::Name)) {
227 queries << queryString(fileInfo.baseName(), version);
232 bool AbstractSqlStorage::upgradeDb()
234 if (schemaVersion() <= installedSchemaVersion())
237 QSqlDatabase db = logDb();
239 // TODO: For databases that support it (e.g. almost only PostgreSQL), wrap upgrades in a
240 // transaction. This will need careful testing of potential additional space requirements and
241 // any database modifications that might not be allowed in a transaction.
243 for (int ver = installedSchemaVersion() + 1; ver <= schemaVersion(); ver++) {
244 foreach (QString queryString, upgradeQueries(ver)) {
245 QSqlQuery query = db.exec(queryString);
246 if (!watchQuery(query)) {
247 // Individual upgrade query failed, bail out
248 qCritical() << "Unable to upgrade Logging Backend! Upgrade query in schema version" << ver << "failed.";
253 // Update the schema version for each intermediate step. This ensures that any interrupted
254 // upgrades have a greater chance of resuming correctly after core restart.
256 // Almost all databases make single queries atomic (fully works or fully fails, no partial),
257 // and with many of the longest migrations being a single query, this makes upgrade
258 // interruptions much more likely to leave the database in a valid intermediate schema
260 if (!updateSchemaVersion(ver)) {
261 // Updating the schema version failed, bail out
262 qCritical() << "Unable to upgrade Logging Backend! Setting schema version" << ver << "failed.";
267 // Update the schema version for the final step. Split this out to offer more informative
268 // logging (though setting schema version really should not fail).
269 if (!updateSchemaVersion(schemaVersion())) {
270 // Updating the final schema version failed, bail out
271 qCritical() << "Unable to upgrade Logging Backend! Setting final schema version" << schemaVersion() << "failed.";
275 // If we made it here, everything seems to have worked!
279 int AbstractSqlStorage::schemaVersion()
281 // returns the newest Schema Version!
282 // not the currently used one! (though it can be the same)
283 if (_schemaVersion > 0)
284 return _schemaVersion;
288 // Schema versions are stored in the 'version/##' subfolders.
289 QDir dir = QDir(QString(":/SQL/%1/version/").arg(displayName()));
290 foreach (QFileInfo fileInfo, dir.entryInfoList()) {
291 if (!fileInfo.isDir())
294 version = fileInfo.fileName().toInt(&ok);
298 if (version > _schemaVersion)
299 _schemaVersion = version;
301 return _schemaVersion;
304 bool AbstractSqlStorage::watchQuery(QSqlQuery& query)
306 bool queryError = query.lastError().isValid();
307 if (queryError || _debug) {
309 qCritical() << "unhandled Error in QSqlQuery!";
310 qCritical() << " last Query:\n" << qPrintable(query.lastQuery());
311 qCritical() << " executed Query:\n" << qPrintable(query.executedQuery());
312 QVariantMap boundValues = query.boundValues();
313 QStringList valueStrings;
314 QVariantMap::const_iterator iter;
315 for (iter = boundValues.constBegin(); iter != boundValues.constEnd(); ++iter) {
318 if (query.driver()) {
319 // let the driver do the formatting
320 field.setType(iter.value().type());
321 if (iter.value().isNull())
324 field.setValue(iter.value());
325 value = query.driver()->formatValue(field);
328 switch (iter.value().type()) {
329 case QVariant::Invalid:
333 value = iter.value().toString();
336 value = QString("'%1'").arg(iter.value().toString());
339 valueStrings << QString("%1=%2").arg(iter.key(), value);
341 qCritical() << " bound Values:" << qPrintable(valueStrings.join(", "));
342 qCritical() << " Error Number:" << query.lastError().number();
343 qCritical() << " Error Message:" << qPrintable(query.lastError().text());
344 qCritical() << " Driver Message:" << qPrintable(query.lastError().driverText());
345 qCritical() << " DB Message:" << qPrintable(query.lastError().databaseText());
352 void AbstractSqlStorage::connectionDestroyed()
354 QMutexLocker locker(&_connectionPoolMutex);
355 _connectionPool.remove(sender()->thread());
358 // ========================================
359 // AbstractSqlStorage::Connection
360 // ========================================
361 AbstractSqlStorage::Connection::Connection(const QString& name, QObject* parent)
363 , _name(name.toLatin1())
366 AbstractSqlStorage::Connection::~Connection()
369 QSqlDatabase db = QSqlDatabase::database(name(), false);
375 QSqlDatabase::removeDatabase(name());
378 // ========================================
379 // AbstractSqlMigrator
380 // ========================================
382 void AbstractSqlMigrator::newQuery(const QString& query, QSqlDatabase db)
385 _query = new QSqlQuery(db);
386 _query->prepare(query);
389 void AbstractSqlMigrator::resetQuery()
395 bool AbstractSqlMigrator::exec()
399 return !_query->lastError().isValid();
402 QString AbstractSqlMigrator::migrationObject(MigrationObject moType)
406 return "QuasselUser";
412 return "IdentityNick";
422 return "UserSetting";
429 QVariantList AbstractSqlMigrator::boundValues()
435 int numValues = _query->boundValues().count();
436 for (int i = 0; i < numValues; i++) {
437 values << _query->boundValue(i);
442 void AbstractSqlMigrator::dumpStatus()
444 qWarning() << " executed Query:";
445 qWarning() << qPrintable(executedQuery());
446 qWarning() << " bound Values:";
447 QList<QVariant> list = boundValues();
448 for (int i = 0; i < list.size(); ++i)
449 qWarning() << i << ": " << list.at(i).toString().toLatin1().data();
450 qWarning() << " Error Number:" << lastError().number();
451 qWarning() << " Error Message:" << lastError().text();
454 // ========================================
455 // AbstractSqlMigrationReader
456 // ========================================
457 AbstractSqlMigrationReader::AbstractSqlMigrationReader()
458 : AbstractSqlMigrator()
461 bool AbstractSqlMigrationReader::migrateTo(AbstractSqlMigrationWriter* writer)
463 if (!transaction()) {
464 qWarning() << "AbstractSqlMigrationReader::migrateTo(): unable to start reader's transaction!";
467 if (!writer->transaction()) {
468 qWarning() << "AbstractSqlMigrationReader::migrateTo(): unable to start writer's transaction!";
469 rollback(); // close the reader transaction;
475 // due to the incompatibility across Migration objects we can't run this in a loop... :/
476 QuasselUserMO quasselUserMo;
477 if (!transferMo(QuasselUser, quasselUserMo))
480 IdentityMO identityMo;
481 if (!transferMo(Identity, identityMo))
484 IdentityNickMO identityNickMo;
485 if (!transferMo(IdentityNick, identityNickMo))
489 if (!transferMo(Network, networkMo))
493 if (!transferMo(Buffer, bufferMo))
497 if (!transferMo(Sender, senderMo))
501 if (!transferMo(Backlog, backlogMo))
504 IrcServerMO ircServerMo;
505 if (!transferMo(IrcServer, ircServerMo))
508 UserSettingMO userSettingMo;
509 if (!transferMo(UserSetting, userSettingMo))
512 CoreStateMO coreStateMO;
513 if (!transferMo(CoreState, coreStateMO))
516 if (!_writer->postProcess())
518 return finalizeMigration();
521 void AbstractSqlMigrationReader::abortMigration(const QString& errorMsg)
523 qWarning() << "Migration Failed!";
524 if (!errorMsg.isNull()) {
525 qWarning() << qPrintable(errorMsg);
527 if (lastError().isValid()) {
528 qWarning() << "ReaderError:";
532 if (_writer->lastError().isValid()) {
533 qWarning() << "WriterError:";
534 _writer->dumpStatus();
542 bool AbstractSqlMigrationReader::finalizeMigration()
545 _writer->resetQuery();
548 if (!_writer->commit()) {
557 bool AbstractSqlMigrationReader::transferMo(MigrationObject moType, T& mo)
560 _writer->resetQuery();
562 if (!prepareQuery(moType)) {
563 abortMigration(QString("AbstractSqlMigrationReader::migrateTo(): unable to prepare reader query of type %1!")
564 .arg(AbstractSqlMigrator::migrationObject(moType)));
567 if (!_writer->prepareQuery(moType)) {
568 abortMigration(QString("AbstractSqlMigrationReader::migrateTo(): unable to prepare writer query of type %1!")
569 .arg(AbstractSqlMigrator::migrationObject(moType)));
573 qDebug() << qPrintable(QString("Transferring %1...").arg(AbstractSqlMigrator::migrationObject(moType)));
576 file.open(stdout, QIODevice::WriteOnly);
579 if (!_writer->writeMo(mo)) {
580 abortMigration(QString("AbstractSqlMigrationReader::transferMo(): unable to transfer Migratable Object of type %1!")
581 .arg(AbstractSqlMigrator::migrationObject(moType)));
599 uint qHash(const SenderData& key)
601 return qHash(QString(key.sender + "\n" + key.realname + "\n" + key.avatarurl));
604 bool operator==(const SenderData& a, const SenderData& b)
606 return a.sender == b.sender && a.realname == b.realname && a.avatarurl == b.avatarurl;