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)
39 AbstractSqlStorage::~AbstractSqlStorage()
41 // disconnect the connections, so their deletion is no longer interessting for us
42 QHash<QThread *, Connection *>::iterator conIter;
43 for (conIter = _connectionPool.begin(); conIter != _connectionPool.end(); ++conIter) {
44 QSqlDatabase::removeDatabase(conIter.value()->name());
45 disconnect(conIter.value(), nullptr, this, nullptr);
50 QSqlDatabase AbstractSqlStorage::logDb()
52 if (!_connectionPool.contains(QThread::currentThread()))
53 addConnectionToPool();
55 QSqlDatabase db = QSqlDatabase::database(_connectionPool[QThread::currentThread()]->name(),false);
58 qWarning() << "Database connection" << displayName() << "for thread" << QThread::currentThread() << "was lost, attempting to reconnect...";
66 void AbstractSqlStorage::addConnectionToPool()
68 QMutexLocker locker(&_connectionPoolMutex);
69 // we have to recheck if the connection pool already contains a connection for
70 // this thread. Since now (after the lock) we can only tell for sure
71 if (_connectionPool.contains(QThread::currentThread()))
74 QThread *currentThread = QThread::currentThread();
76 int connectionId = _nextConnectionId++;
78 Connection *connection = new Connection(QLatin1String(QString("quassel_%1_con_%2").arg(driverName()).arg(connectionId).toLatin1()));
79 connection->moveToThread(currentThread);
80 connect(this, SIGNAL(destroyed()), connection, SLOT(deleteLater()));
81 connect(currentThread, SIGNAL(destroyed()), connection, SLOT(deleteLater()));
82 connect(connection, SIGNAL(destroyed()), this, SLOT(connectionDestroyed()));
83 _connectionPool[currentThread] = connection;
85 QSqlDatabase db = QSqlDatabase::addDatabase(driverName(), connection->name());
86 db.setDatabaseName(databaseName());
88 if (!hostName().isEmpty())
89 db.setHostName(hostName());
94 if (!userName().isEmpty()) {
95 db.setUserName(userName());
96 db.setPassword(password());
103 void AbstractSqlStorage::dbConnect(QSqlDatabase &db)
106 quWarning() << "Unable to open database" << displayName() << "for thread" << QThread::currentThread();
107 quWarning() << "-" << db.lastError().text();
110 if (!initDbSession(db)) {
111 quWarning() << "Unable to initialize database" << displayName() << "for thread" << QThread::currentThread();
118 Storage::State AbstractSqlStorage::init(const QVariantMap &settings,
119 const QProcessEnvironment &environment,
120 bool loadFromEnvironment)
122 setConnectionProperties(settings, environment, loadFromEnvironment);
124 _debug = Quassel::isOptionSet("debug");
126 QSqlDatabase db = logDb();
127 if (!db.isValid() || !db.isOpen())
130 if (installedSchemaVersion() == -1) {
131 qCritical() << "Storage Schema is missing!";
135 if (installedSchemaVersion() > schemaVersion()) {
136 qCritical() << "Installed Schema is newer then any known Version.";
140 if (installedSchemaVersion() < schemaVersion()) {
141 quInfo() << qPrintable(tr("Installed database schema (version %1) is not up to date. Upgrading to "
142 "version %2... This may take a while for major upgrades."
143 ).arg(installedSchemaVersion()).arg(schemaVersion()));
144 emit dbUpgradeInProgress(true);
145 auto upgradeResult = upgradeDb();
146 emit dbUpgradeInProgress(false);
147 if (!upgradeResult) {
148 qWarning() << qPrintable(tr("Upgrade failed..."));
151 // Add a message when migration succeeds to avoid confusing folks by implying the schema upgrade failed if
152 // later functionality does not work.
153 quInfo() << qPrintable(tr("Installed database schema successfully upgraded to version %1.").arg(schemaVersion()));
156 quInfo() << qPrintable(displayName()) << "storage backend is ready. Schema version:" << installedSchemaVersion();
161 QString AbstractSqlStorage::queryString(const QString &queryName, int version)
165 // The current schema is stored in the root folder, while upgrade queries are stored in the
166 // 'versions/##' subfolders.
168 // Use the current SQL schema, not a versioned request
169 queryInfo = QFileInfo(QString(":/SQL/%1/%2.sql").arg(displayName()).arg(queryName));
170 // If version is needed later, get it via version = schemaVersion();
172 // Use the specified schema version, not the general folder
173 queryInfo = QFileInfo(QString(":/SQL/%1/version/%2/%3.sql")
174 .arg(displayName()).arg(version).arg(queryName));
177 if (!queryInfo.exists() || !queryInfo.isFile() || !queryInfo.isReadable()) {
178 qCritical() << "Unable to read SQL-Query" << queryName << "for engine" << displayName();
182 QFile queryFile(queryInfo.filePath());
183 if (!queryFile.open(QIODevice::ReadOnly | QIODevice::Text))
185 QString query = QTextStream(&queryFile).readAll();
188 return query.trimmed();
192 QStringList AbstractSqlStorage::setupQueries()
195 // The current schema is stored in the root folder, including setup scripts.
196 QDir dir = QDir(QString(":/SQL/%1/").arg(displayName()));
197 foreach(QFileInfo fileInfo, dir.entryInfoList(QStringList() << "setup*", QDir::NoFilter, QDir::Name)) {
198 queries << queryString(fileInfo.baseName());
204 bool AbstractSqlStorage::setup(const QVariantMap &settings, const QProcessEnvironment &environment,
205 bool loadFromEnvironment)
207 setConnectionProperties(settings, environment, loadFromEnvironment);
208 QSqlDatabase db = logDb();
210 qCritical() << "Unable to setup Logging Backend!";
215 foreach(QString queryString, setupQueries()) {
216 QSqlQuery query = db.exec(queryString);
217 if (!watchQuery(query)) {
218 qCritical() << "Unable to setup Logging Backend!";
223 bool success = setupSchemaVersion(schemaVersion());
232 QStringList AbstractSqlStorage::upgradeQueries(int version)
235 // Upgrade queries are stored in the 'version/##' subfolders.
236 QDir dir = QDir(QString(":/SQL/%1/version/%2/").arg(displayName()).arg(version));
237 foreach(QFileInfo fileInfo, dir.entryInfoList(QStringList() << "upgrade*", QDir::NoFilter, QDir::Name)) {
238 queries << queryString(fileInfo.baseName(), version);
244 bool AbstractSqlStorage::upgradeDb()
246 if (schemaVersion() <= installedSchemaVersion())
249 QSqlDatabase db = logDb();
251 // TODO: For databases that support it (e.g. almost only PostgreSQL), wrap upgrades in a
252 // transaction. This will need careful testing of potential additional space requirements and
253 // any database modifications that might not be allowed in a transaction.
255 for (int ver = installedSchemaVersion() + 1; ver <= schemaVersion(); ver++) {
256 foreach(QString queryString, upgradeQueries(ver)) {
257 QSqlQuery query = db.exec(queryString);
258 if (!watchQuery(query)) {
259 // Individual upgrade query failed, bail out
260 qCritical() << "Unable to upgrade Logging Backend! Upgrade query in schema version"
266 // Update the schema version for each intermediate step. This ensures that any interrupted
267 // upgrades have a greater chance of resuming correctly after core restart.
269 // Almost all databases make single queries atomic (fully works or fully fails, no partial),
270 // and with many of the longest migrations being a single query, this makes upgrade
271 // interruptions much more likely to leave the database in a valid intermediate schema
273 if (!updateSchemaVersion(ver)) {
274 // Updating the schema version failed, bail out
275 qCritical() << "Unable to upgrade Logging Backend! Setting schema version"
281 // Update the schema version for the final step. Split this out to offer more informative
282 // logging (though setting schema version really should not fail).
283 if (!updateSchemaVersion(schemaVersion())) {
284 // Updating the final schema version failed, bail out
285 qCritical() << "Unable to upgrade Logging Backend! Setting final schema version"
286 << schemaVersion() << "failed.";
290 // If we made it here, everything seems to have worked!
295 int AbstractSqlStorage::schemaVersion()
297 // returns the newest Schema Version!
298 // not the currently used one! (though it can be the same)
299 if (_schemaVersion > 0)
300 return _schemaVersion;
304 // Schema versions are stored in the 'version/##' subfolders.
305 QDir dir = QDir(QString(":/SQL/%1/version/").arg(displayName()));
306 foreach(QFileInfo fileInfo, dir.entryInfoList()) {
307 if (!fileInfo.isDir())
310 version = fileInfo.fileName().toInt(&ok);
314 if (version > _schemaVersion)
315 _schemaVersion = version;
317 return _schemaVersion;
321 bool AbstractSqlStorage::watchQuery(QSqlQuery &query)
323 bool queryError = query.lastError().isValid();
324 if (queryError || _debug) {
326 qCritical() << "unhandled Error in QSqlQuery!";
327 qCritical() << " last Query:\n" << qPrintable(query.lastQuery());
328 qCritical() << " executed Query:\n" << qPrintable(query.executedQuery());
329 QVariantMap boundValues = query.boundValues();
330 QStringList valueStrings;
331 QVariantMap::const_iterator iter;
332 for (iter = boundValues.constBegin(); iter != boundValues.constEnd(); ++iter) {
335 if (query.driver()) {
336 // let the driver do the formatting
337 field.setType(iter.value().type());
338 if (iter.value().isNull())
341 field.setValue(iter.value());
342 value = query.driver()->formatValue(field);
345 switch (iter.value().type()) {
346 case QVariant::Invalid:
350 value = iter.value().toString();
353 value = QString("'%1'").arg(iter.value().toString());
356 valueStrings << QString("%1=%2").arg(iter.key(), value);
358 qCritical() << " bound Values:" << qPrintable(valueStrings.join(", "));
359 qCritical() << " Error Number:" << query.lastError().number();
360 qCritical() << " Error Message:" << qPrintable(query.lastError().text());
361 qCritical() << " Driver Message:" << qPrintable(query.lastError().driverText());
362 qCritical() << " DB Message:" << qPrintable(query.lastError().databaseText());
370 void AbstractSqlStorage::connectionDestroyed()
372 QMutexLocker locker(&_connectionPoolMutex);
373 _connectionPool.remove(sender()->thread());
377 // ========================================
378 // AbstractSqlStorage::Connection
379 // ========================================
380 AbstractSqlStorage::Connection::Connection(const QString &name, QObject *parent)
382 _name(name.toLatin1())
387 AbstractSqlStorage::Connection::~Connection()
390 QSqlDatabase db = QSqlDatabase::database(name(), false);
396 QSqlDatabase::removeDatabase(name());
400 // ========================================
401 // AbstractSqlMigrator
402 // ========================================
403 AbstractSqlMigrator::AbstractSqlMigrator()
408 void AbstractSqlMigrator::newQuery(const QString &query, QSqlDatabase db)
411 _query = new QSqlQuery(db);
412 _query->prepare(query);
416 void AbstractSqlMigrator::resetQuery()
423 bool AbstractSqlMigrator::exec()
427 return !_query->lastError().isValid();
431 QString AbstractSqlMigrator::migrationObject(MigrationObject moType)
435 return "QuasselUser";
441 return "IdentityNick";
451 return "UserSetting";
459 QVariantList AbstractSqlMigrator::boundValues()
465 int numValues = _query->boundValues().count();
466 for (int i = 0; i < numValues; i++) {
467 values << _query->boundValue(i);
473 void AbstractSqlMigrator::dumpStatus()
475 qWarning() << " executed Query:";
476 qWarning() << qPrintable(executedQuery());
477 qWarning() << " bound Values:";
478 QList<QVariant> list = boundValues();
479 for (int i = 0; i < list.size(); ++i)
480 qWarning() << i << ": " << list.at(i).toString().toLatin1().data();
481 qWarning() << " Error Number:" << lastError().number();
482 qWarning() << " Error Message:" << lastError().text();
486 // ========================================
487 // AbstractSqlMigrationReader
488 // ========================================
489 AbstractSqlMigrationReader::AbstractSqlMigrationReader()
490 : AbstractSqlMigrator()
495 bool AbstractSqlMigrationReader::migrateTo(AbstractSqlMigrationWriter *writer)
497 if (!transaction()) {
498 qWarning() << "AbstractSqlMigrationReader::migrateTo(): unable to start reader's transaction!";
501 if (!writer->transaction()) {
502 qWarning() << "AbstractSqlMigrationReader::migrateTo(): unable to start writer's transaction!";
503 rollback(); // close the reader transaction;
509 // due to the incompatibility across Migration objects we can't run this in a loop... :/
510 QuasselUserMO quasselUserMo;
511 if (!transferMo(QuasselUser, quasselUserMo))
514 IdentityMO identityMo;
515 if (!transferMo(Identity, identityMo))
518 IdentityNickMO identityNickMo;
519 if (!transferMo(IdentityNick, identityNickMo))
523 if (!transferMo(Network, networkMo))
527 if (!transferMo(Buffer, bufferMo))
531 if (!transferMo(Sender, senderMo))
535 if (!transferMo(Backlog, backlogMo))
538 IrcServerMO ircServerMo;
539 if (!transferMo(IrcServer, ircServerMo))
542 UserSettingMO userSettingMo;
543 if (!transferMo(UserSetting, userSettingMo))
546 CoreStateMO coreStateMO;
547 if (!transferMo(CoreState, coreStateMO))
550 if (!_writer->postProcess())
552 return finalizeMigration();
556 void AbstractSqlMigrationReader::abortMigration(const QString &errorMsg)
558 qWarning() << "Migration Failed!";
559 if (!errorMsg.isNull()) {
560 qWarning() << qPrintable(errorMsg);
562 if (lastError().isValid()) {
563 qWarning() << "ReaderError:";
567 if (_writer->lastError().isValid()) {
568 qWarning() << "WriterError:";
569 _writer->dumpStatus();
578 bool AbstractSqlMigrationReader::finalizeMigration()
581 _writer->resetQuery();
584 if (!_writer->commit()) {
594 bool AbstractSqlMigrationReader::transferMo(MigrationObject moType, T &mo)
597 _writer->resetQuery();
599 if (!prepareQuery(moType)) {
600 abortMigration(QString("AbstractSqlMigrationReader::migrateTo(): unable to prepare reader query of type %1!").arg(AbstractSqlMigrator::migrationObject(moType)));
603 if (!_writer->prepareQuery(moType)) {
604 abortMigration(QString("AbstractSqlMigrationReader::migrateTo(): unable to prepare writer query of type %1!").arg(AbstractSqlMigrator::migrationObject(moType)));
608 qDebug() << qPrintable(QString("Transferring %1...").arg(AbstractSqlMigrator::migrationObject(moType)));
611 file.open(stdout, QIODevice::WriteOnly);
614 if (!_writer->writeMo(mo)) {
615 abortMigration(QString("AbstractSqlMigrationReader::transferMo(): unable to transfer Migratable Object of type %1!").arg(AbstractSqlMigrator::migrationObject(moType)));
633 uint qHash(const SenderData &key) {
634 return qHash(QString(key.sender + "\n" + key.realname + "\n" + key.avatarurl));
637 bool operator==(const SenderData &a, const SenderData &b) {
638 return a.sender == b.sender &&
639 a.realname == b.realname &&
640 a.avatarurl == b.avatarurl;