aboutsummaryrefslogtreecommitdiffstats
path: root/Plugins/ConfigMigration/configmigrationwizard.cpp
blob: 7868761f1b7c61363bcf6949f516ccc37ea9e1d0 (plain) (blame)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "configmigrationwizard.h"
#include "ui_configmigrationwizard.h"
#include "configmigration.h"
#include "configmigrationitem.h"
#include "iconmanager.h"
#include "uiutils.h"
#include "dbtree/dbtree.h"
#include "dbtree/dbtreemodel.h"
#include "services/config.h"
#include "sqlitestudio.h"
#include "db/dbsqlite3.h"
#include "services/notifymanager.h"
#include "services/dbmanager.h"

ConfigMigrationWizard::ConfigMigrationWizard(QWidget *parent, ConfigMigration* cfgMigration) :
    QWizard(parent),
    ui(new Ui::ConfigMigrationWizard),
    cfgMigration(cfgMigration)
{
    init();
}

ConfigMigrationWizard::~ConfigMigrationWizard()
{
    clearFunctions();
    delete ui;
}

bool ConfigMigrationWizard::didMigrate()
{
    return migrated;
}

void ConfigMigrationWizard::accept()
{
    migrate();
    QWizard::accept();
}

void ConfigMigrationWizard::init()
{
    ui->setupUi(this);

#ifdef Q_OS_MACX
    resize(width() + 150, height());
    setPixmap(QWizard::BackgroundPixmap, addOpacity(ICONMANAGER->getIcon("config_migration")->pixmap(180, 180), 0.4));
#endif

    ui->optionsPage->setValidator([=]() -> bool
    {
        QString grpName = ui->groupNameEdit->text();

        bool grpOk = true;
        QString grpErrorMsg;
        if (ui->dbGroup->isEnabled() && ui->dbGroup->isChecked())
        {
            if (grpName.isEmpty())
            {
                grpOk = false;
                grpErrorMsg = tr("Enter a non-empty name.");
            }
            else
            {
                DbTreeItem* item = DBTREE->getModel()->findItem(DbTreeItem::Type::DIR, grpName);
                if (item && !item->parentDbTreeItem())
                {
                    grpOk = false;
                    grpErrorMsg = tr("Top level group named '%1' already exists. Enter a group name that does not exist yet.").arg(grpName);
                }
            }
        }

        setValidState(ui->groupNameEdit, grpOk, grpErrorMsg);

        return grpOk;
    });


    QTreeWidgetItem* treeItem = nullptr;
    for (ConfigMigrationItem* cfgItem : cfgMigration->getItemsToMigrate())
    {
        treeItem = new QTreeWidgetItem({cfgItem->label});
        treeItem->setData(0, Qt::UserRole, static_cast<int>(cfgItem->type));
        treeItem->setFlags(treeItem->flags() | Qt::ItemIsUserCheckable);
        treeItem->setCheckState(0, Qt::Checked);
        ui->itemsTree->addTopLevelItem(treeItem);
    }

    connect(ui->dbGroup, SIGNAL(clicked()), ui->optionsPage, SIGNAL(completeChanged()));
    connect(ui->groupNameEdit, SIGNAL(textChanged(QString)), ui->optionsPage, SIGNAL(completeChanged()));
    connect(this, SIGNAL(updateOptionsValidation()), ui->optionsPage, SIGNAL(completeChanged()));
    connect(this, SIGNAL(currentIdChanged(int)), this, SLOT(updateOptions()));

    emit updateOptionsValidation();
}

void ConfigMigrationWizard::migrate()
{
    Db* oldCfgDb = cfgMigration->getOldCfgDb();
    if (!oldCfgDb->open())
    {
        notifyError(tr("Could not open old configuration file in order to migrate settings from it."));
        return;
    }

    QString cfgFilePath = SQLITESTUDIO->getConfig()->getConfigFilePath();
    Db* newCfgDb = new DbSqlite3("Config migration connection", cfgFilePath, {{DB_PURE_INIT, true}});
    if (!newCfgDb->open())
    {
        notifyError(tr("Could not open current configuration file in order to migrate settings from old configuration file."));
        delete newCfgDb;
        return;
    }

    newCfgDb->begin();
    bool migrated = migrateSelected(oldCfgDb, newCfgDb);
    if (migrated && !newCfgDb->commit())
    {
        notifyError(tr("Could not commit migrated data into new configuration file: %1").arg(newCfgDb->getErrorText()));
        newCfgDb->rollback();
    }
    else if (!migrated)
    {
        newCfgDb->rollback();
    }
    else
    {
        finalize();
    }
    oldCfgDb->close();
    newCfgDb->close();
    delete newCfgDb;
    clearFunctions();
}

bool ConfigMigrationWizard::migrateSelected(Db* oldCfgDb, Db* newCfgDb)
{
    if (checkedTypes.contains(ConfigMigrationItem::Type::BUG_REPORTS) && !migrateBugReports(oldCfgDb, newCfgDb))
        return false;

    if (checkedTypes.contains(ConfigMigrationItem::Type::DATABASES) && !migrateDatabases(oldCfgDb, newCfgDb))
        return false;

    if (checkedTypes.contains(ConfigMigrationItem::Type::FUNCTION_LIST) && !migrateFunction(oldCfgDb, newCfgDb))
        return false;

    if (checkedTypes.contains(ConfigMigrationItem::Type::SQL_HISTORY) && !migrateSqlHistory(oldCfgDb, newCfgDb))
        return false;

    return true;
}

bool ConfigMigrationWizard::migrateBugReports(Db* oldCfgDb, Db* newCfgDb)
{
    static_qstring(oldBugsQuery, "SELECT created_on, brief, url, type FROM bugs");
    static_qstring(newBugsInsert, "INSERT INTO reports_history (timestamp, feature_request, title, url) VALUES (?, ?, ?, ?)");

    SqlQueryPtr insertResults;
    SqlResultsRowPtr row;
    SqlQueryPtr results = oldCfgDb->exec(oldBugsQuery);
    if (results->isError())
    {
        notifyError(tr("Could not read bug reports history from old configuration file in order to migrate it: %1").arg(results->getErrorText()));
        return false;
    }

    bool feature;
    QString url;
    while (results->hasNext())
    {
        row = results->next();
        feature = (row->value("type").toString().toUpper() == "FEATURE");
        url = row->value("url").toString().trimmed();
        if (url.startsWith("http://") && url.contains("sqlitestudio.one.pl"))
            url.replace("sqlitestudio.one.pl", "sqlitestudio.pl").replace("report_bug.rvt", "report_bug3.rvt");

        insertResults = newCfgDb->exec(newBugsInsert, {row->value("created_on"), feature, row->value("brief"), url});
        if (insertResults->isError())
        {
            notifyError(tr("Could not insert a bug reports history entry into new configuration file: %1").arg(insertResults->getErrorText()));
            return false;
        }
    }

    return true;
}

bool ConfigMigrationWizard::migrateDatabases(Db* oldCfgDb, Db* newCfgDb)
{
    static_qstring(oldDbListQuery, "SELECT name, path FROM dblist");
    static_qstring(newDbListInsert, "INSERT INTO dblist (name, path) VALUES (?, ?)");
    static_qstring(groupOrderQuery, "SELECT max([order]) + 1 FROM groups WHERE parent %1");
    static_qstring(groupInsert, "INSERT INTO groups (name, [order], parent, open, dbname) VALUES (?, ?, ?, ?, ?)");

    SqlQueryPtr groupResults;
    SqlQueryPtr insertResults;
    SqlResultsRowPtr row;
    SqlQueryPtr results = oldCfgDb->exec(oldDbListQuery);
    if (results->isError())
    {
        notifyError(tr("Could not read database list from old configuration file in order to migrate it: %1").arg(results->getErrorText()));
        return false;
    }

    // Creating containing group
    bool putInGroup = ui->dbGroup->isEnabled() && ui->dbGroup->isChecked();
    qint64 groupId = -1;
    int order;
    if (putInGroup)
    {
        // Query order
        groupResults = newCfgDb->exec(groupOrderQuery.arg("IS NULL"));
        if (groupResults->isError())
        {
            notifyError(tr("Could not query for available order for containing group in new configuration file in order to migrate the database list: %1")
                        .arg(groupResults->getErrorText()));
            return false;
        }

        order = groupResults->getSingleCell().toInt();

        // Insert group
        groupResults = newCfgDb->exec(groupInsert, {ui->groupNameEdit->text(), order, QVariant(), 1, QVariant()});
        if (groupResults->isError())
        {
            notifyError(tr("Could not create containing group in new configuration file in order to migrate the database list: %1").arg(groupResults->getErrorText()));
            return false;
        }
        groupId = groupResults->getRegularInsertRowId();
    }

    // Migrating the list
    QString name;
    QString path;
    while (results->hasNext())
    {
        row = results->next();
        name = row->value("name").toString();
        path = row->value("path").toString();

        if (DBLIST->getByName(name) || DBLIST->getByPath(path)) // already on the new list
            continue;

        insertResults = newCfgDb->exec(newDbListInsert, {name, path});
        if (insertResults->isError())
        {
            notifyError(tr("Could not insert a database entry into new configuration file: %1").arg(insertResults->getErrorText()));
            return false;
        }

        // Query order
        if (putInGroup)
            groupResults = newCfgDb->exec(groupOrderQuery.arg("= ?"), {groupId});
        else
            groupResults = newCfgDb->exec(groupOrderQuery.arg("IS NULL"));

        if (groupResults->isError())
        {
            notifyError(tr("Could not query for available order for next database in new configuration file in order to migrate the database list: %1")
                        .arg(groupResults->getErrorText()));
            return false;
        }

        order = groupResults->getSingleCell().toInt();

        // Insert group
        groupResults = newCfgDb->exec(groupInsert, {QVariant(), order, putInGroup ? QVariant(groupId) : QVariant(), 0, name});
        if (groupResults->isError())
        {
            notifyError(tr("Could not create group referencing the database in new configuration file: %1").arg(groupResults->getErrorText()));
            return false;
        }
    }

    return true;
}

bool ConfigMigrationWizard::migrateFunction(Db* oldCfgDb, Db* newCfgDb)
{
    UNUSED(newCfgDb);

    static_qstring(oldFunctionsQuery, "SELECT name, type, code FROM functions");

    SqlResultsRowPtr row;
    SqlQueryPtr results = oldCfgDb->exec(oldFunctionsQuery);
    if (results->isError())
    {
        notifyError(tr("Could not read function list from old configuration file in order to migrate it: %1").arg(results->getErrorText()));
        return false;
    }

    clearFunctions();
    for (FunctionManager::ScriptFunction* fn : FUNCTIONS->getAllScriptFunctions())
        fnList << new FunctionManager::ScriptFunction(*fn);

    FunctionManager::ScriptFunction* fn = nullptr;
    while (results->hasNext())
    {
        row = results->next();

        fn = new FunctionManager::ScriptFunction();
        fn->type = FunctionManager::ScriptFunction::SCALAR;
        fn->lang = row->value("type").toString();
        fn->name = row->value("name").toString();
        fn->code = row->value("code").toString();
        fnList << fn;
    }

    return true;
}

bool ConfigMigrationWizard::migrateSqlHistory(Db* oldCfgDb, Db* newCfgDb)
{
    static_qstring(historyIdQuery, "SELECT CASE WHEN max(id) IS NULL THEN 0 ELSE max(id) + 1 END FROM sqleditor_history");
    static_qstring(oldHistoryQuery, "SELECT dbname, date, time, rows, sql FROM history");
    static_qstring(newHistoryInsert, "INSERT INTO sqleditor_history (id, dbname, date, time_spent, rows, sql) VALUES (?, ?, ?, ?, ?, ?)");

    SqlQueryPtr insertResults;
    SqlResultsRowPtr row;
    SqlQueryPtr results = oldCfgDb->exec(oldHistoryQuery);
    if (results->isError())
    {
        notifyError(tr("Could not read SQL queries history from old configuration file in order to migrate it: %1").arg(results->getErrorText()));
        return false;
    }

    SqlQueryPtr idResults = newCfgDb->exec(historyIdQuery);
    if (idResults->isError())
    {
        notifyError(tr("Could not read next ID for SQL queries history in new configuration file: %1").arg(idResults->getErrorText()));
        return false;
    }
    qint64 nextId = idResults->getSingleCell().toLongLong();

    int timeSpent;
    int date;
    while (results->hasNext())
    {
        row = results->next();
        timeSpent = qRound(row->value("time").toDouble() * 1000);
        date = QDateTime::fromString(row->value("date").toString(), "yyyy-MM-dd HH:mm").toTime_t();

        insertResults = newCfgDb->exec(newHistoryInsert, {nextId++, row->value("dbname"), date, timeSpent, row->value("rows"), row->value("sql")});
        if (insertResults->isError())
        {
            notifyError(tr("Could not insert SQL history entry into new configuration file: %1").arg(insertResults->getErrorText()));
            return false;
        }
    }

    return true;
}

void ConfigMigrationWizard::finalize()
{
    if (checkedTypes.contains(ConfigMigrationItem::Type::FUNCTION_LIST))
    {
        FUNCTIONS->setScriptFunctions(fnList);
        fnList.clear();
    }

    if (checkedTypes.contains(ConfigMigrationItem::Type::SQL_HISTORY))
        CFG->refreshSqlHistory();

    if (checkedTypes.contains(ConfigMigrationItem::Type::DATABASES))
    {
        bool ignore = DBTREE->getModel()->getIgnoreDbLoadedSignal();
        DBTREE->getModel()->setIgnoreDbLoadedSignal(true);
        DBLIST->scanForNewDatabasesInConfig();
        DBTREE->getModel()->setIgnoreDbLoadedSignal(ignore);
        DBTREE->getModel()->loadDbList();
    }

    migrated = true;
}

void ConfigMigrationWizard::collectCheckedTypes()
{
    checkedTypes.clear();

    QTreeWidgetItem* item = nullptr;
    for (int i = 0, total = ui->itemsTree->topLevelItemCount(); i < total; ++i)
    {
        item = ui->itemsTree->topLevelItem(i);
        if (item->checkState(0) != Qt::Checked)
            continue;

        checkedTypes << static_cast<ConfigMigrationItem::Type>(item->data(0, Qt::UserRole).toInt());
    }
}

void ConfigMigrationWizard::clearFunctions()
{
    for (FunctionManager::ScriptFunction* fn : fnList)
        delete fn;

    fnList.clear();
}

void ConfigMigrationWizard::updateOptions()
{
    if (currentPage() == ui->optionsPage)
    {
        collectCheckedTypes();
        ui->dbGroup->setEnabled(checkedTypes.contains(ConfigMigrationItem::Type::DATABASES));
    }
}