shared: DBMigrator: Take DatabaseFactory, run vacuum, add error() sig, start() slot

This commit is contained in:
Albert S. 2022-07-28 13:27:37 +02:00
джерело 5996971195
коміт 49b57e1740
2 змінених файлів з 52 додано та 29 видалено

@ -6,10 +6,10 @@
#include "dbmigrator.h"
#include "looqsgeneralexception.h"
DBMigrator::DBMigrator(QSqlDatabase &db)
DBMigrator::DBMigrator(DatabaseFactory &factory)
{
Q_INIT_RESOURCE(migrations);
this->db = &db;
this->dbFactory = &factory;
}
DBMigrator::~DBMigrator()
@ -30,7 +30,8 @@ QStringList DBMigrator::getMigrationFilenames()
uint32_t DBMigrator::currentRevision()
{
QSqlQuery dbquery(*db);
QSqlDatabase db = dbFactory->forCurrentThread();
QSqlQuery dbquery(db);
dbquery.exec("PRAGMA user_version;");
if(!dbquery.next())
{
@ -48,38 +49,56 @@ bool DBMigrator::migrationNeeded()
return currentRev < static_cast<uint32_t>(migrations.size());
}
void DBMigrator::start()
{
performMigrations();
}
void DBMigrator::performMigrations()
{
QStringList migrations = getMigrationFilenames();
uint32_t currentRev = currentRevision();
uint32_t targetRev = (migrations.size());
for(uint32_t i = currentRev + 1; i <= targetRev; i++)
{
QString fileName = QString(":/looqs-migrations/%1.sql").arg(i);
QFile file{fileName};
if(!file.open(QIODevice::ReadOnly))
{
throw LooqsGeneralException("Migration: Failed to find required revision file");
}
QTextStream stream(&file);
db->transaction();
while(!stream.atEnd())
{
QString sql = stream.readLine();
QSqlQuery sqlQuery{*db};
if(!sqlQuery.exec(sql))
{
QSqlDatabase db = dbFactory->forCurrentThread();
db->rollback();
throw LooqsGeneralException("Failed to execute sql statement while initializing database: " +
sqlQuery.lastError().text());
try
{
for(uint32_t i = currentRev + 1; i <= targetRev; i++)
{
QString fileName = QString(":/looqs-migrations/%1.sql").arg(i);
QFile file{fileName};
if(!file.open(QIODevice::ReadOnly))
{
throw LooqsGeneralException("Migration: Failed to find required revision file");
}
QTextStream stream(&file);
db.transaction();
while(!stream.atEnd())
{
QString sql = stream.readLine();
QSqlQuery sqlQuery{db};
if(!sqlQuery.exec(sql))
{
db.rollback();
throw LooqsGeneralException("Failed to execute sql statement while initializing database: " +
sqlQuery.lastError().text());
}
}
QSqlQuery updateVersion{db};
updateVersion.exec(QString("PRAGMA user_version=%1;").arg(i));
db.commit();
QSqlQuery vacuumQuery{db};
vacuumQuery.exec("VACUUM;");
emit migrationDone(i);
}
QSqlQuery updateVersion{*db};
updateVersion.exec(QString("PRAGMA user_version=%1;").arg(i));
db->commit();
emit migrationDone(i);
emit done();
}
catch(LooqsGeneralException &e)
{
emit error(e.message);
}
emit done();
}

@ -3,14 +3,15 @@
#include <QStringList>
#include <QSqlDatabase>
#include <QObject>
#include "databasefactory.h"
class DBMigrator : public QObject
{
Q_OBJECT
private:
QSqlDatabase *db;
DatabaseFactory *dbFactory;
public:
DBMigrator(QSqlDatabase &db);
DBMigrator(DatabaseFactory &dbFactory);
~DBMigrator();
uint32_t currentRevision();
void performMigrations();
@ -19,6 +20,9 @@ class DBMigrator : public QObject
signals:
void migrationDone(uint32_t);
void done();
void error(QString e);
public slots:
void start();
};
#endif // DBMIGRATOR_H