1 | /* |
---|---|
2 | SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.1-or-later |
5 | */ |
6 | |
7 | #include "migrator.h" |
8 | #include "fileindexerconfig.h" |
9 | |
10 | #include <QFile> |
11 | #include <QDir> |
12 | |
13 | using namespace Baloo; |
14 | |
15 | Migrator::Migrator(const QString& dbPath, FileIndexerConfig* config) |
16 | : m_dbPath(dbPath) |
17 | , m_config(config) |
18 | { |
19 | Q_ASSERT(!dbPath.isEmpty()); |
20 | Q_ASSERT(!dbPath.endsWith(QLatin1Char('/'))); |
21 | Q_ASSERT(config); |
22 | } |
23 | |
24 | /* |
25 | * Changing this version number indicates that the old index should be deleted |
26 | * and the indexing should be started from scratch. |
27 | */ |
28 | static int s_dbVersion = 2; |
29 | |
30 | bool Migrator::migrationRequired() const |
31 | { |
32 | return m_config->databaseVersion() != s_dbVersion; |
33 | } |
34 | |
35 | void Migrator::migrate() |
36 | { |
37 | Q_ASSERT(migrationRequired()); |
38 | |
39 | int dbVersion = m_config->databaseVersion(); |
40 | if (dbVersion == 0 && QFile::exists(fileName: m_dbPath + QStringLiteral("/file"))) { |
41 | QDir dir(m_dbPath + QStringLiteral("/file")); |
42 | dir.removeRecursively(); |
43 | } |
44 | else if (QFile::exists(fileName: m_dbPath + QStringLiteral("/index"))) { |
45 | QFile::remove(fileName: m_dbPath + QStringLiteral("/index")); |
46 | QFile::remove(fileName: m_dbPath + QStringLiteral("/index-lock")); |
47 | } |
48 | |
49 | m_config->setDatabaseVersion(s_dbVersion); |
50 | } |
51 |