summaryrefslogtreecommitdiffhomepage
path: root/migration/migration.cpp
blob: a196d762f46ad9cb56fc15c92b9fdb7945f1423f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "migration.hpp"

#include "options/options.hpp"
#include "compat/util.hpp"

#include <QString>
#include <QSettings>
#include <QDebug>

#include <memory>

namespace migrations {

namespace detail {

void migrator::register_migration(migration* m)
{
    migrations().push_back(m);
}

migrator::vec& migrator::migrations()
{
    static vec ret;
    return ret;
}

QString migrator::last_migration_time()
{
    QString ret;

    std::shared_ptr<QSettings> s(options::group::ini_file());

    s->beginGroup("migrations");
    ret = s->value("last-migration-at", "19700101_00").toString();
    s->endGroup();

    return ret;
}

QString migrator::time_after_migrations()
{
    const vec list = sorted_migrations();

    if (list.size() == 0u)
        return QStringLiteral("19700101_00");

    QString ret = list[list.size() - 1]->unique_date();
    ret += QStringLiteral("~");

    return ret;
}

void migrator::set_last_migration_time(const QString& val)
{
    std::shared_ptr<QSettings> s(options::group::ini_file());

    s->beginGroup("migrations");
    s->setValue("last-migration-at", val);
    s->endGroup();
}

migrator::vec migrator::sorted_migrations()
{
    vec list(migrations());
    std::sort(list.begin(), list.end(), [](const mm x, const mm y) { return x->unique_date() < y->unique_date(); });
    return list;
}

std::vector<QString> migrator::run()
{
    vec migrations = sorted_migrations();
    vstr 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();
        }
    }

    return done;
}

}

migration::migration() {}
migration::~migration() {}

} // ns

std::vector<QString> run_migrations()
{
    return migrations::detail::migrator::run();
}

void mark_config_as_not_needing_migration()
{
    using m = migrations::detail::migrator;

    m::mark_config_as_not_needing_migration();
}

void migrations::detail::migrator::mark_config_as_not_needing_migration()
{
    set_last_migration_time(time_after_migrations());
}