c00ab62c90b457f0733b12f7490b01a9e4fa8ac8
[quassel.git] / src / core / abstractsqlstorage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "abstractsqlstorage.h"
22
23 #include <QMutexLocker>
24 #include <QSqlDriver>
25 #include <QSqlError>
26 #include <QSqlField>
27 #include <QSqlQuery>
28
29 #include "logmessage.h"
30 #include "quassel.h"
31
32 int AbstractSqlStorage::_nextConnectionId = 0;
33 AbstractSqlStorage::AbstractSqlStorage(QObject *parent)
34     : Storage(parent),
35     _schemaVersion(0)
36 {
37 }
38
39
40 AbstractSqlStorage::~AbstractSqlStorage()
41 {
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);
47     }
48 }
49
50
51 QSqlDatabase AbstractSqlStorage::logDb()
52 {
53     if (!_connectionPool.contains(QThread::currentThread()))
54         addConnectionToPool();
55
56     QSqlDatabase db = QSqlDatabase::database(_connectionPool[QThread::currentThread()]->name(),false);
57
58     if (!db.isOpen()) {
59         qWarning() << "Database connection" << displayName() << "for thread" << QThread::currentThread() << "was lost, attempting to reconnect...";
60         dbConnect(db);
61     }
62
63     return db;
64 }
65
66
67 void AbstractSqlStorage::addConnectionToPool()
68 {
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()))
73         return;
74
75     QThread *currentThread = QThread::currentThread();
76
77     int connectionId = _nextConnectionId++;
78
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;
85
86     QSqlDatabase db = QSqlDatabase::addDatabase(driverName(), connection->name());
87     db.setDatabaseName(databaseName());
88
89     if (!hostName().isEmpty())
90         db.setHostName(hostName());
91
92     if (port() != -1)
93         db.setPort(port());
94
95     if (!userName().isEmpty()) {
96         db.setUserName(userName());
97         db.setPassword(password());
98     }
99
100     dbConnect(db);
101 }
102
103
104 void AbstractSqlStorage::dbConnect(QSqlDatabase &db)
105 {
106     if (!db.open()) {
107         quWarning() << "Unable to open database" << displayName() << "for thread" << QThread::currentThread();
108         quWarning() << "-" << db.lastError().text();
109     }
110     else {
111         if (!initDbSession(db)) {
112             quWarning() << "Unable to initialize database" << displayName() << "for thread" << QThread::currentThread();
113             db.close();
114         }
115     }
116 }
117
118
119 Storage::State AbstractSqlStorage::init(const QVariantMap &settings,
120                                         const QProcessEnvironment &environment,
121                                         bool loadFromEnvironment)
122 {
123     setConnectionProperties(settings, environment, loadFromEnvironment);
124
125     _debug = Quassel::isOptionSet("debug");
126
127     QSqlDatabase db = logDb();
128     if (!db.isValid() || !db.isOpen())
129         return NotAvailable;
130
131     if (installedSchemaVersion() == -1) {
132         qCritical() << "Storage Schema is missing!";
133         return NeedsSetup;
134     }
135
136     if (installedSchemaVersion() > schemaVersion()) {
137         qCritical() << "Installed Schema is newer then any known Version.";
138         return NotAvailable;
139     }
140
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..."));
150             return NotAvailable;
151         }
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()));
155     }
156
157     quInfo() << qPrintable(displayName()) << "storage backend is ready. Schema version:" << installedSchemaVersion();
158     return IsReady;
159 }
160
161
162 QString AbstractSqlStorage::queryString(const QString &queryName, int version)
163 {
164     QFileInfo queryInfo;
165
166     // The current schema is stored in the root folder, while upgrade queries are stored in the
167     // 'versions/##' subfolders.
168     if (version == 0) {
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();
172     } else {
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));
176     }
177
178     if (!queryInfo.exists() || !queryInfo.isFile() || !queryInfo.isReadable()) {
179         qCritical() << "Unable to read SQL-Query" << queryName << "for engine" << displayName();
180         return QString();
181     }
182
183     QFile queryFile(queryInfo.filePath());
184     if (!queryFile.open(QIODevice::ReadOnly | QIODevice::Text))
185         return QString();
186     QString query = QTextStream(&queryFile).readAll();
187     queryFile.close();
188
189     return query.trimmed();
190 }
191
192
193 QList<AbstractSqlStorage::SqlQueryResource> AbstractSqlStorage::setupQueries()
194 {
195     QList<SqlQueryResource> queries;
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 << SqlQueryResource(queryString(fileInfo.baseName()), fileInfo.baseName());
200     }
201     return queries;
202 }
203
204
205 bool AbstractSqlStorage::setup(const QVariantMap &settings, const QProcessEnvironment &environment,
206                                bool loadFromEnvironment)
207 {
208     setConnectionProperties(settings, environment, loadFromEnvironment);
209     QSqlDatabase db = logDb();
210     if (!db.isOpen()) {
211         qCritical() << "Unable to setup Logging Backend!";
212         return false;
213     }
214
215     db.transaction();
216     foreach (auto queryResource, setupQueries()) {
217         QSqlQuery query = db.exec(queryResource.queryString);
218         if (!watchQuery(query)) {
219             qCritical() << qPrintable(QString("Unable to setup Logging Backend!  Setup query failed (step: %1).")
220                                       .arg(queryResource.queryFilename));
221             db.rollback();
222             return false;
223         }
224     }
225     bool success = setupSchemaVersion(schemaVersion());
226     if (success)
227         db.commit();
228     else
229         db.rollback();
230     return success;
231 }
232
233
234 QList<AbstractSqlStorage::SqlQueryResource> AbstractSqlStorage::upgradeQueries(int version)
235 {
236     QList<SqlQueryResource> queries;
237     // Upgrade queries are stored in the 'version/##' subfolders.
238     QDir dir = QDir(QString(":/SQL/%1/version/%2/").arg(displayName()).arg(version));
239     foreach(QFileInfo fileInfo, dir.entryInfoList(QStringList() << "upgrade*", QDir::NoFilter, QDir::Name)) {
240         queries << SqlQueryResource(queryString(fileInfo.baseName(), version), fileInfo.baseName());
241     }
242     return queries;
243 }
244
245
246 bool AbstractSqlStorage::upgradeDb()
247 {
248     if (schemaVersion() <= installedSchemaVersion())
249         return true;
250
251     QSqlDatabase db = logDb();
252
253     // TODO: For databases that support it (e.g. almost only PostgreSQL), wrap upgrades in a
254     // transaction.  This will need careful testing of potential additional space requirements and
255     // any database modifications that might not be allowed in a transaction.
256
257     // Check if we're resuming an interrupted multi-step upgrade: is an upgrade step stored?
258     const QString previousLaunchUpgradeStep = schemaVersionUpgradeStep();
259     bool resumingUpgrade = !previousLaunchUpgradeStep.isEmpty();
260
261     for (int ver = installedSchemaVersion() + 1; ver <= schemaVersion(); ver++) {
262         foreach (auto queryResource, upgradeQueries(ver)) {
263             if (resumingUpgrade) {
264                 // An upgrade was interrupted.  Check if this matches the the last successful query.
265                 if (previousLaunchUpgradeStep == queryResource.queryFilename) {
266                     // Found the matching query!
267                     quInfo() << qPrintable(QString("Resuming interrupted upgrade for schema version %1 (last step: %2)")
268                                            .arg(QString::number(ver), previousLaunchUpgradeStep));
269
270                     // Stop searching for queries
271                     resumingUpgrade = false;
272                     // Continue past the previous query with the next not-yet-tried query
273                     continue;
274                 }
275                 else {
276                     // Not yet matched, keep looking
277                     continue;
278                 }
279             }
280
281             // Run the upgrade query
282             QSqlQuery query = db.exec(queryResource.queryString);
283             if (!watchQuery(query)) {
284                 // Individual upgrade query failed, bail out
285                 qCritical() << qPrintable(QString("Unable to upgrade Logging Backend!  Upgrade query in schema version %1 failed (step: %2).")
286                                           .arg(QString::number(ver), queryResource.queryFilename));
287                 return false;
288             }
289             else {
290                 // Mark as successful
291                 setSchemaVersionUpgradeStep(queryResource.queryFilename);
292             }
293         }
294
295         if (resumingUpgrade) {
296             // Something went wrong and the last successful SQL query to resume from couldn't be
297             // found.
298             // 1.  The storage of successful query glitched, or the database was manually changed
299             // 2.  Quassel changed the filenames of upgrade queries, and the local Quassel core
300             //     version was replaced during an interrupted schema upgrade
301             //
302             // Both are unlikely, but it's a good idea to handle it anyways.
303
304             qCritical() << qPrintable(QString("Unable to resume interrupted upgrade in Logging "
305                                               "Backend!  Missing upgrade step in schema version %1 "
306                                               "(expected step: %2)")
307                                       .arg(QString::number(ver), previousLaunchUpgradeStep));
308             return false;
309         }
310
311         // Update the schema version for each intermediate step and mark the step as done.  This
312         // ensures that any interrupted upgrades have a greater chance of resuming correctly after
313         // core restart.
314         //
315         // Almost all databases make single queries atomic (fully works or fully fails, no partial),
316         // and with many of the longest migrations being a single query, this makes upgrade
317         // interruptions much more likely to leave the database in a valid intermediate schema
318         // version.
319         if (!updateSchemaVersion(ver, true)) {
320             // Updating the schema version failed, bail out
321             qCritical() << "Unable to upgrade Logging Backend!  Setting schema version"
322                         << ver << "failed.";
323             return false;
324         }
325     }
326
327     // Update the schema version for the final step.  Split this out to offer more informative
328     // logging (though setting schema version really should not fail).
329     if (!updateSchemaVersion(schemaVersion())) {
330         // Updating the final schema version failed, bail out
331         qCritical() << "Unable to upgrade Logging Backend!  Setting final schema version"
332                     << schemaVersion() << "failed.";
333         return false;
334     }
335
336     // If we made it here, everything seems to have worked!
337     return true;
338 }
339
340
341 int AbstractSqlStorage::schemaVersion()
342 {
343     // returns the newest Schema Version!
344     // not the currently used one! (though it can be the same)
345     if (_schemaVersion > 0)
346         return _schemaVersion;
347
348     int version;
349     bool ok;
350     // Schema versions are stored in the 'version/##' subfolders.
351     QDir dir = QDir(QString(":/SQL/%1/version/").arg(displayName()));
352     foreach(QFileInfo fileInfo, dir.entryInfoList()) {
353         if (!fileInfo.isDir())
354             continue;
355
356         version = fileInfo.fileName().toInt(&ok);
357         if (!ok)
358             continue;
359
360         if (version > _schemaVersion)
361             _schemaVersion = version;
362     }
363     return _schemaVersion;
364 }
365
366
367 QString AbstractSqlStorage::schemaVersionUpgradeStep()
368 {
369     // By default, assume there's no pending upgrade
370     return {};
371 }
372
373
374 bool AbstractSqlStorage::watchQuery(QSqlQuery &query)
375 {
376     bool queryError = query.lastError().isValid();
377     if (queryError || _debug) {
378         if (queryError)
379             qCritical() << "unhandled Error in QSqlQuery!";
380         qCritical() << "                  last Query:\n" << qPrintable(query.lastQuery());
381         qCritical() << "              executed Query:\n" << qPrintable(query.executedQuery());
382         QVariantMap boundValues = query.boundValues();
383         QStringList valueStrings;
384         QVariantMap::const_iterator iter;
385         for (iter = boundValues.constBegin(); iter != boundValues.constEnd(); ++iter) {
386             QString value;
387             QSqlField field;
388             if (query.driver()) {
389                 // let the driver do the formatting
390                 field.setType(iter.value().type());
391                 if (iter.value().isNull())
392                     field.clear();
393                 else
394                     field.setValue(iter.value());
395                 value =  query.driver()->formatValue(field);
396             }
397             else {
398                 switch (iter.value().type()) {
399                 case QVariant::Invalid:
400                     value = "NULL";
401                     break;
402                 case QVariant::Int:
403                     value = iter.value().toString();
404                     break;
405                 default:
406                     value = QString("'%1'").arg(iter.value().toString());
407                 }
408             }
409             valueStrings << QString("%1=%2").arg(iter.key(), value);
410         }
411         qCritical() << "                bound Values:" << qPrintable(valueStrings.join(", "));
412         qCritical() << "                Error Number:" << query.lastError().number();
413         qCritical() << "               Error Message:" << qPrintable(query.lastError().text());
414         qCritical() << "              Driver Message:" << qPrintable(query.lastError().driverText());
415         qCritical() << "                  DB Message:" << qPrintable(query.lastError().databaseText());
416
417         return !queryError;
418     }
419     return true;
420 }
421
422
423 void AbstractSqlStorage::connectionDestroyed()
424 {
425     QMutexLocker locker(&_connectionPoolMutex);
426     _connectionPool.remove(sender()->thread());
427 }
428
429
430 // ========================================
431 //  AbstractSqlStorage::Connection
432 // ========================================
433 AbstractSqlStorage::Connection::Connection(const QString &name, QObject *parent)
434     : QObject(parent),
435     _name(name.toLatin1())
436 {
437 }
438
439
440 AbstractSqlStorage::Connection::~Connection()
441 {
442     {
443         QSqlDatabase db = QSqlDatabase::database(name(), false);
444         if (db.isOpen()) {
445             db.commit();
446             db.close();
447         }
448     }
449     QSqlDatabase::removeDatabase(name());
450 }
451
452
453 // ========================================
454 //  AbstractSqlMigrator
455 // ========================================
456 AbstractSqlMigrator::AbstractSqlMigrator()
457     : _query(0)
458 {
459 }
460
461
462 void AbstractSqlMigrator::newQuery(const QString &query, QSqlDatabase db)
463 {
464     Q_ASSERT(!_query);
465     _query = new QSqlQuery(db);
466     _query->prepare(query);
467 }
468
469
470 void AbstractSqlMigrator::resetQuery()
471 {
472     delete _query;
473     _query = 0;
474 }
475
476
477 bool AbstractSqlMigrator::exec()
478 {
479     Q_ASSERT(_query);
480     _query->exec();
481     return !_query->lastError().isValid();
482 }
483
484
485 QString AbstractSqlMigrator::migrationObject(MigrationObject moType)
486 {
487     switch (moType) {
488     case QuasselUser:
489         return "QuasselUser";
490     case Sender:
491         return "Sender";
492     case Identity:
493         return "Identity";
494     case IdentityNick:
495         return "IdentityNick";
496     case Network:
497         return "Network";
498     case Buffer:
499         return "Buffer";
500     case Backlog:
501         return "Backlog";
502     case IrcServer:
503         return "IrcServer";
504     case UserSetting:
505         return "UserSetting";
506     case CoreState:
507         return "CoreState";
508     };
509     return QString();
510 }
511
512
513 QVariantList AbstractSqlMigrator::boundValues()
514 {
515     QVariantList values;
516     if (!_query)
517         return values;
518
519     int numValues = _query->boundValues().count();
520     for (int i = 0; i < numValues; i++) {
521         values << _query->boundValue(i);
522     }
523     return values;
524 }
525
526
527 void AbstractSqlMigrator::dumpStatus()
528 {
529     qWarning() << "  executed Query:";
530     qWarning() << qPrintable(executedQuery());
531     qWarning() << "  bound Values:";
532     QList<QVariant> list = boundValues();
533     for (int i = 0; i < list.size(); ++i)
534         qWarning() << i << ": " << list.at(i).toString().toLatin1().data();
535     qWarning() << "  Error Number:"   << lastError().number();
536     qWarning() << "  Error Message:"   << lastError().text();
537 }
538
539
540 // ========================================
541 //  AbstractSqlMigrationReader
542 // ========================================
543 AbstractSqlMigrationReader::AbstractSqlMigrationReader()
544     : AbstractSqlMigrator(),
545     _writer(0)
546 {
547 }
548
549
550 bool AbstractSqlMigrationReader::migrateTo(AbstractSqlMigrationWriter *writer)
551 {
552     if (!transaction()) {
553         qWarning() << "AbstractSqlMigrationReader::migrateTo(): unable to start reader's transaction!";
554         return false;
555     }
556     if (!writer->transaction()) {
557         qWarning() << "AbstractSqlMigrationReader::migrateTo(): unable to start writer's transaction!";
558         rollback(); // close the reader transaction;
559         return false;
560     }
561
562     _writer = writer;
563
564     // due to the incompatibility across Migration objects we can't run this in a loop... :/
565     QuasselUserMO quasselUserMo;
566     if (!transferMo(QuasselUser, quasselUserMo))
567         return false;
568
569     IdentityMO identityMo;
570     if (!transferMo(Identity, identityMo))
571         return false;
572
573     IdentityNickMO identityNickMo;
574     if (!transferMo(IdentityNick, identityNickMo))
575         return false;
576
577     NetworkMO networkMo;
578     if (!transferMo(Network, networkMo))
579         return false;
580
581     BufferMO bufferMo;
582     if (!transferMo(Buffer, bufferMo))
583         return false;
584
585     SenderMO senderMo;
586     if (!transferMo(Sender, senderMo))
587         return false;
588
589     BacklogMO backlogMo;
590     if (!transferMo(Backlog, backlogMo))
591         return false;
592
593     IrcServerMO ircServerMo;
594     if (!transferMo(IrcServer, ircServerMo))
595         return false;
596
597     UserSettingMO userSettingMo;
598     if (!transferMo(UserSetting, userSettingMo))
599         return false;
600
601     CoreStateMO coreStateMO;
602     if (!transferMo(CoreState, coreStateMO))
603         return false;
604
605     if (!_writer->postProcess())
606         abortMigration();
607     return finalizeMigration();
608 }
609
610
611 void AbstractSqlMigrationReader::abortMigration(const QString &errorMsg)
612 {
613     qWarning() << "Migration Failed!";
614     if (!errorMsg.isNull()) {
615         qWarning() << qPrintable(errorMsg);
616     }
617     if (lastError().isValid()) {
618         qWarning() << "ReaderError:";
619         dumpStatus();
620     }
621
622     if (_writer->lastError().isValid()) {
623         qWarning() << "WriterError:";
624         _writer->dumpStatus();
625     }
626
627     rollback();
628     _writer->rollback();
629     _writer = 0;
630 }
631
632
633 bool AbstractSqlMigrationReader::finalizeMigration()
634 {
635     resetQuery();
636     _writer->resetQuery();
637
638     commit();
639     if (!_writer->commit()) {
640         _writer = 0;
641         return false;
642     }
643     _writer = 0;
644     return true;
645 }
646
647
648 template<typename T>
649 bool AbstractSqlMigrationReader::transferMo(MigrationObject moType, T &mo)
650 {
651     resetQuery();
652     _writer->resetQuery();
653
654     if (!prepareQuery(moType)) {
655         abortMigration(QString("AbstractSqlMigrationReader::migrateTo(): unable to prepare reader query of type %1!").arg(AbstractSqlMigrator::migrationObject(moType)));
656         return false;
657     }
658     if (!_writer->prepareQuery(moType)) {
659         abortMigration(QString("AbstractSqlMigrationReader::migrateTo(): unable to prepare writer query of type %1!").arg(AbstractSqlMigrator::migrationObject(moType)));
660         return false;
661     }
662
663     qDebug() << qPrintable(QString("Transferring %1...").arg(AbstractSqlMigrator::migrationObject(moType)));
664     int i = 0;
665     QFile file;
666     file.open(stdout, QIODevice::WriteOnly);
667
668     while (readMo(mo)) {
669         if (!_writer->writeMo(mo)) {
670             abortMigration(QString("AbstractSqlMigrationReader::transferMo(): unable to transfer Migratable Object of type %1!").arg(AbstractSqlMigrator::migrationObject(moType)));
671             return false;
672         }
673         i++;
674         if (i % 1000 == 0) {
675             file.write("*");
676             file.flush();
677         }
678     }
679     if (i > 1000) {
680         file.write("\n");
681         file.flush();
682     }
683
684     qDebug() << "Done.";
685     return true;
686 }
687
688 uint qHash(const SenderData &key) {
689     return qHash(QString(key.sender + "\n" + key.realname + "\n" + key.avatarurl));
690 }
691
692 bool operator==(const SenderData &a, const SenderData &b) {
693     return a.sender == b.sender &&
694         a.realname == b.realname &&
695         a.avatarurl == b.avatarurl;
696 }