Skip to content

Commit 93feb18

Browse files
committed
refactored database path creation
1 parent a6bc172 commit 93feb18

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

source/gameanalytics/GAStore.cpp

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -207,48 +207,69 @@ namespace gameanalytics
207207
return sqlDatabase;
208208
}
209209

210-
bool GAStore::fixOldDatabase()
210+
bool GAStore::fixOldDatabaseIfRequired()
211211
{
212-
std::string oldPath = device::GADevice::getWritablePath() + "/ga.sqlite3";
213-
std::string newPath = device::GADevice::getWritablePath() + "/" + state::GAState::getGameKey() + "/ga.sqlite3";
214-
if(std::filesystem::exists(oldPath) && !std::filesystem::exists(newPath))
212+
std::filesystem::path oldPath = dbPath;
213+
std::filesystem::path filename = oldPath.filename();
214+
215+
// get location of the faulty database
216+
oldPath = oldPath.parent_path() / ".." / filename;
217+
218+
// check if no new database exists
219+
if(std::filesystem::exists(oldPath) && !std::filesystem::exists(dbPath))
215220
{
216-
std::filesystem::rename(oldPath, newPath);
217-
return true;
221+
std::error_code ec = {};
222+
223+
// move the database to the correct location
224+
std::filesystem::rename(oldPath, dbPath, ec);
225+
226+
return ec.value() != 0;
218227
}
219228

220229
return false;
221230
}
222-
223-
bool GAStore::ensureDatabase(bool dropDatabase, std::string const& key)
231+
232+
bool GAStore::initDatabaseLocation()
224233
{
225-
// lazy creation of db path
226-
if(getInstance().dbPath.empty())
234+
constexpr const char* DATABASE_NAME = "ga.sqlite3";
235+
236+
// get writable path for this platform
237+
std::filesystem::path p = device::GADevice::getWritablePath();
238+
239+
// get an unique game folder using the game key
240+
p /= state::GAState::getGameKey();
241+
242+
dbPath = (p / DATABASE_NAME).string();
243+
244+
// check if the directory already exists
245+
if(!std::filesystem::exists(p))
227246
{
228-
std::string dir = device::GADevice::getWritablePath() + "/" + key;
229-
if(!std::filesystem::exists(dir))
230-
{
231-
std::filesystem::create_directory(dir);
232-
}
247+
// check if the directory has been created
248+
if(!std::filesystem::create_directory(p))
249+
return false;
233250

234-
std::string path = dir + "/ga.sqlite3";
235-
getInstance().dbPath = path;
251+
fixOldDatabaseIfRequired();
236252
}
237253

238-
const std::string dbPath = getInstance().dbPath;
239-
const bool shouldFixDb = getInstance().fixOldDatabase();
254+
return true;
255+
}
240256

257+
bool GAStore::ensureDatabase(bool dropDatabase, std::string const& key)
258+
{
259+
// lazy creation of db path
260+
getInstance().initDatabaseLocation();
261+
241262
// Open database
242-
if (sqlite3_open(dbPath.c_str(), &getInstance().sqlDatabase) != SQLITE_OK)
263+
if (sqlite3_open(getInstance().dbPath.c_str(), &getInstance().sqlDatabase) != SQLITE_OK)
243264
{
244265
getInstance().dbReady = false;
245-
logging::GALogger::w("Could not open database: %s", dbPath.c_str());
266+
logging::GALogger::w("Could not open database: %s", getInstance().dbPath.c_str());
246267
return false;
247268
}
248269
else
249270
{
250271
getInstance().dbReady = true;
251-
logging::GALogger::i("Database opened: %s", dbPath.c_str());
272+
logging::GALogger::i("Database opened: %s", getInstance().dbPath.c_str());
252273
}
253274

254275
if (dropDatabase)

source/gameanalytics/GAStore.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ namespace gameanalytics
5050

5151
static GAStore& getInstance();
5252

53-
bool fixOldDatabase();
53+
bool fixOldDatabaseIfRequired();
5454
bool trimEventTable();
55+
56+
bool initDatabaseLocation();
5557

5658
// set when calling "ensureDatabase"
5759
// using a "writablePath" that needs to be set into the C++ component before

0 commit comments

Comments
 (0)