Skip to content

Commit 80756c1

Browse files
ndelanoups-filipefreitasbartekpacia
authored
Handle migration for PR #728 (#757)
* Update DBManager.m download_tasks.db should be created in internal app directory (#722) * migrate database file from documents directory to app directory on init * Update ios/Classes/DBManager.m Co-authored-by: ps-filipefreitas <[email protected]> Co-authored-by: ndelanou <[email protected]> Co-authored-by: Bartek Pacia <[email protected]>
1 parent 7f3c1be commit 80756c1

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

ios/Classes/DBManager.m

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111
@interface DBManager()
1212

13-
@property (nonatomic, strong) NSString *documentsDirectory;
13+
@property (nonatomic, strong) NSString *appDirectory;
1414
@property (nonatomic, strong) NSString *databaseFilePath;
1515
@property (nonatomic, strong) NSString *databaseFilename;
1616
@property (nonatomic, strong) NSMutableArray *arrResults;
1717

18-
-(void)copyDatabaseIntoDocumentsDirectory;
18+
-(void)copyDatabaseIntoAppDirectory;
1919
-(void)runQuery:(const char *)query isQueryExecutable:(BOOL)queryExecutable;
2020

2121
@end
@@ -27,30 +27,38 @@ @implementation DBManager
2727
-(instancetype)initWithDatabaseFilePath:(NSString *)dbFilePath{
2828
self = [super init];
2929
if (self) {
30-
// Set the documents directory path to the documentsDirectory property.
31-
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
32-
self.documentsDirectory = [paths objectAtIndex:0];
30+
// Set the app directory path to the application support files property.
31+
self.appDirectory = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) firstObject];
3332

3433
// Keep the database filepath
3534
self.databaseFilePath = dbFilePath;
3635

3736
// Keep the database filename.
3837
self.databaseFilename = [dbFilePath lastPathComponent];
3938

40-
// Copy the database file into the documents directory if necessary.
41-
[self copyDatabaseIntoDocumentsDirectory];
39+
// Copy the database file into the app directory if necessary.
40+
[self copyDatabaseIntoAppDirectory];
4241
}
4342
return self;
4443
}
4544

46-
-(void)copyDatabaseIntoDocumentsDirectory{
47-
// Check if the database file exists in the documents directory.
48-
NSString *destinationPath = [self.documentsDirectory stringByAppendingPathComponent:self.databaseFilename];
45+
// Will be removed in the next major version.
46+
-(void)copyDatabaseIntoAppDirectory{
47+
// Check if the database file exists in the app directory.
48+
NSString *destinationPath = [self.appDirectory stringByAppendingPathComponent:self.databaseFilename];
4949
if (![[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) {
50-
// The database file does not exist in the documents directory, so copy it from the main bundle now.
51-
NSString *sourcePath = self.databaseFilePath;
50+
51+
// Attemp database file migration from the documents directory if exists
52+
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
53+
NSString *migrationSourcePath = [documentsDirectory stringByAppendingPathComponent:self.databaseFilename];
5254
NSError *error;
53-
[[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:destinationPath error:&error];
55+
if ([[NSFileManager defaultManager] fileExistsAtPath:migrationSourcePath]) {
56+
// Migrate the database file from the documents directory to the app directory
57+
[[NSFileManager defaultManager] moveItemAtPath:migrationSourcePath toPath:destinationPath error:&error];
58+
} else {
59+
// The database file does not exist in the app directory, so copy it from the main bundle now.
60+
[[NSFileManager defaultManager] copyItemAtPath:self.databaseFilePath toPath:destinationPath error:&error];
61+
}
5462

5563
// Check if any error occurred during copying and display it.
5664
if (debug) {
@@ -72,7 +80,7 @@ -(void)runQuery:(const char *)query isQueryExecutable:(BOOL)queryExecutable{
7280
sqlite3 *sqlite3Database;
7381

7482
// Set the database file path.
75-
NSString *databasePath = [self.documentsDirectory stringByAppendingPathComponent:self.databaseFilename];
83+
NSString *databasePath = [self.appDirectory stringByAppendingPathComponent:self.databaseFilename];
7684

7785
// Initialize the results array.
7886
if (self.arrResults != nil) {

0 commit comments

Comments
 (0)