shared: DBMigrator: Take DatabaseFactory, run vacuum, add error() sig, start() slot
This commit is contained in:
vanhempi
5996971195
commit
49b57e1740
@ -6,10 +6,10 @@
|
|||||||
#include "dbmigrator.h"
|
#include "dbmigrator.h"
|
||||||
#include "looqsgeneralexception.h"
|
#include "looqsgeneralexception.h"
|
||||||
|
|
||||||
DBMigrator::DBMigrator(QSqlDatabase &db)
|
DBMigrator::DBMigrator(DatabaseFactory &factory)
|
||||||
{
|
{
|
||||||
Q_INIT_RESOURCE(migrations);
|
Q_INIT_RESOURCE(migrations);
|
||||||
this->db = &db;
|
this->dbFactory = &factory;
|
||||||
}
|
}
|
||||||
|
|
||||||
DBMigrator::~DBMigrator()
|
DBMigrator::~DBMigrator()
|
||||||
@ -30,7 +30,8 @@ QStringList DBMigrator::getMigrationFilenames()
|
|||||||
|
|
||||||
uint32_t DBMigrator::currentRevision()
|
uint32_t DBMigrator::currentRevision()
|
||||||
{
|
{
|
||||||
QSqlQuery dbquery(*db);
|
QSqlDatabase db = dbFactory->forCurrentThread();
|
||||||
|
QSqlQuery dbquery(db);
|
||||||
dbquery.exec("PRAGMA user_version;");
|
dbquery.exec("PRAGMA user_version;");
|
||||||
if(!dbquery.next())
|
if(!dbquery.next())
|
||||||
{
|
{
|
||||||
@ -48,38 +49,56 @@ bool DBMigrator::migrationNeeded()
|
|||||||
return currentRev < static_cast<uint32_t>(migrations.size());
|
return currentRev < static_cast<uint32_t>(migrations.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DBMigrator::start()
|
||||||
|
{
|
||||||
|
performMigrations();
|
||||||
|
}
|
||||||
|
|
||||||
void DBMigrator::performMigrations()
|
void DBMigrator::performMigrations()
|
||||||
{
|
{
|
||||||
QStringList migrations = getMigrationFilenames();
|
QStringList migrations = getMigrationFilenames();
|
||||||
uint32_t currentRev = currentRevision();
|
uint32_t currentRev = currentRevision();
|
||||||
uint32_t targetRev = (migrations.size());
|
uint32_t targetRev = (migrations.size());
|
||||||
|
|
||||||
for(uint32_t i = currentRev + 1; i <= targetRev; i++)
|
QSqlDatabase db = dbFactory->forCurrentThread();
|
||||||
{
|
|
||||||
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();
|
try
|
||||||
throw LooqsGeneralException("Failed to execute sql statement while initializing database: " +
|
{
|
||||||
sqlQuery.lastError().text());
|
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};
|
emit done();
|
||||||
updateVersion.exec(QString("PRAGMA user_version=%1;").arg(i));
|
}
|
||||||
db->commit();
|
catch(LooqsGeneralException &e)
|
||||||
emit migrationDone(i);
|
{
|
||||||
|
emit error(e.message);
|
||||||
}
|
}
|
||||||
emit done();
|
|
||||||
}
|
}
|
||||||
|
@ -3,14 +3,15 @@
|
|||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
#include <QSqlDatabase>
|
#include <QSqlDatabase>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
#include "databasefactory.h"
|
||||||
class DBMigrator : public QObject
|
class DBMigrator : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
private:
|
private:
|
||||||
QSqlDatabase *db;
|
DatabaseFactory *dbFactory;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DBMigrator(QSqlDatabase &db);
|
DBMigrator(DatabaseFactory &dbFactory);
|
||||||
~DBMigrator();
|
~DBMigrator();
|
||||||
uint32_t currentRevision();
|
uint32_t currentRevision();
|
||||||
void performMigrations();
|
void performMigrations();
|
||||||
@ -19,6 +20,9 @@ class DBMigrator : public QObject
|
|||||||
signals:
|
signals:
|
||||||
void migrationDone(uint32_t);
|
void migrationDone(uint32_t);
|
||||||
void done();
|
void done();
|
||||||
|
void error(QString e);
|
||||||
|
public slots:
|
||||||
|
void start();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // DBMIGRATOR_H
|
#endif // DBMIGRATOR_H
|
||||||
|
Ladataan…
x
Viittaa uudesa ongelmassa
Block a user