@@ -207,18 +207,36 @@ namespace gameanalytics
207207 return sqlDatabase;
208208 }
209209
210+ bool GAStore::fixOldDatabase ()
211+ {
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))
215+ {
216+ std::filesystem::rename (oldPath, newPath);
217+ return true ;
218+ }
219+
220+ return false ;
221+ }
222+
210223 bool GAStore::ensureDatabase (bool dropDatabase, std::string const & key)
211224 {
212225 // lazy creation of db path
213226 if (getInstance ().dbPath .empty ())
214227 {
215- std::string path = device::GADevice::getWritablePath ();
216- path += utilities::printString (" /%s/ga.sqlite3" , key.c_str ());
228+ std::string dir = device::GADevice::getWritablePath () + " /" + key;
229+ if (!std::filesystem::exists (dir))
230+ {
231+ std::filesystem::create_directory (dir);
232+ }
217233
234+ std::string path = dir + " /ga.sqlite3" ;
218235 getInstance ().dbPath = path;
219236 }
220237
221238 const std::string dbPath = getInstance ().dbPath ;
239+ const bool shouldFixDb = getInstance ().fixOldDatabase ();
222240
223241 // Open database
224242 if (sqlite3_open (dbPath.c_str (), &getInstance ().sqlDatabase ) != SQLITE_OK)
@@ -232,7 +250,7 @@ namespace gameanalytics
232250 getInstance ().dbReady = true ;
233251 logging::GALogger::i (" Database opened: %s" , dbPath.c_str ());
234252 }
235-
253+
236254 if (dropDatabase)
237255 {
238256 logging::GALogger::d (" Drop tables" );
@@ -242,6 +260,13 @@ namespace gameanalytics
242260 GAStore::executeQuerySync (" DROP TABLE ga_progression" );
243261 GAStore::executeQuerySync (" VACUUM" );
244262 }
263+ else if (shouldFixDb)
264+ {
265+ GAStore::executeQuerySync (" DROP TABLE ga_events" );
266+ GAStore::executeQuerySync (" DROP TABLE ga_progression" );
267+ GAStore::executeQuerySync (" DROP TABLE ga_session" );
268+ GAStore::executeQuerySync (" VACUUM" );
269+ }
245270
246271 // Create statements
247272 constexpr const char * sql_ga_events = " CREATE TABLE IF NOT EXISTS ga_events(status CHAR(50) NOT NULL, category CHAR(50) NOT NULL, session_id CHAR(50) NOT NULL, client_ts CHAR(50) NOT NULL, event TEXT NOT NULL);" ;
0 commit comments