first version of backend migration interface
[quassel.git] / src / core / abstractsqlstorage.cpp
index 467b226..3d339ec 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-07 by the Quassel IRC Team                         *
+ *   Copyright (C) 2005-07 by the Quassel Project                          *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -45,7 +45,6 @@ QSqlDatabase AbstractSqlStorage::logDb() {
   if(!_connectionPool.contains(QThread::currentThread()))
     addConnectionToPool();
 
-  qDebug() << "using logDb" << _connectionPool[QThread::currentThread()]->name() << QThread::currentThread();
   return QSqlDatabase::database(_connectionPool[QThread::currentThread()]->name());
 }
 
@@ -60,10 +59,8 @@ void AbstractSqlStorage::addConnectionToPool() {
 
   int connectionId = _nextConnectionId++;
 
-  Connection *connection = new Connection(QLatin1String(QString("quassel_connection_%1").arg(connectionId).toLatin1()), this);
-  qDebug() << "new connection" << connection->name() << currentThread << QLatin1String(QString("quassel_connection_%1").arg(connectionId).toLatin1());
+  Connection *connection = new Connection(QLatin1String(QString("quassel_%1_con_%2").arg(driverName()).arg(connectionId).toLatin1()));
   connection->moveToThread(currentThread);
-  connect(this, SIGNAL(syncCachedQueries()), connection, SLOT(syncCachedQueries()));
   connect(this, SIGNAL(destroyed()), connection, SLOT(deleteLater()));
   connect(currentThread, SIGNAL(destroyed()), connection, SLOT(deleteLater()));
   connect(connection, SIGNAL(destroyed()), this, SLOT(connectionDestroyed()));
@@ -75,6 +72,9 @@ void AbstractSqlStorage::addConnectionToPool() {
   if(!hostName().isEmpty())
     db.setHostName(hostName());
 
+  if(port() != -1)
+    db.setPort(port());
+
   if(!userName().isEmpty()) {
     db.setUserName(userName());
     db.setPassword(password());
@@ -86,40 +86,39 @@ void AbstractSqlStorage::addConnectionToPool() {
   }
 }
 
-bool AbstractSqlStorage::init(const QVariantMap &settings) {
-  Q_UNUSED(settings)
+Storage::State AbstractSqlStorage::init(const QVariantMap &settings) {
+  setConnectionProperties(settings);
+
   QSqlDatabase db = logDb();
   if(!db.isValid() || !db.isOpen())
-    return false;
+    return NotAvailable;
 
   if(installedSchemaVersion() == -1) {
     qCritical() << "Storage Schema is missing!";
-    return false;
+    return NeedsSetup;
   }
 
   if(installedSchemaVersion() > schemaVersion()) {
     qCritical() << "Installed Schema is newer then any known Version.";
-    return false;
+    return NotAvailable;
   }
-  
+
   if(installedSchemaVersion() < schemaVersion()) {
-    qWarning() << "Installed Schema is not up to date. Upgrading...";
-    if(!upgradeDb())
-      return false;
+    qWarning() << qPrintable(tr("Installed Schema (version %1) is not up to date. Upgrading to version %2...").arg(installedSchemaVersion()).arg(schemaVersion()));
+    if(!upgradeDb()) {
+      qWarning() << qPrintable(tr("Upgrade failed..."));
+      return NotAvailable;
+    }
   }
-  
-  quInfo() << "Storage Backend is ready. Quassel Schema Version:" << installedSchemaVersion();
-  return true;
-}
 
-void AbstractSqlStorage::sync() {
-  emit syncCachedQueries();
+  quInfo() << qPrintable(displayName()) << "Storage Backend is ready. Quassel Schema Version:" << installedSchemaVersion();
+  return IsReady;
 }
 
 QString AbstractSqlStorage::queryString(const QString &queryName, int version) {
   if(version == 0)
     version = schemaVersion();
-    
+
   QFileInfo queryInfo(QString(":/SQL/%1/%2/%3.sql").arg(displayName()).arg(version).arg(queryName));
   if(!queryInfo.exists() || !queryInfo.isFile() || !queryInfo.isReadable()) {
     qCritical() << "Unable to read SQL-Query" << queryName << "for engine" << displayName();
@@ -131,14 +130,8 @@ QString AbstractSqlStorage::queryString(const QString &queryName, int version) {
     return QString();
   QString query = QTextStream(&queryFile).readAll();
   queryFile.close();
-  
-  return query.trimmed();
-}
 
-QSqlQuery &AbstractSqlStorage::cachedQuery(const QString &queryName, int version) {
-  Q_ASSERT(_connectionPool.contains(QThread::currentThread()));
-  qDebug() << "cached query" << queryName << "using" << _connectionPool[QThread::currentThread()]->name() << QThread::currentThread();
-  return _connectionPool[QThread::currentThread()]->cachedQuery(queryName, version);
+  return query.trimmed();
 }
 
 QStringList AbstractSqlStorage::setupQueries() {
@@ -151,21 +144,28 @@ QStringList AbstractSqlStorage::setupQueries() {
 }
 
 bool AbstractSqlStorage::setup(const QVariantMap &settings) {
-  Q_UNUSED(settings)
+  setConnectionProperties(settings);
   QSqlDatabase db = logDb();
   if(!db.isOpen()) {
     qCritical() << "Unable to setup Logging Backend!";
     return false;
   }
 
+  db.transaction();
   foreach(QString queryString, setupQueries()) {
     QSqlQuery query = db.exec(queryString);
     if(!watchQuery(query)) {
       qCritical() << "Unable to setup Logging Backend!";
+      db.rollback();
       return false;
     }
   }
-  return true;
+  bool success = setupSchemaVersion(schemaVersion());
+  if(success)
+    db.commit();
+  else
+    db.rollback();
+  return success;
 }
 
 QStringList AbstractSqlStorage::upgradeQueries(int version) {
@@ -192,7 +192,7 @@ bool AbstractSqlStorage::upgradeDb() {
       }
     }
   }
-  return true;
+  return updateSchemaVersion(schemaVersion());
 }
 
 
@@ -232,7 +232,7 @@ bool AbstractSqlStorage::watchQuery(QSqlQuery &query) {
     qCritical() << "               Error Message:"   << query.lastError().text();
     qCritical() << "              Driver Message:"   << query.lastError().driverText();
     qCritical() << "                  DB Message:"   << query.lastError().databaseText();
-    
+
     return false;
   }
   return true;
@@ -246,19 +246,13 @@ void AbstractSqlStorage::connectionDestroyed() {
 // ========================================
 //  AbstractSqlStorage::Connection
 // ========================================
-AbstractSqlStorage::Connection::Connection(const QString &name, AbstractSqlStorage *storage, QObject *parent)
+AbstractSqlStorage::Connection::Connection(const QString &name, QObject *parent)
   : QObject(parent),
-    _name(name.toLatin1()),
-    _storageEngine(storage)
+    _name(name.toLatin1())
 {
 }
 
 AbstractSqlStorage::Connection::~Connection() {
-  QHash<QPair<QString, int>, QSqlQuery *>::iterator iter = _queryCache.begin();
-  while(iter != _queryCache.end()) {
-    delete *iter;
-    iter = _queryCache.erase(iter);
-  }
   {
     QSqlDatabase db = QSqlDatabase::database(name(), false);
     if(db.isOpen()) {
@@ -269,25 +263,212 @@ AbstractSqlStorage::Connection::~Connection() {
   QSqlDatabase::removeDatabase(name());
 }
 
-QSqlQuery &AbstractSqlStorage::Connection::cachedQuery(const QString &queryName, int version) {
-  QPair<QString, int> queryId = qMakePair(queryName, version);
-  if(_queryCache.contains(queryId)) {
-    return *(_queryCache[queryId]);
+
+
+
+// ========================================
+//  AbstractSqlMigrator
+// ========================================
+AbstractSqlMigrator::AbstractSqlMigrator()
+  : _query(0)
+{
+}
+
+void AbstractSqlMigrator::newQuery(const QString &query, QSqlDatabase db) {
+  Q_ASSERT(!_query);
+  _query = new QSqlQuery(db);
+  _query->prepare(query);
+}
+
+void AbstractSqlMigrator::resetQuery() {
+  delete _query;
+  _query = 0;
+}
+
+bool AbstractSqlMigrator::exec() {
+  Q_ASSERT(_query);
+  _query->exec();
+  return !_query->lastError().isValid();
+}
+
+QString AbstractSqlMigrator::migrationObject(MigrationObject moType) {
+  switch(moType) {
+  case QuasselUser:
+    return "QuasselUser";
+  case Sender:
+    return "Sender";
+  case Identity:
+    return "Identity";
+  case IdentityNick:
+    return "IdentityNick";
+  case Network:
+    return "Network";
+  case Buffer:
+    return "Buffer";
+  case Backlog:
+    return "Backlog";
+  case IrcServer:
+    return "IrcServer";
+  case UserSetting:
+    return "UserSetting";
+  };
+  return QString();
+}
+
+QVariantList AbstractSqlMigrator::boundValues() {
+  QVariantList values;
+  if(!_query)
+    return values;
+
+  int numValues = _query->boundValues().count();
+  for(int i = 0; i < numValues; i++) {
+    values << _query->boundValue(i);
   }
+  return values;
+}
+
+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().toAscii().data();
+  qWarning() << "  Error Number:"   << lastError().number();
+  qWarning() << "  Error Message:"   << lastError().text();
+}
 
-  QSqlQuery *query = new QSqlQuery(QSqlDatabase::database(name()));
-  query->prepare(_storageEngine->queryString(queryName, version));
-  _queryCache[queryId] = query;
-  return *query;
+
+// ========================================
+//  AbstractSqlMigrationReader
+// ========================================
+AbstractSqlMigrationReader::AbstractSqlMigrationReader()
+  : AbstractSqlMigrator(),
+    _writer(0)
+{
 }
 
-void AbstractSqlStorage::Connection::syncCachedQueries() {
-  QHash<QPair<QString, int>, QSqlQuery *>::iterator iter = _queryCache.begin();
-  while(iter != _queryCache.end()) {
-    delete *iter;
-    iter = _queryCache.erase(iter);
+bool AbstractSqlMigrationReader::migrateTo(AbstractSqlMigrationWriter *writer) {
+  if(!transaction()) {
+    qWarning() << "AbstractSqlMigrationReader::migrateTo(): unable to start reader stransaction!";
+    return false;
   }
-  QSqlDatabase db = QSqlDatabase::database(name(), false);
-  if(db.isOpen())
-    db.commit();
+  if(!writer->transaction()) {
+    qWarning() << "AbstractSqlMigrationReader::migrateTo(): unable to start writer stransaction!";
+    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;
+
+  SenderMO senderMo;
+  if(!transferMo(Sender, senderMo))
+    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;
+
+  BacklogMO backlogMo;
+  if(!transferMo(Backlog, backlogMo))
+    return false;
+
+  IrcServerMO ircServerMo;
+  if(!transferMo(IrcServer, ircServerMo))
+    return false;
+
+  UserSettingMO userSettingMo;
+  if(!transferMo(UserSetting, userSettingMo))
+    return false;
+
+  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 = 0;
+}
+
+bool AbstractSqlMigrationReader::finalizeMigration() {
+  resetQuery();
+  _writer->resetQuery();
+
+  commit();
+  if(!_writer->commit()) {
+    _writer = 0;
+    return false;
+  }
+  _writer = 0;
+  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("Transfering %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;
+}
+