+void AbstractSqlMigrator::dumpStatus()
+{
+ qWarning() << " executed Query:";
+ qWarning() << qPrintable(executedQuery());
+ qWarning() << " bound Values:";
+ QList<QVariant> list = boundValues();
+ for (int i = 0; i < list.size(); ++i)
+ qWarning() << i << ": " << list.at(i).toString().toLatin1().data();
+ qWarning() << " Error Number:" << lastError().number();
+ qWarning() << " Error Message:" << lastError().text();
+}
+
+
+// ========================================
+// AbstractSqlMigrationReader
+// ========================================
+AbstractSqlMigrationReader::AbstractSqlMigrationReader()
+ : AbstractSqlMigrator()
+{
+}
+
+
+bool AbstractSqlMigrationReader::migrateTo(AbstractSqlMigrationWriter *writer)
+{
+ if (!transaction()) {
+ qWarning() << "AbstractSqlMigrationReader::migrateTo(): unable to start reader's transaction!";
+ return false;
+ }
+ if (!writer->transaction()) {
+ qWarning() << "AbstractSqlMigrationReader::migrateTo(): unable to start writer's transaction!";
+ rollback(); // close the reader transaction;
+ return false;
+ }
+
+ _writer = writer;
+
+ // due to the incompatibility across Migration objects we can't run this in a loop... :/
+ QuasselUserMO quasselUserMo;
+ if (!transferMo(QuasselUser, quasselUserMo))
+ return false;
+
+ IdentityMO identityMo;
+ if (!transferMo(Identity, identityMo))
+ return false;
+
+ IdentityNickMO identityNickMo;
+ if (!transferMo(IdentityNick, identityNickMo))
+ return false;
+
+ NetworkMO networkMo;
+ if (!transferMo(Network, networkMo))
+ return false;
+
+ BufferMO bufferMo;
+ if (!transferMo(Buffer, bufferMo))
+ return false;
+
+ SenderMO senderMo;
+ if (!transferMo(Sender, senderMo))
+ return false;
+
+ BacklogMO backlogMo;
+ if (!transferMo(Backlog, backlogMo))
+ return false;
+
+ IrcServerMO ircServerMo;
+ if (!transferMo(IrcServer, ircServerMo))
+ return false;
+
+ UserSettingMO userSettingMo;
+ if (!transferMo(UserSetting, userSettingMo))
+ return false;
+
+ CoreStateMO coreStateMO;
+ if (!transferMo(CoreState, coreStateMO))
+ return false;
+
+ if (!_writer->postProcess())
+ abortMigration();
+ return finalizeMigration();
+}
+
+
+void AbstractSqlMigrationReader::abortMigration(const QString &errorMsg)
+{
+ qWarning() << "Migration Failed!";
+ if (!errorMsg.isNull()) {
+ qWarning() << qPrintable(errorMsg);
+ }
+ if (lastError().isValid()) {
+ qWarning() << "ReaderError:";
+ dumpStatus();
+ }
+
+ if (_writer->lastError().isValid()) {
+ qWarning() << "WriterError:";
+ _writer->dumpStatus();
+ }
+
+ rollback();
+ _writer->rollback();
+ _writer = nullptr;
+}
+
+
+bool AbstractSqlMigrationReader::finalizeMigration()
+{
+ resetQuery();
+ _writer->resetQuery();
+
+ commit();
+ if (!_writer->commit()) {
+ _writer = nullptr;
+ return false;
+ }
+ _writer = nullptr;
+ return true;
+}
+
+
+template<typename T>
+bool AbstractSqlMigrationReader::transferMo(MigrationObject moType, T &mo)
+{
+ resetQuery();
+ _writer->resetQuery();
+
+ if (!prepareQuery(moType)) {
+ abortMigration(QString("AbstractSqlMigrationReader::migrateTo(): unable to prepare reader query of type %1!").arg(AbstractSqlMigrator::migrationObject(moType)));
+ return false;
+ }
+ if (!_writer->prepareQuery(moType)) {
+ abortMigration(QString("AbstractSqlMigrationReader::migrateTo(): unable to prepare writer query of type %1!").arg(AbstractSqlMigrator::migrationObject(moType)));
+ return false;
+ }
+
+ qDebug() << qPrintable(QString("Transferring %1...").arg(AbstractSqlMigrator::migrationObject(moType)));
+ int i = 0;
+ QFile file;
+ file.open(stdout, QIODevice::WriteOnly);
+
+ while (readMo(mo)) {
+ if (!_writer->writeMo(mo)) {
+ abortMigration(QString("AbstractSqlMigrationReader::transferMo(): unable to transfer Migratable Object of type %1!").arg(AbstractSqlMigrator::migrationObject(moType)));
+ return false;
+ }
+ i++;
+ if (i % 1000 == 0) {
+ file.write("*");
+ file.flush();
+ }
+ }
+ if (i > 1000) {
+ file.write("\n");
+ file.flush();
+ }
+
+ qDebug() << "Done.";
+ return true;
+}
+
+uint qHash(const SenderData &key) {
+ return qHash(QString(key.sender + "\n" + key.realname + "\n" + key.avatarurl));
+}