diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2016-09-09 20:02:29 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2016-09-09 20:41:53 +0200 |
commit | 64e983b3dda8f9b7d10cb31bebbc05ed13954e67 (patch) | |
tree | 5b9838ffc546555ef4fc9233e2fef82ad362d2c9 | |
parent | bc139ef46760fcb9b9b7e5e6bb8e4a914b8eb222 (diff) |
migration: try prevent developer naming migrations badly
-rw-r--r-- | migration/migration.cpp | 50 | ||||
-rw-r--r-- | migration/migration.hpp | 1 |
2 files changed, 51 insertions, 0 deletions
diff --git a/migration/migration.cpp b/migration/migration.cpp index a196d762..06cd5b93 100644 --- a/migration/migration.cpp +++ b/migration/migration.cpp @@ -1,3 +1,5 @@ +#include <cstdlib> + #include "migration.hpp" #include "options/options.hpp" @@ -5,16 +7,56 @@ #include <QString> #include <QSettings> +#include <QChar> + #include <QDebug> #include <memory> +// individual migrations are run in the UI thread. they can be interactive if necessary. + namespace migrations { namespace detail { void migrator::register_migration(migration* m) { + const QString date = m->unique_date(); + + for (migration* m2 : migrations()) + if (m2->unique_date() == date) + std::abort(); + + if (date.size() != 11) + abort(); + + if (date[8] != QChar('_')) + abort(); + + const QString year = date.left(4); + const QString month = date.mid(4, 2); + const QString day = date.mid(6, 2); + const QString serial = date.mid(9, 2); + + bool ok = true; + + if (year < "2016") + abort(); + + const int month_ = to_int(month, ok), day_ = to_int(day, ok); + + (void) to_int(year, ok); + (void) to_int(serial, ok); + + if (!ok) + abort(); + + if (month_ < 1 || month_ > 12) + abort(); + + if (day_ < 1 || day_ > 31) + abort(); + migrations().push_back(m); } @@ -66,6 +108,14 @@ migrator::vec migrator::sorted_migrations() return list; } +int migrator::to_int(const QString& str, bool& ok) +{ + bool tmp = false; + const int ret = int(str.toUInt(&tmp)); + ok &= tmp; + return ret; +} + std::vector<QString> migrator::run() { vec migrations = sorted_migrations(); diff --git a/migration/migration.hpp b/migration/migration.hpp index b9c20f15..73ab2d53 100644 --- a/migration/migration.hpp +++ b/migration/migration.hpp @@ -23,6 +23,7 @@ namespace detail { static void set_last_migration_time(const QString& val); migrator() = delete; static vec sorted_migrations(); + static int to_int(const QString& str, bool& ok); public: static vstr run(); static void register_migration(migration* m); |