mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.5 KiB
Matlab
112 lines
3.5 KiB
Matlab
8 years ago
|
//
|
||
6 years ago
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
||
8 years ago
|
//
|
||
9 years ago
|
|
||
|
#import "OWSDatabaseMigrationRunner.h"
|
||
7 years ago
|
#import "OWSDatabaseMigration.h"
|
||
4 years ago
|
#import <SignalUtilitiesKit/SignalUtilitiesKit-Swift.h>
|
||
4 years ago
|
#import <SessionUtilitiesKit/AppContext.h>
|
||
9 years ago
|
|
||
|
NS_ASSUME_NONNULL_BEGIN
|
||
|
|
||
|
@implementation OWSDatabaseMigrationRunner
|
||
|
|
||
6 years ago
|
#pragma mark - Dependencies
|
||
|
|
||
|
- (OWSPrimaryStorage *)primaryStorage
|
||
|
{
|
||
|
OWSAssertDebug(SSKEnvironment.shared.primaryStorage);
|
||
|
|
||
|
return SSKEnvironment.shared.primaryStorage;
|
||
|
}
|
||
|
|
||
|
#pragma mark -
|
||
|
|
||
7 years ago
|
// This should all migrations which do NOT qualify as safeBlockingMigrations:
|
||
9 years ago
|
- (NSArray<OWSDatabaseMigration *> *)allMigrations
|
||
|
{
|
||
4 years ago
|
return @[
|
||
4 years ago
|
[SNContactsMigration new],
|
||
|
[SNClosedGroupsV2Migration new]
|
||
4 years ago
|
];
|
||
7 years ago
|
}
|
||
|
|
||
9 years ago
|
- (void)assumeAllExistingMigrationsRun
|
||
|
{
|
||
|
for (OWSDatabaseMigration *migration in self.allMigrations) {
|
||
7 years ago
|
OWSLogInfo(@"Skipping migration on new install: %@", migration);
|
||
9 years ago
|
[migration save];
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
- (void)runAllOutstandingWithCompletion:(OWSDatabaseMigrationCompletion)completion
|
||
9 years ago
|
{
|
||
6 years ago
|
[self removeUnknownMigrations];
|
||
|
|
||
7 years ago
|
[self runMigrations:[self.allMigrations mutableCopy] completion:completion];
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
// Some users (especially internal users) will move back and forth between
|
||
|
// app versions. Whenever they move "forward" in the version history, we
|
||
|
// want them to re-run any new migrations. Therefore, when they move "backward"
|
||
|
// in the version history, we cull any unknown migrations.
|
||
|
- (void)removeUnknownMigrations
|
||
|
{
|
||
|
NSMutableSet<NSString *> *knownMigrationIds = [NSMutableSet new];
|
||
|
for (OWSDatabaseMigration *migration in self.allMigrations) {
|
||
|
[knownMigrationIds addObject:migration.uniqueId];
|
||
|
}
|
||
|
|
||
4 years ago
|
[OWSPrimaryStorage.sharedManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
||
6 years ago
|
NSArray<NSString *> *savedMigrationIds = [transaction allKeysInCollection:OWSDatabaseMigration.collection];
|
||
|
|
||
|
NSMutableSet<NSString *> *unknownMigrationIds = [NSMutableSet new];
|
||
|
[unknownMigrationIds addObjectsFromArray:savedMigrationIds];
|
||
|
[unknownMigrationIds minusSet:knownMigrationIds];
|
||
|
|
||
|
for (NSString *unknownMigrationId in unknownMigrationIds) {
|
||
|
OWSLogInfo(@"Culling unknown migration: %@", unknownMigrationId);
|
||
|
[transaction removeObjectForKey:unknownMigrationId inCollection:OWSDatabaseMigration.collection];
|
||
|
}
|
||
5 years ago
|
}];
|
||
6 years ago
|
}
|
||
|
|
||
7 years ago
|
// Run migrations serially to:
|
||
|
//
|
||
|
// * Ensure predictable ordering.
|
||
|
// * Prevent them from interfering with each other (e.g. deadlock).
|
||
|
- (void)runMigrations:(NSMutableArray<OWSDatabaseMigration *> *)migrations
|
||
7 years ago
|
completion:(OWSDatabaseMigrationCompletion)completion
|
||
8 years ago
|
{
|
||
7 years ago
|
OWSAssertDebug(migrations);
|
||
|
OWSAssertDebug(completion);
|
||
8 years ago
|
|
||
7 years ago
|
// If there are no more migrations to run, complete.
|
||
|
if (migrations.count < 1) {
|
||
7 years ago
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||
|
completion();
|
||
|
});
|
||
|
return;
|
||
|
}
|
||
|
|
||
7 years ago
|
// Pop next migration from front of queue.
|
||
|
OWSDatabaseMigration *migration = migrations.firstObject;
|
||
|
[migrations removeObjectAtIndex:0];
|
||
|
|
||
|
// If migration has already been run, skip it.
|
||
|
if ([OWSDatabaseMigration fetchObjectWithUniqueID:migration.uniqueId] != nil) {
|
||
|
[self runMigrations:migrations completion:completion];
|
||
|
return;
|
||
9 years ago
|
}
|
||
7 years ago
|
|
||
7 years ago
|
OWSLogInfo(@"Running migration: %@", migration);
|
||
7 years ago
|
[migration runUpWithCompletion:^{
|
||
7 years ago
|
OWSLogInfo(@"Migration complete: %@", migration);
|
||
7 years ago
|
[self runMigrations:migrations completion:completion];
|
||
|
}];
|
||
9 years ago
|
}
|
||
|
|
||
|
@end
|
||
|
|
||
|
NS_ASSUME_NONNULL_END
|