Skip to content

Commit 81b33cc

Browse files
dorin-gaGogoshika-ga
authored andcommitted
check for existing database
fix default path collision
1 parent b897cd0 commit 81b33cc

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
lines changed

source/gameanalytics/GAStore.cpp

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,32 +207,61 @@ namespace gameanalytics
207207
return sqlDatabase;
208208
}
209209

210-
bool GAStore::ensureDatabase(bool dropDatabase, std::string const& key)
210+
bool GAStore::fixOldDatabaseIfRequired()
211+
{
212+
std::filesystem::path oldPath = dbPath;
213+
std::filesystem::path filename = oldPath.filename();
214+
215+
oldPath = oldPath.parent_path() / ".." / filename;
216+
217+
if(std::filesystem::exists(oldPath) && !std::filesystem::exists(dbPath))
218+
{
219+
std::error_code ec = {};
220+
std::filesystem::rename(oldPath, dbPath, ec);
221+
222+
return ec.value() != 0;
223+
}
224+
225+
return false;
226+
}
227+
228+
bool GAStore::initDatabaseLocation()
211229
{
212-
// lazy creation of db path
213-
if(getInstance().dbPath.empty())
230+
constexpr const char* DATABASE_NAME = "ga.sqlite3";
231+
232+
std::filesystem::path p = device::GADevice::getWritablePath();
233+
234+
p /= state::GAState::getGameKey();
235+
236+
dbPath = (p / DATABASE_NAME).string();
237+
if(!std::filesystem::exists(p))
214238
{
215-
std::string path = device::GADevice::getWritablePath();
216-
path += "/ga.sqlite3";
239+
if(!std::filesystem::create_directory(p))
240+
return false;
217241

218-
getInstance().dbPath = path;
242+
fixOldDatabaseIfRequired();
219243
}
220244

221-
const std::string dbPath = getInstance().dbPath;
245+
return true;
246+
}
222247

248+
bool GAStore::ensureDatabase(bool dropDatabase, std::string const& key)
249+
{
250+
getInstance().initDatabaseLocation();
251+
223252
// Open database
224-
if (sqlite3_open(dbPath.c_str(), &getInstance().sqlDatabase) != SQLITE_OK)
253+
if (sqlite3_open(getInstance().dbPath.c_str(), &getInstance().sqlDatabase) != SQLITE_OK)
225254
{
226255
getInstance().dbReady = false;
227-
logging::GALogger::w("Could not open database: %s", dbPath.c_str());
256+
logging::GALogger::w("Could not open database: %s", getInstance().dbPath.c_str());
228257
return false;
229258
}
230259
else
231260
{
232261
getInstance().dbReady = true;
233-
logging::GALogger::i("Database opened: %s", dbPath.c_str());
262+
logging::GALogger::i("Database opened: %s", getInstance().dbPath.c_str());
234263
}
235-
264+
236265
if (dropDatabase)
237266
{
238267
logging::GALogger::d("Drop tables");

source/gameanalytics/GAStore.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ namespace gameanalytics
5050

5151
static GAStore& getInstance();
5252

53+
bool fixOldDatabaseIfRequired();
5354
bool trimEventTable();
55+
56+
bool initDatabaseLocation();
5457

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

source/gameanalytics/GameAnalytics.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,7 @@ namespace gameanalytics
356356
{
357357
return;
358358
}
359-
360-
359+
361360
threading::GAThreading::performTaskOnGAThread([gameKey, gameSecret]()
362361
{
363362
if (isSdkReady(true, false))

source/gameanalytics/Platform/GADevicePlatform.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,3 @@ namespace gameanalytics
4949
return std::make_unique<GADevicePlatform>();
5050
}
5151
}
52-
53-

0 commit comments

Comments
 (0)