Skip to content

Commit b165aa3

Browse files
committed
Implement RCTStorageDirectoryMigrationCheck
1 parent 15eaafe commit b165aa3

File tree

1 file changed

+56
-6
lines changed

1 file changed

+56
-6
lines changed

ios/RNCAsyncStorage.m

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static void RCTAppendError(NSDictionary *error, NSMutableArray<NSDictionary *> *
8585
#else
8686
storageDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
8787
#endif
88-
storageDirectoryPath = [storageDirectoryPath stringByAppendingPathComponent:RCTStorageDirectory];
88+
storageDirectoryPath = [storageDirectoryPath stringByAppendingPathComponent:storageDir];
8989
return storageDirectoryPath;
9090
}
9191

@@ -174,10 +174,60 @@ static dispatch_queue_t RCTGetMethodQueue()
174174
return error ? RCTMakeError(@"Failed to delete storage directory.", error, nil) : nil;
175175
}
176176

177-
static void RCTPerformDirectoryMigrationCheck() {
178-
// NSString *oldRCTStorageDirectory = @"RNCAsyncLocalStorage_V1";
179-
// NSString *storageDir = RCTCreateStorageDirectoryPath(oldRCTStorageDirectory);
180-
// printf("%s%s\n", "TESTING: ", [storageDir UTF8String]);
177+
/**
178+
* This check is added to make sure that anyone coming from pre-1.2.2 does not lose cached data.
179+
* Data is migrated from the "RNCAsyncLocalStorage_V1" directory to the "RCTAsyncLocalStorage_V1" directory.
180+
*/
181+
static void RCTStorageDirectoryMigrationCheck() {
182+
NSString *oldStorageDir = RCTCreateStorageDirectoryPath(@"RNCAsyncLocalStorage_V1");
183+
184+
BOOL isDir;
185+
BOOL oldExists = [[NSFileManager defaultManager] fileExistsAtPath:oldStorageDir isDirectory:&isDir];
186+
187+
// If the old directory exists, it means we need to migrate data to the new directory
188+
if (oldExists && isDir) {
189+
NSError *error;
190+
BOOL migrationSuccess;
191+
// If the new storage directory already exists, then this may be caused by ever older data being left behind from previous versions.
192+
// This old data will need to be overwritten.
193+
NSString *newStorageDir = RCTGetStorageDirectory();
194+
BOOL newExists = [[NSFileManager defaultManager] fileExistsAtPath:newStorageDir isDirectory:&isDir];
195+
if (newExists && isDir) {
196+
NSString *backupStorageDir = RCTCreateStorageDirectoryPath(@"RCTBackupAsyncLocalStorage_V1");
197+
// Replace any possible existing data in the new storage with old storage directory
198+
migrationSuccess = [[NSFileManager defaultManager] replaceItemAtURL:[[NSURL alloc] initWithString:newStorageDir]
199+
withItemAtURL:[[NSURL alloc] initWithString:oldStorageDir]
200+
backupItemName:backupStorageDir
201+
options:NSFileManagerItemReplacementUsingNewMetadataOnly
202+
resultingItemURL:nil
203+
error:&error];
204+
if (error || !success) {
205+
// Attempt to recover from failed overwriting
206+
[[NSFileManager defaultManager] removeItemAtPath:newStorageDir error:nil];
207+
[[NSFileManager defaultManager] copyItemAtPath:backupStorageDir toPath:newStorageDir error:nil];
208+
[[NSFileManager defaultManager] removeItemAtPath:backupStorageDir error:nil];
209+
if (error) {
210+
RCTMakeError(@"Failed to overwrite old storage directory to new storage directory location during migration", error, nil);
211+
return;
212+
}
213+
}
214+
} else {
215+
// Copy over the old storage directory to new storage directory path
216+
migrationSuccess = [[NSFileManager defaultManager] copyItemAtPath:oldStorageDir toPath:newStorageDir error:&error];
217+
if (error) {
218+
RCTMakeError(@"Failed to copy old storage directory to new storage directory location during migration", error, nil);
219+
return;
220+
}
221+
}
222+
223+
// If the migration was a success, remove old storage directory
224+
if (migrationSuccess) {
225+
[[NSFileManager defaultManager] removeItemAtPath:oldStorageDir error:error];
226+
if (error) {
227+
RCTMakeError(@"Failed to remove old storage directory after migration", error, nil);
228+
}
229+
}
230+
}
181231
}
182232

183233
#pragma mark - RNCAsyncStorage
@@ -200,7 +250,7 @@ - (instancetype)init
200250
if (!(self = [super init])) {
201251
return nil;
202252
}
203-
RCTPerformDirectoryMigrationCheck();
253+
RCTStorageDirectoryMigrationCheck();
204254
return self;
205255
}
206256

0 commit comments

Comments
 (0)