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
|
/*
* Copyright (C) 2012-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "DatabaseManager.h"
#include "ServiceBroker.h"
#include "TextureDatabase.h"
#include "addons/AddonDatabase.h"
#include "music/MusicDatabase.h"
#include "pvr/PVRDatabase.h"
#include "pvr/epg/EpgDatabase.h"
#include "settings/AdvancedSettings.h"
#include "settings/SettingsComponent.h"
#include "utils/log.h"
#include "video/VideoDatabase.h"
#include "view/ViewDatabase.h"
#include <mutex>
using namespace PVR;
CDatabaseManager::CDatabaseManager() :
m_bIsUpgrading(false)
{
// Initialize the addon database (must be before the addon manager is init'd)
ADDON::CAddonDatabase db;
UpdateDatabase(db);
}
CDatabaseManager::~CDatabaseManager() = default;
void CDatabaseManager::Initialize()
{
std::unique_lock<CCriticalSection> lock(m_section);
m_dbStatus.clear();
CLog::Log(LOGDEBUG, "{}, updating databases...", __FUNCTION__);
const std::shared_ptr<CAdvancedSettings> advancedSettings = CServiceBroker::GetSettingsComponent()->GetAdvancedSettings();
// NOTE: Order here is important. In particular, CTextureDatabase has to be updated
// before CVideoDatabase.
{
ADDON::CAddonDatabase db;
UpdateDatabase(db);
}
{ CViewDatabase db; UpdateDatabase(db); }
{ CTextureDatabase db; UpdateDatabase(db); }
{ CMusicDatabase db; UpdateDatabase(db, &advancedSettings->m_databaseMusic); }
{ CVideoDatabase db; UpdateDatabase(db, &advancedSettings->m_databaseVideo); }
{ CPVRDatabase db; UpdateDatabase(db, &advancedSettings->m_databaseTV); }
{ CPVREpgDatabase db; UpdateDatabase(db, &advancedSettings->m_databaseEpg); }
CLog::Log(LOGDEBUG, "{}, updating databases... DONE", __FUNCTION__);
m_bIsUpgrading = false;
}
bool CDatabaseManager::CanOpen(const std::string &name)
{
std::unique_lock<CCriticalSection> lock(m_section);
std::map<std::string, DB_STATUS>::const_iterator i = m_dbStatus.find(name);
if (i != m_dbStatus.end())
return i->second == DB_READY;
return false; // db isn't even attempted to update yet
}
void CDatabaseManager::UpdateDatabase(CDatabase &db, DatabaseSettings *settings)
{
std::string name = db.GetBaseDBName();
UpdateStatus(name, DB_UPDATING);
if (Update(db, settings ? *settings : DatabaseSettings()))
UpdateStatus(name, DB_READY);
else
UpdateStatus(name, DB_FAILED);
}
bool CDatabaseManager::Update(CDatabase &db, const DatabaseSettings &settings)
{
DatabaseSettings dbSettings = settings;
db.InitSettings(dbSettings);
int version = db.GetSchemaVersion();
std::string latestDb = dbSettings.name;
latestDb += std::to_string(version);
while (version >= db.GetMinSchemaVersion())
{
std::string dbName = dbSettings.name;
if (version)
dbName += std::to_string(version);
if (db.Connect(dbName, dbSettings, false))
{
// Database exists, take a copy for our current version (if needed) and reopen that one
if (version < db.GetSchemaVersion())
{
CLog::Log(LOGINFO, "Old database found - updating from version {} to {}", version,
db.GetSchemaVersion());
m_bIsUpgrading = true;
bool copy_fail = false;
try
{
db.CopyDB(latestDb);
}
catch (...)
{
CLog::Log(LOGERROR, "Unable to copy old database {} to new version {}", dbName, latestDb);
copy_fail = true;
}
db.Close();
if (copy_fail)
return false;
if (!db.Connect(latestDb, dbSettings, false))
{
CLog::Log(LOGERROR, "Unable to open freshly copied database {}", latestDb);
return false;
}
}
// yay - we have a copy of our db, now do our worst with it
if (UpdateVersion(db, latestDb))
return true;
// update failed - loop around and see if we have another one available
db.Close();
}
// drop back to the previous version and try that
version--;
}
// try creating a new one
if (db.Connect(latestDb, dbSettings, true))
return true;
// failed to update or open the database
db.Close();
CLog::Log(LOGERROR, "Unable to create new database");
return false;
}
bool CDatabaseManager::UpdateVersion(CDatabase &db, const std::string &dbName)
{
int version = db.GetDBVersion();
bool bReturn = false;
if (version < db.GetMinSchemaVersion())
{
CLog::Log(LOGERROR, "Can't update database {} from version {} - it's too old", dbName, version);
return false;
}
else if (version < db.GetSchemaVersion())
{
CLog::Log(LOGINFO, "Attempting to update the database {} from version {} to {}", dbName,
version, db.GetSchemaVersion());
bool success = true;
db.BeginTransaction();
try
{
// drop old analytics
db.DropAnalytics();
}
catch (...)
{
success = false;
}
if (!success)
{
CLog::Log(LOGERROR, "Exception droping old analytics from {}", dbName);
db.RollbackTransaction();
return false;
}
db.CommitTransaction();
db.BeginTransaction();
try
{
// update table(s), recreate analytics, update version
db.UpdateTables(version);
db.CreateAnalytics();
db.UpdateVersionNumber();
}
catch (...)
{
CLog::Log(LOGERROR, "Exception updating database {} from version {} to {}", dbName, version,
db.GetSchemaVersion());
success = false;
}
if (!success)
{
CLog::Log(LOGERROR, "Error updating database {} from version {} to {}", dbName, version,
db.GetSchemaVersion());
db.RollbackTransaction();
return false;
}
bReturn = db.CommitTransaction();
CLog::Log(LOGINFO, "Update to version {} successful", db.GetSchemaVersion());
}
else if (version > db.GetSchemaVersion())
{
bReturn = false;
CLog::Log(LOGERROR,
"Can't open the database {} as it is a NEWER version than what we were expecting?",
dbName);
}
else
{
bReturn = true;
CLog::Log(LOGINFO, "Running database version {}", dbName);
}
return bReturn;
}
void CDatabaseManager::UpdateStatus(const std::string &name, DB_STATUS status)
{
std::unique_lock<CCriticalSection> lock(m_section);
m_dbStatus[name] = status;
}
|