From 526304a41970c1ad890cf81d92bb4b123e8608e7 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 28 Apr 2018 17:19:27 +0200 Subject: migration: use thunks Otherwise we get QCoreApplication used before QApplication instance is created. Requesting the sorted migration data will force the thunks. --- migration/migration.cpp | 103 ++++++++++++++++++++++++++---------------------- 1 file changed, 56 insertions(+), 47 deletions(-) (limited to 'migration/migration.cpp') diff --git a/migration/migration.cpp b/migration/migration.cpp index b92d6384..569dff99 100644 --- a/migration/migration.cpp +++ b/migration/migration.cpp @@ -26,11 +26,14 @@ namespace migrations { namespace detail { -void migrator::register_migration(migration* m) +static std::vector migration_list; +static std::vector migration_thunks; + +void migrator::register_migration(mptr m) { const QString date = m->unique_date(); - for (migration* m2 : migrations()) + for (mptr m2 : migration_list) if (m2->unique_date() == date) std::abort(); @@ -64,13 +67,38 @@ void migrator::register_migration(migration* m) if (day_ < 1 || day_ > 31) abort(); - migrations().push_back(m); + migration_list.push_back(m); } -std::vector& migrator::migrations() +void migrator::eval_thunks() { - static std::vector ret; - return ret; + for (auto& fun : migration_thunks) + { + mptr m = fun(); + register_migration(m); + } + if (migration_thunks.size()) + sort_migrations(); + migration_thunks.clear(); +} + +void migrator::add_migration_thunk(mfun& thunk) +{ + migration_thunks.push_back(thunk); +} + +std::vector& migrator::migrations() +{ + eval_thunks(); + return migration_list; +} + +void migrator::sort_migrations() +{ + std::sort(migration_list.begin(), migration_list.end(), + [](const mptr x, const mptr y) { + return x->unique_date() < y->unique_date(); + }); } QString migrator::last_migration_time() @@ -88,7 +116,7 @@ QString migrator::last_migration_time() QString migrator::time_after_migrations() { - const std::vector list = sorted_migrations(); + const std::vector& list = migrations(); if (list.size() == 0u) return QStringLiteral("19700101_00"); @@ -113,16 +141,6 @@ void migrator::set_last_migration_time(const QString& val) }); } -std::vector migrator::sorted_migrations() -{ - std::vector list(migrations()); - - using mm = migration*; - - std::sort(list.begin(), list.end(), [](const mm x, const mm y) { return x->unique_date() < y->unique_date(); }); - return list; -} - int migrator::to_int(const QString& str, bool& ok) { bool tmp = false; @@ -133,46 +151,37 @@ int migrator::to_int(const QString& str, bool& ok) std::vector migrator::run() { - std::vector migrations = sorted_migrations(); std::vector done; const QString last_migration = last_migration_time(); - for (migration* m_ : migrations) - { - migration& m(*m_); - - const QString date = m.unique_date(); - - if (date <= last_migration) - continue; - - if (m.should_run()) - { - m.run(); - done.push_back(m.name()); - } - } - - mark_config_as_not_needing_migration(); - - if (done.size()) - { - for (const QString& name : done) - { - const QByteArray data = name.toUtf8(); - qDebug() << "migrate:" << data.constData(); - } - } + options::group::with_global_settings_object([&](QSettings&) { + options::group::with_settings_object([&](QSettings&) { + for (mptr m : migrations()) + { + const QString date = m->unique_date(); + + if (date <= last_migration) + continue; + + if (m->should_run()) + { + const QByteArray name = m->name().toUtf8(); + const QByteArray date = m->unique_date().toUtf8(); + qDebug() << "migrate:" << date.constData() << name.constData(); + m->run(); + done.push_back(m->name()); + } + } + mark_config_as_not_needing_migration(); + }); + }); return done; } } -migration::migration() {} -migration::~migration() {} - } // ns std::vector run_migrations() -- cgit v1.2.3