diff --git a/native/ios/Comm/AppDelegate.mm b/native/ios/Comm/AppDelegate.mm --- a/native/ios/Comm/AppDelegate.mm +++ b/native/ios/Comm/AppDelegate.mm @@ -19,6 +19,8 @@ #import "NetworkModule.h" #import "SQLiteQueryExecutor.h" #import "Tools.h" +#import +#import #import #ifdef FB_SONARKIT_ENABLED @@ -232,6 +234,25 @@ - (void)attemptDatabaseInitialization { std::string sqliteFilePath = std::string([[Tools getSQLiteFilePath] UTF8String]); + /* + Previous versions of Comm app used to keep SQLite database at location + that was specific to the app. Now that we share SQLite database with + NotificationService extension we need to keep the database in place + defined by App Groups. The code below is a migration fired if user + upgrades from version using app-specific path to newer that uses + App Groups. + */ + NSString *appSpecificSQLiteFilePath = [Tools getAppSpecificSQLiteFilePath]; + if ([NSFileManager.defaultManager + fileExistsAtPath:appSpecificSQLiteFilePath]) { + if (std::rename( + std::string([appSpecificSQLiteFilePath UTF8String]).c_str(), + sqliteFilePath.c_str())) { + throw std::runtime_error( + "Failed to move SQLite database from app-specific to app group " + "location"); + } + } comm::SQLiteQueryExecutor::initialize(sqliteFilePath); } diff --git a/native/ios/Comm/Tools.h b/native/ios/Comm/Tools.h --- a/native/ios/Comm/Tools.h +++ b/native/ios/Comm/Tools.h @@ -3,5 +3,6 @@ #import @interface Tools : NSObject ++ (NSString *)getAppSpecificSQLiteFilePath; + (NSString *)getSQLiteFilePath; @end diff --git a/native/ios/Comm/Tools.mm b/native/ios/Comm/Tools.mm --- a/native/ios/Comm/Tools.mm +++ b/native/ios/Comm/Tools.mm @@ -5,6 +5,16 @@ @implementation Tools + (NSString *)getSQLiteFilePath { + NSURL *groupUrl = [NSFileManager.defaultManager + containerURLForSecurityApplicationGroupIdentifier:@"group.app.comm"]; + if (groupUrl == nil) { + throw std::runtime_error( + "Failed to resolve database path - could not find groupUrl"); + } + return [groupUrl URLByAppendingPathComponent:@"comm.sqlite"].path; +} + ++ (NSString *)getAppSpecificSQLiteFilePath { NSError *err = nil; NSURL *documentsUrl = [NSFileManager.defaultManager URLForDirectory:NSDocumentDirectory